日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program

發(fā)布時(shí)間:2025/3/19 python 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 多进程 multiprocessing.Queue()报错:The freeze_support() line can be omitted if the program 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

運(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__':下:
如:

# -*- 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)def queue_test():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)if __name__ == '__main__':queue_test()

原因:
這是一個(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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。