我记得很久以前,有人问过Windows在这里有负PID,我再也找不到这个问题了。在所有情况下,我都看到了学习Python第4版的一些代码,其中包含负面流程ID,所以让我们先看看代码:
#spawnv.py
import os, sys
for i in range(10):
if sys.platform[:3] == 'win':
pypath = sys.executable
os.spawnv(os.P_NOWAIT, pypath, ('python', 'child.py', str(i)))
else:
pid = os.fork()
if pid != 0:
print('Process %d spawned' % pid)
else:
os.execlp('python3', 'python3', 'child.py', str(i))
print('Main process exiting.')
#child.py
import os, sys
print('Hello from child', os.getpid(), sys.argv[1])
书中提到的输出:
Hello from child −583587 0
Hello from child −558199 2
Hello from child −586755 1
Hello from child −562171 3
Main process exiting.
Hello from child −581867 6
Hello from child −588651 5
Hello from child −568247 4
Hello from child −563527 7
Hello from child −543163 9
Hello from child −587083 8
我在Windows 7上运行完全相同的代码,所有生成的进程都有正PID。将P_NOWAIT
标志用于spawnv
的事实使其更加令人费解。 P_WAIT
可以返回否定状态代码:-SIGNAL
。
这是一个错字还是Windows可以有负PID?