python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program
生活随笔
收集整理的这篇文章主要介绍了
python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
运行以下多进程测试代码时报错:
# -*- coding: utf-8 -*- """ @File : test_191205_测试多进程Multiprocessing_queue.py @Time : 2019/12/5 11:35 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import random import time import multiprocessingdef worker(name, q):t = 0for i in range(10):print(name + " " + str(i))x = random.randint(1, 3)t += xtime.sleep(x * 0.1)q.put(t)q = multiprocessing.Queue() jobs = [] for i in range(2):p = multiprocessing.Process(target=worker, args=(str(i), q))jobs.append(p)p.start()for p in jobs:p.join()results = [q.get() for j in jobs] print(results) D:\20191031_tensorflow_yolov3\python\python.exe D:/20191031_tensorflow_yolov3/needed/test/test_Intel_realsense/test_191205_测试多进程Multiprocessing_queue.py Traceback (most recent call last):File "<string>", line 1, in <module>File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 105, in spawn_mainexitcode = _main(fd)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 114, in _mainprepare(preparation_data)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 225, in prepare_fixup_main_from_path(data['init_main_from_path'])File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_pathrun_name="__mp_main__")File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 263, in run_pathpkg_name=pkg_name, script_name=fname)File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 96, in _run_module_codemod_name, mod_spec, pkg_name, script_name)File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 85, in _run_codeexec(code, run_globals)File "D:\20191031_tensorflow_yolov3\needed\test\test_Intel_realsense\test_191205_测试多进程Multiprocessing_queue.py", line 29, in <module>p.start()File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\process.py", line 105, in startself._popen = self._Popen(self)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\context.py", line 223, in _Popenreturn _default_context.get_context().Process._Popen(process_obj)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\context.py", line 322, in _Popenreturn Popen(process_obj)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__prep_data = spawn.get_preparation_data(process_obj._name)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 143, in get_preparation_data_check_not_importing_main()File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_mainis not going to be frozen to produce an executable.''') RuntimeError: An attempt has been made to start a new process before thecurrent process has finished its bootstrapping phase.This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module:if __name__ == '__main__':freeze_support()...The "freeze_support()" line can be omitted if the programis not going to be frozen to produce an executable. Traceback (most recent call last):File "<string>", line 1, in <module>File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 105, in spawn_mainexitcode = _main(fd)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 114, in _mainprepare(preparation_data)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 225, in prepare_fixup_main_from_path(data['init_main_from_path'])File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_pathrun_name="__mp_main__")File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 263, in run_pathpkg_name=pkg_name, script_name=fname)File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 96, in _run_module_codemod_name, mod_spec, pkg_name, script_name)File "D:\20191031_tensorflow_yolov3\python\lib\runpy.py", line 85, in _run_codeexec(code, run_globals)File "D:\20191031_tensorflow_yolov3\needed\test\test_Intel_realsense\test_191205_测试多进程Multiprocessing_queue.py", line 29, in <module>p.start()File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\process.py", line 105, in startself._popen = self._Popen(self)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\context.py", line 223, in _Popenreturn _default_context.get_context().Process._Popen(process_obj)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\context.py", line 322, in _Popenreturn Popen(process_obj)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__prep_data = spawn.get_preparation_data(process_obj._name)File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 143, in get_preparation_data_check_not_importing_main()File "D:\20191031_tensorflow_yolov3\python\lib\multiprocessing\spawn.py", line 136, in _check_not_importing_mainis not going to be frozen to produce an executable.''') RuntimeError: An attempt has been made to start a new process before thecurrent process has finished its bootstrapping phase.This probably means that you are not using fork to start yourchild processes and you have forgotten to use the proper idiomin the main module:if __name__ == '__main__':freeze_support()...The "freeze_support()" line can be omitted if the programis not going to be frozen to produce an executable.解决方法是将要执行的代码放到if __name__ == '__main__':下:
如:
原因:
这是一个关于windows上多进程实现的恩特。在windows上,子进程会自动import启动它的这个文件,而在import的时候是会自动执行这些语句的。如果不加__main__限制的化,就会无限递归创建子进程,进而报错。于是import的时候使用 name == “main” 保护起来就可以了。
参考文章1:python进程池multiprocessing.Pool运行错误:The freeze_support() line can be omitted if the program is not g
参考文章2:PyTorch:The “freeze_support()” line can be omitted if the program is not going to be frozen
总结
以上是生活随笔为你收集整理的python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: python 多进程 multiproc
- 下一篇: python 多进程multiproce