1

我在一本书 Programming Python 中看到了这些代码:

import os

parm = 0
while True:
    parm += 1
    pid = os.fork()
    if pid == 0:                                             # copy process
        os.execlp('python', 'python', 'child.py', str(parm)) # overlay program
        assert False, 'error starting program'               # shouldn't return
    else:
        print('Child is', pid)
        if input() == 'q': break

为什么里面有两条蟒蛇os.execlp?似乎第二个可以是任何字符串。第二个参数有什么作用?

编辑:阅读可能重复的问题后,我仍然不明白。这是 child.py:

import os, sys
print('Hello from child', os.getpid(), sys.argv[1])

所以,argv[1]是child.py,不是吗?str(parm)argv[0]

4

2 回答 2

0

It's its name, that's why it can be any string.

See http://docs.python.org/3/library/os.html?highlight=os.exec#process-management

The document is not structured well, people could have ignored the second paragraph simply because they jump straight to os.exec* thinking contents above are not relevant.

于 2013-08-31T19:42:43.083 回答
0

第一个python是要运行的可执行文件的名称,第二个是 的值argv[0]

于 2013-08-31T18:33:20.937 回答