试图了解python中的main的多处理 [英] Trying to understand Multiprocessing with main in python
问题描述
使用下面的代码,我得到奇怪的输出:
Using the code below I am getting strange output:
import sys
from multiprocessing import Process
import time
from time import strftime
now =time.time()
print time.strftime("%Y%m%d %H:%M:%S", time.localtime(now))
fr= [1,2,3]
for row in fr:
print 3
print 1
def worker():
print 'worker line'
time.sleep(1)
sys.exit(1)
def main():
print 'start worker'
Process(target=worker, args=()).start()
print 'main line'
if __name__ == "__main__":
start_time = time.time()
main()
end_time = time.time()
duration = end_time - start_time
print "Duration: %s" % duration
输出为:
20120324 20:35:53
3
3
3
1
start worker
main line
Duration: 0.0
20120324 20:35:53
3
3
3
1
worker line
我当时想我会得到这个:
I was thinking I would get this:
20120324 20:35:53
3
3
3
1
start worker
worker line
main line
Duration: 1.0
为什么要运行两次?在WinX64上使用python 2.7:
Why is this run twice? Using python 2.7 on WinX64 :
20120324 20:35:53
3
3
3
1
worker line
推荐答案
问题基本上是因为multiprocessing
确实设计为在posix系统上运行,该系统具有
the problem is basically because multiprocessing
is really designed to run on a posix system, one with the fork(2)
syscall. on those operating systems, the process can split into two, the child magically cloning the state from the parent, and both resume running in the same place, with the child now having a new process ID. In that situation, multiprocessing
can arrange for some mechanism to ship state from parent to child as needed, with the certainty the child will already have most of the needed python state.
Windows没有fork()
.
Windows does not have fork()
.
因此,multiprocessing
必须采取措施.这基本上涉及启动运行多处理子脚本的全新python解释器.几乎立即,父级会要求孩子使用处于父级状态的东西,因此,孩子必须从头开始通过将脚本导入到子级中来重新创建该状态.
And so multiprocessing
has to pick up the slack. This basically involves launching a brand new python interpreter running a multiprocessing child script. Almost immediately, the parent will ask the child to use something that is in the parent's state, and so the child will have to recreate that state from scratch, by importing your script into the child.
因此,在脚本导入时发生的任何事情都会发生两次,一次发生在父进程中,再一次发生在子进程中,因为它会重新创建服务于父进程的python环境.
So anything that happens at import time in your script, will happen twice, once in the parent, and again in the child as it recreates the python environment needed to serve the parent.
这篇关于试图了解python中的main的多处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!