0
我需要得到真實進程ID 在產生的孩子正如我的例子中的父級看到的。現在os.getpgid(0)只是返回父進程ID,因爲它的繼承是正確的。但是在父進程中,子進程ID是不同的。這只是一個簡單的例子,但我會在另一個程序中實現它:Python衍生的子進程ID
#!/usr/bin/python3
import os
mypid = os.getpgid(0)
print ("My pid is %s" % mypid)
childpid = os.spawnv(os.P_NOWAIT, "./spawn2.py", ["foo", "bar"])
print ("I forked to spawn2 with pid %s" % childpid)
#!/usr/bin/python3
import os
pid = os.getpgid(0)
print ("I'm spawn2")
print ("My pid is %s" % pid)
輸出看起來是這樣,但我希望催生孩子說,它的PID是1658:
[email protected]:~/git-repos/zguide/examples/Python $ ./spawn1.py
My pid is 1657
I forked to spawn2 with pid 1658
[email protected]:~/git-repos/zguide/examples/Python $ I'm spawn2
My pid is 1657
你是否故意使用getp ** g ** id()? – Jasper
沒有我所知道的。我已經懷疑可能還有其他我想用的東西,但我還沒有發現它。你是否暗示有別的東西?我會再次檢查os文檔,也許我錯過了它。 – dpetican