問題描述
了解 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()
調(diào)用,子進程將處于空閑狀態(tài)并且不會終止,成為必須手動殺死的僵尸".
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
的打印輸出來測試,據(jù)我所知,進程自行終止:
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 秒內(nèi):
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()
方法是否在此處連接任何內(nèi)容?它是否將一個過程與它的結(jié)束連接起來?或者它只是與 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()
方法,當(dāng)與 threading
或 multiprocessing
一起使用時, 與 str.join()
無關(guān) - 它實際上并沒有將任何東西連接在一起.相反,它只是意味著等待這個[線程/進程]完成".使用名稱 join
是因為 multiprocessing
模塊的 API 看起來類似于 threading
模塊的 API,而 threading
模塊使用 join
作為它的 Thread
對象.使用術(shù)語 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.
現(xiàn)在,無論是否調(diào)用 join()
,您都會看到 20 秒延遲的原因是因為默認(rèn)情況下,當(dāng)主進程準(zhǔn)備退出時,它會隱式調(diào)用 join()
在所有正在運行的 multiprocessing.Process
實例上.這在 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:
還請記住,非守護進程將自動加入.
Remember also that non-daemonic processes will be automatically be joined.
您可以通過在啟動進程之前將 Process
上的 daemon
標(biāo)志設(shè)置為 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:
守護進程
進程的守護進程標(biāo)志,一個布爾值.這必須在之前設(shè)置start() 被調(diào)用.
The process’s daemon flag, a Boolean value. This must be set before start() is called.
初始值繼承自創(chuàng)建過程.
The initial value is inherited from the creating process.
當(dāng)一個進程退出時,它會嘗試終止它的所有守護進程子進程.
這篇關(guān)于Python 多處理模塊的 .join() 方法到底在做什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!