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