python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program
運(yùn)行以下多進(jìn)程測(cè)試代碼時(shí)報(bào)錯(cuò):
# -*- coding: utf-8 -*- """ @File : test_191205_測(cè)試多進(jìn)程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_測(cè)試多進(jìn)程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_測(cè)試多進(jìn)程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_測(cè)試多進(jìn)程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.解決方法是將要執(zhí)行的代碼放到if __name__ == '__main__':下:
如:
原因:
這是一個(gè)關(guān)于windows上多進(jìn)程實(shí)現(xiàn)的恩特。在windows上,子進(jìn)程會(huì)自動(dòng)import啟動(dòng)它的這個(gè)文件,而在import的時(shí)候是會(huì)自動(dòng)執(zhí)行這些語(yǔ)句的。如果不加__main__限制的化,就會(huì)無(wú)限遞歸創(chuàng)建子進(jìn)程,進(jìn)而報(bào)錯(cuò)。于是import的時(shí)候使用 name == “main” 保護(hù)起來(lái)就可以了。
參考文章1:python進(jìn)程池multiprocessing.Pool運(yùn)行錯(cuò)誤: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
總結(jié)
以上是生活随笔為你收集整理的python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: python 多进程 multiproc
- 下一篇: python 多进程multiproce