在osx上使用python 3.8
行import multiprocessing as mp
pool = mp.Pool(2)
导致错误的无限循环,但下面收集了其中的一个片段。
同样,mp.freeze_support()似乎也没有帮助。
Traceback (most recent call last):
File "<string>", line 1, in <module>
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
exitcode = _main(fd, parent_sentinel)
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 125, in _main
prepare(preparation_data)
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
prepare(preparation_data)
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
_fixup_main_from_path(data['init_main_from_path'])
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/multiprocessing/spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "/Users/biscuit/.conda/envs/advanced-projects/lib/python3.8/runpy.py", line 264, in run_path
main_content = runpy.run_path(main_path,
.
.
.
知道为什么吗?
编辑:
当时所有的解决方案都不起作用,但今天似乎起作用了?我确实安装了一些其他的包,让我对依赖项进行更新,所以也许它改变了我的多处理版本或其他一些修复它的包版本?
不管怎样,它现在工作,对不起,这不会对别人有太大的帮助/:
特别是当start方法为"spawn"(现在MacOS默认):
您必须只在if __name__ == "__main__":
块中创建子进程(创建池会创建子进程),因为子进程会导入__main__
文件。当导入发生时,文件被执行(就像任何导入一样),并且您将递归地继续创建越来越多的子进程,除非某种东西(如if __name__ ...
块)限制只执行父进程。