python进程池multiprocessing.Pool运行错误:

错误:

Traceback (most recent call last):
  File "", line 1, in
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 106, in spawn_main
    exitcode = _main(fd)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 115, in _main
    prepare(preparation_data)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 226, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 278, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Program Files\Anaconda3\lib\runpy.py", line 254, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Program Files\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Program Files\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\pycharm\python_accelerate\pkg_acc.py", line 121, in
    pool = multiprocessing.Pool(processes=3)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\context.py", line 118, in Pool
    context=self.get_context())
  File "C:\Program Files\Anaconda3\lib\multiprocessing\pool.py", line 168, in __init__
    self._repopulate_pool()
  File "C:\Program Files\Anaconda3\lib\multiprocessing\pool.py", line 233, in _repopulate_pool
    w.start()
  File "C:\Program Files\Anaconda3\lib\multiprocessing\process.py", line 105, in start
    self._popen = self._Popen(self)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\context.py", line 313, in _Popen
    return Popen(process_obj)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\popen_spawn_win32.py", line 34, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 144, in get_preparation_data
    _check_not_importing_main()
  File "C:\Program Files\Anaconda3\lib\multiprocessing\spawn.py", line 137, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
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.

代码:

def muchjob(x):
    time.sleep(2)
    return x**2

tic = time.time()
import time

ans = [muchjob(i) for i in range(4)]
print('ans', ans)
toc = time.time()
print('used {:.10}s'.format(toc - tic))
print('*' * 20)

tic = time.time()
import multiprocessing
pool = multiprocessing.Pool(processes=3)
res = []
for i in range(4):
    res.append(pool.apply_async(muchjob, (i,)))
pool.close()
pool.join()
ans = [r.get() for r in res]
print('ans', ans)
toc = time.time()
print('used {:.10}s'.format(toc-tic))

这是由于mutiprocessing要写在main下,修改如下:

def muchjob(x):
    time.sleep(2)
    return x**2

if __name__ == '__main__':
    tic = time.time()
    import time
    
    ans = [muchjob(i) for i in range(4)]
    print('ans', ans)
    toc = time.time()
    print('used {:.10}s'.format(toc - tic))
    print('*' * 20)

    tic = time.time()
    import multiprocessing
    pool = multiprocessing.Pool(processes=3)
    res = []
    for i in range(4):
        res.append(pool.apply_async(muchjob, (i,)))
    pool.close()
    pool.join()
    ans = [r.get() for r in res]
    print('ans', ans)
    toc = time.time()
    print('used {:.10}s'.format(toc-tic))

reference:

https://blog.csdn.net/jacke121/article/details/79826420

https://stackoverflow.com/questions/29690091/python2-7-exception-the-freeze-support-line-can-be-omitted-if-the-program

你可能感兴趣的:(Python)