多重处理无法开始 [英] multiprocessing don't start
问题描述
我今天才开始学习多处理模块,我正在尝试这段代码,但是它没有用,而且我不知道为什么它不会给我任何错误,或者什么只会在不打印的情况下正常结束程序这里的任何事情都是我的简单代码:
i just started learning about multiprocessing module today and i was trying this code but it didn't work and i don't know why it doesn't give me any error or any thing it just end the program normally with out printing any thing here is my simple code :
import multiprocessing
def x ():
print ("hi")
example=multiprocessing.Process(target=x)
example.start()
它在我这样做时有效:
import multiprocessing
def x ():
print ("hi")
example=multiprocessing.Process(target=x())
但是那样对我没有用,因为我需要使用join()
和start()
but it will not be useful to me like that because i need to use the join()
and the start()
我正在使用python 3.5.2
and i am using python 3.5.2
推荐答案
嗯,您仍然可以使用start
和join
Umm, you can still use start
and join
import multiprocessing
def x ():
print ("hi")
example=multiprocessing.Process(target=x)
example.start() # START
example.join() # JOIN
在python repl中运行,我明白了,
Running in the python repl, I get,
>>> import multiprocessing
>>> def x ():
... print ("hi")
...
>>> example=multiprocessing.Process(target=x)
>>> example.start(); example.join()
hi
>>>
第二个示例似乎起作用的唯一原因是因为您正在调用x
(注意target=x()
与target=x
).
The only reason the second example seems to work is because you are calling x
(notice target=x()
vs target=x
).
您的第一个示例有效,但没有join
,该程序在main完成之后(但在子进程完成之前)终止.这会创建一个僵尸进程,并可能阻止它执行更多工作,例如将hi
打印到标准输出
Your first example works but without join
, the program is terminated after main is finished (but before the child process is finished). This creates a zombie process and possibly prevents it from doing more work, like printing hi
to standard output
这篇关于多重处理无法开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!