我是靠谱客的博主 负责云朵,最近开发中收集的这篇文章主要介绍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 multiprocessing


def worker(name, q):
    t = 0
    for i in range(10):
        print(name + " " + str(i))
        x = random.randint(1, 3)
        t += x
        time.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_yolov3pythonpython.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_yolov3pythonlibmultiprocessingspawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 114, in _main
    prepare(preparation_data)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:20191031_tensorflow_yolov3neededtesttest_Intel_realsensetest_191205_测试多进程Multiprocessing_queue.py", line 29, in <module>
    p.start()
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingprocess.py", line 105, in start
    self._popen = self._Popen(self)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingcontext.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingcontext.py", line 322, in _Popen
    return Popen(process_obj)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingpopen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 105, in spawn_main
    exitcode = _main(fd)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 114, in _main
    prepare(preparation_data)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "D:20191031_tensorflow_yolov3pythonlibrunpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:20191031_tensorflow_yolov3neededtesttest_Intel_realsensetest_191205_测试多进程Multiprocessing_queue.py", line 29, in <module>
    p.start()
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingprocess.py", line 105, in start
    self._popen = self._Popen(self)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingcontext.py", line 223, in _Popen
    return _default_context.get_context().Process._Popen(process_obj)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingcontext.py", line 322, in _Popen
    return Popen(process_obj)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingpopen_spawn_win32.py", line 33, in __init__
    prep_data = spawn.get_preparation_data(process_obj._name)
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 143, in get_preparation_data
    _check_not_importing_main()
  File "D:20191031_tensorflow_yolov3pythonlibmultiprocessingspawn.py", line 136, in _check_not_importing_main
    is not going to be frozen to produce an executable.''')
RuntimeError: 
        An attempt has been made to start a new process before the
        current process has finished its bootstrapping phase.

        This probably means that you are not using fork to start your
        child processes and you have forgotten to use the proper idiom
        in the main module:

            if __name__ == '__main__':
                freeze_support()
                ...

        The "freeze_support()" line can be omitted if the program
        is not going to be frozen to produce an executable.

解决方法是将要执行的代码放到if __name__ == '__main__':下:
如:

# -*- 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 multiprocessing


def worker(name, q):
    t = 0
    for i in range(10):
        print(name + " " + str(i))
        x = random.randint(1, 3)
        t += x
        time.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()

原因:
这是一个关于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 多进程 multiprocessing.Queue()报错:The "freeze_support()" line can be omitted if the program所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部