问题描述
了解Python 多重处理(来自 PMOTW文章),并且希望澄清join()
方法的作用.
Learning about Python Multiprocessing (from a PMOTW article) and would love some clarification on what exactly the join()
method is doing.
在 2008年的旧教程中指出:在下面的代码中没有p.join()
调用,子进程将处于空闲状态而不会终止,成为必须手动杀死的僵尸".
In an old tutorial from 2008 it states that without the p.join()
call in the code below, "the child process will sit idle and not terminate, becoming a zombie you must manually kill".
from multiprocessing import Process
def say_hello(name='world'):
print "Hello, %s" % name
p = Process(target=say_hello)
p.start()
p.join()
我添加了PID
和time.sleep
的打印输出进行测试,据我所知,该过程自行终止:
I added a printout of the PID
as well as a time.sleep
to test and as far as I can tell, the process terminates on its own:
from multiprocessing import Process
import sys
import time
def say_hello(name='world'):
print "Hello, %s" % name
print 'Starting:', p.name, p.pid
sys.stdout.flush()
print 'Exiting :', p.name, p.pid
sys.stdout.flush()
time.sleep(20)
p = Process(target=say_hello)
p.start()
# no p.join()
在20秒内:
936 ttys000 0:00.05 /Library/Frameworks/Python.framework/Versions/2.7/Reso
938 ttys000 0:00.00 /Library/Frameworks/Python.framework/Versions/2.7/Reso
947 ttys001 0:00.13 -bash
20秒后:
947 ttys001 0:00.13 -bash
行为与文件末尾添加的p.join()
相同.本周Python模块提供了对该模块的可读性很强的解释; 要等到进程完成工作并退出后,请使用join()方法.",但看来至少OS X仍在这样做.
Behavior is the same with p.join()
added back at end of the file. Python Module of the Week offers a very readable explanation of the module; "To wait until a process has completed its work and exited, use the join() method.", but it seems like at least OS X was doing that anyway.
我也想知道方法的名称. .join()
方法在这里串联了什么吗?它是在连接过程的末尾吗?还是只是与Python的本机.join()
方法共享一个名称?
Am also wondering about the name of the method. Is the .join()
method concatenating anything here? Is it concatenating a process with it's end? Or does it just share a name with Python's native .join()
method?
推荐答案
join()
方法与threading
或multiprocessing
一起使用时,与str.join()
无关-实际上没有将任何东西串联在一起.相反,它仅表示等待此[线程/进程]完成".之所以使用名称join
,是因为multiprocessing
模块的API看起来与threading
模块的API相似,并且threading
模块将join
用于其Thread
对象.在许多编程语言中,使用术语join
表示等待线程完成"是很常见的,因此Python也采用了它.
The join()
method, when used with threading
or multiprocessing
, is not related to str.join()
- it's not actually concatenating anything together. Rather, it just means "wait for this [thread/process] to complete". The name join
is used because the multiprocessing
module's API is meant to look as similar to the threading
module's API, and the threading
module uses join
for its Thread
object. Using the term join
to mean "wait for a thread to complete" is common across many programming languages, so Python just adopted it as well.
现在,无论是否调用join()
都看到20秒的延迟,这是因为默认情况下,当主进程准备退出时,它将在所有正在运行的multiprocessing.Process
上隐式调用join()
实例.在multiprocessing
文档中并未对此做出应有的明确说明,但在部分:
Now, the reason you see the 20 second delay both with and without the call to join()
is because by default, when the main process is ready to exit, it will implicitly call join()
on all running multiprocessing.Process
instances. This isn't as clearly stated in the multiprocessing
docs as it should be, but it is mentioned in the Programming Guidelines section:
您可以通过在开始该过程之前将Process
上的daemon
标志设置为True
来覆盖此行为:
You can override this behavior by setting the daemon
flag on the Process
to True
prior to starting the process:
p = Process(target=say_hello)
p.daemon = True
p.start()
# Both parent and child will exit here, since the main process has completed.
如果您这样做,则子进程主进程将尽快终止完成:
If you do that, the child process will be terminated as soon as the main process completes:
进程的守护程序标志,一个布尔值.必须在此之前设置 start()被调用.
The process’s daemon flag, a Boolean value. This must be set before start() is called.
初始值是从创建过程继承的.
The initial value is inherited from the creating process.
进程退出时,它将尝试终止其所有守护进程 子进程.
When a process exits, it attempts to terminate all of its daemonic child processes.
这篇关于Python多重处理模块的.join()方法到底在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!