5

I have this simple code to make things simpler.

from multiprocessing import Process
def abc():
    print("HI")
    print(a)

a = 4
p = Process(target = abc)
p.start()

It works perfectly fine in ubuntu (Python 3.8.5) and provides the output:

HI
4

However, it fails in spyder (Python 3.9.5) "AttributeError: Can't get attribute 'abc' on <module 'main' (built-in)>" and macOS (Python 3.8.10, I tried other versions as well and failed) CLI "RuntimeError".

Spyder error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "multiprocessing/spawn.pyc", line 116, in spawn_main
  File "multiprocessing/spawn.pyc", line 126, in _main
AttributeError: Can't get attribute 'abc' on <module '__main__' (built-in)>
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "multiprocessing/spawn.pyc", line 116, in spawn_main
  File "multiprocessing/spawn.pyc", line 126, in _main
AttributeError: Can't get attribute 'abc' on <module '__main__' (built-in)>

MacOS-BigSur error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
    exitcode = _main(fd, parent_sentinel)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
    prepare(preparation_data)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
    main_content = runpy.run_path(main_path,
  File "/Users/asavas/opt/anaconda3/lib/python3.8/runpy.py", line 265, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/Users/asavas/opt/anaconda3/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/Users/asavas/opt/anaconda3/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/Users/asavas/delete.py", line 8, in <module>
    p.start()
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/process.py", line 121, in start
    self._popen = self._Popen(self)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/context.py", line 224, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/context.py", line 284, in _Popen
    return Popen(process_obj)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 32, in __init__
    super().__init__(process_obj)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/popen_fork.py", line 19, in __init__
    self._launch(process_obj)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/popen_spawn_posix.py", line 42, in _launch
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 154, in get_preparation_data
    _check_not_importing_main()
  File "/Users/asavas/opt/anaconda3/lib/python3.8/multiprocessing/spawn.py", line 134, in _check_not_importing_main
    raise RuntimeError('''
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

Trying to understand why and how can I resolve this issue?

Thanks

  • "CLI `RuntimeError`" - what does the error message say? – ForceBru Jun 01 '21 at 22:09
  • I think this is a duplicate of [this](https://stackoverflow.com/questions/41385708/multiprocessing-example-giving-attributeerror), but that question involved `Pool`. – Carcigenicate Jun 01 '21 at 22:09
  • iFor me in Ubuntu it works with python 3.8.5 and python 3.9.5, but on MacOS, python 3.9.0 gives erorr message `An attempt has been made to start a new process before the current process has finished its bootstrapping phase.` – Yuri Ginsburg Jun 01 '21 at 22:36
  • Just added the full error message – user3154126 Jun 02 '21 at 00:42
  • Did you read the full error message and then adapt your code accordingly, i.e. wrap the main code in the `if __name__ == "__main__"` idiom? – AKX Jun 02 '21 at 11:24

1 Answers1

3

It appears that this version of macOS has switched over to using the spawn rather than fork method of creating new processes. This means that when a new process is created, a new, empty address space is created, a new Python interpreter is launched and the source file is re-executed from the top. Consequently, any code that is at global scope and not within a block that begins if __name__ == '__main__': will get executed. That is why any code that creates processes must be in such a block or you will get in a recursive, process-creating loop that generates the errors you see. You simply need:

from multiprocessing import Process

def abc():
    print("HI")
    print(a)

# this will get re-executed in the new subprocess:
a = 4

if __name__ == '__main__':
    p = Process(target=abc)
    p.start()
    p.join() # explicitly wait for process to complete
Booboo
  • 38,656
  • 3
  • 37
  • 60
  • That worked for the MacOS piece but within Spyder, still getting the error message if I execute the code which I guess expected but if I run the file, I get no printouts. – user3154126 Jun 02 '21 at 16:22
  • Take a look at [this](https://stackoverflow.com/questions/67702357/attribute-error-when-running-a-pytorch-neural-network-in-spyder). Perhaps it addresses your issue. – Booboo Jun 02 '21 at 16:38
  • 3
    "Spawn" was made the default start method on python 3.8+ on MacOS where it previously defaulted to "fork". other *nix systems still default to "fork" and windows continues to only offer "spawn" (as of early 2021). It may be that you have multiple python installations, and spyder is using an older or newer version. – Aaron Jun 02 '21 at 17:33