Hub*_*bro 29 python subprocess popen
我正在研究一个漂亮的小功能:
def startProcess(name, path):
"""
Starts a process in the background and writes a PID file
returns integer: pid
"""
# Check if the process is already running
status, pid = processStatus(name)
if status == RUNNING:
raise AlreadyStartedError(pid)
# Start process
process = subprocess.Popen(path + ' > /dev/null 2> /dev/null &', shell=True)
# Write PID file
pidfilename = os.path.join(PIDPATH, name + '.pid')
pidfile = open(pidfilename, 'w')
pidfile.write(str(process.pid))
pidfile.close()
return process.pid
Run Code Online (Sandbox Code Playgroud)
问题是这process.pid不是正确的PID.它似乎总是比正确的PID低1.例如,它表示该过程始于31729,但ps表示它正在31730运行.每次我尝试将其关闭1.我猜它返回的PID是当前进程的PID ,而不是已启动的PID ,并且新进程获得的"下一个"pid高出1.如果是这种情况,我不能仅仅依靠返回,process.pid + 1因为我不能保证它总是正确的.
为什么不process.pid返回新进程的PID,我怎样才能实现我追求的行为?
kro*_*sey 30
来自http://docs.python.org/library/subprocess.html上的文档:
Popen.pid子进程的进程ID.
请注意,如果将shell参数设置为True,则这是生成的shell的进程ID.
如果shell是假的,它应该按照你的预期行事,我想.
| 归档时间: |
|
| 查看次数: |
65163 次 |
| 最近记录: |