Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python multiprocessing RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase

I tried to run a multiprocessing function on spyder3. After it wouldnt print anything inside a loop and be stuck infinitly i read that I could run it on an external terminal in spyder like this.

Run > Configuration per file > Execute in an external system terminal

now it finally shows me something. Sadly its this:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "c:\users\ben\appdata\local\programs\python\python37\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\ben\Arc_Project\löschbarer_test_der_mp_func.py", line 161, in <module>
    cpus,closeIdx=valueSeries.index,t1=df['t1'])
  File "C:\Users\ben\Arc_Project\löschbarer_test_der_mp_func.py", line 27, in mpPandasObj
    out=processJobs(jobs,numThreads=numThreads)
  File "C:\Users\ben\Arc_Project\löschbarer_test_der_mp_func.py", line 75, in processJobs
    pool=mp.Pool(processes=numThreads)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\context.py", line 119, in Pool
    context=self.get_context())
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\pool.py", line 176, in __init__
    self._repopulate_pool()
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\pool.py", line 241, in _repopulate_pool
    w.start()
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\process.py", line 112, in start
    self._popen = self._Popen(self)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\context.py", line 322, in _Popen
    return Popen(process_obj)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\popen_spawn_win32.py", line 46, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "c:\users\ben\appdata\local\programs\python\python37\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')

and the this occours in a (probably infinite) loop:

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.

this is the multiprocessing function:

def processJobs(jobs,task=None,numThreads=24): 
    # Run in parallel. 
    # jobs must contain a ’func’ callback, for expandCall 

    if task is None:
        task=jobs[0]['func'].__name__ 
        pool=mp.Pool(processes=numThreads) 

    outputs,out,time0=pool.imap_unordered(expandCall,jobs),[],time.time() 


    # Process asynchronous output, report progress  
    for i,out_ in enumerate(outputs,1):                            #   <---- Here. 
        out.append(out_) 
        reportProgress(i,len(jobs),time0,task) 
    pool.close(); pool.join() # this is needed to prevent memory leaks return out 

    return out

How can I solve this? (I really need multiprocessing)

EDIT: tried it on Pycharm -> it reports the same errors

like image 848
Benoid Avatar asked Nov 16 '22 11:11

Benoid


1 Answers

I just ran into this. You are probably directly calling processJobs() directly in the Python script.

You just need to add the main idiom like so:

if __name__ == '__main__':
    processJobs()

I suspect multiprocessing will fork/spawn multiple Python processes in the OS and load the module, and not having this guard causes the Pool instantiation to be invoked recursively.

Having the main guard ensures that the Pool instantiation runs only once.

like image 151
chnrxn Avatar answered Mar 29 '23 23:03

chnrxn