首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >由于某些原因,python多处理代码无法工作。

由于某些原因,python多处理代码无法工作。
EN

Stack Overflow用户
提问于 2022-03-10 17:21:17
回答 2查看 501关注 0票数 0

因此,我刚刚从Python多处理开始。我试过这个例子,但没有成功:

代码语言:javascript
运行
复制
import multiprocessing

def function():
    time.sleep(1)
    print("slept once")

p1 = multiprocessing.Process(target=function)
p2 = multiprocessing.Process(target=function)

p1.start()
p2.start()

它的产出应该是:

代码语言:javascript
运行
复制
(sleeping 1 second)
slept once
slept once

但它却给了我一个错误:

代码语言:javascript
运行
复制
Traceback (most recent call last):
 File "<string>", line 1, in <module>
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
   exitcode = _main(fd)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 114, in _main
   prepare(preparation_data)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 225, in prepare
   _fixup_main_from_path(data['init_main_from_path'])
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
   run_name="__mp_main__")
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 263, in run_path
   pkg_name=pkg_name, script_name=fname)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 96, in _run_module_code
   mod_name, mod_spec, pkg_name, script_name)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
   exec(code, run_globals)
 File "C:\Users\krist\PycharmProjects\chat_app\client_1.py", line 11, in <module>
   p1.start()
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 105, in start
   self._popen = self._Popen(self)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 223, in _Popen
   return _default_context.get_context().Process._Popen(process_obj)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 322, in _Popen
   return Popen(process_obj)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
   prep_data = spawn.get_preparation_data(process_obj._name)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
   _check_not_importing_main()
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.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 "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 105, in spawn_main
   exitcode = _main(fd)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 114, in _main
   prepare(preparation_data)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 225, in prepare
   _fixup_main_from_path(data['init_main_from_path'])
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
   run_name="__mp_main__")
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 263, in run_path
   pkg_name=pkg_name, script_name=fname)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 96, in _run_module_code
   mod_name, mod_spec, pkg_name, script_name)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 85, in _run_code
   exec(code, run_globals)
 File "C:\Users\krist\PycharmProjects\chat_app\client_1.py", line 11, in <module>
   p1.start()
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 105, in start
   self._popen = self._Popen(self)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 223, in _Popen
   return _default_context.get_context().Process._Popen(process_obj)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\context.py", line 322, in _Popen
   return Popen(process_obj)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\popen_spawn_win32.py", line 33, in __init__
   prep_data = spawn.get_preparation_data(process_obj._name)
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.py", line 143, in get_preparation_data
   _check_not_importing_main()
 File "C:\Users\krist\AppData\Local\Programs\Python\Python36\lib\multiprocessing\spawn.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.

我使用的是windows 11,python 3.6

希望有人能帮上忙!!能帮助!将被改造成一种专用的产品,特别是一种专用的产品,一种很好的方法,一种很好的方法,希望有人能帮助!希望有人能帮上忙!能帮助!

这里只是一堆随机的东西,因为堆栈溢出不允许我发布这个,否则:

资料:1,myfile;1:0_sub_.error:help.error:help.data:1,myfile;1:0_sub_error:help.data:1,myfile;1:0_sub_error:help.data:1,myfile;1:0_sub_error:help.data:1,myfile;1:0_sub_;;rm.0000;ip/ip.错误:data.数据:1,myfile;1:0_sub_

EN

回答 2

Stack Overflow用户

发布于 2022-03-10 17:32:11

除非遵循多处理库的,否则不能启动新进程。

这个库中有一个特定的指南,说明start()方法应该只在if __name__ == '__main__条件中运行。

为什么?因为在文件直接运行而不是导入时生成新进程更安全。

所以,这应该能起作用:

代码语言:javascript
运行
复制
import multiprocessing
import time


def function():
    time.sleep(1)
    print("slept once")

p1 = multiprocessing.Process(target=function)
p2 = multiprocessing.Process(target=function)

if __name__ == '__main__':
    p1.start()
    p2.start()

如何使用MultiProcessing库?https://docs.python.org/3/library/multiprocessing.html

1

票数 0
EN

Stack Overflow用户

发布于 2022-03-10 17:42:06

如果添加预期的成语,代码运行良好:

代码语言:javascript
运行
复制
import time
import multiprocessing

def function():
    time.sleep(1)
    print("slept once")

if __name__ == '__main__':
   p1 = multiprocessing.Process(target=function)
   p2 = multiprocessing.Process(target=function)

   p1.start()
   p2.start()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71428275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档