python启动进程并获取pid 您所在的位置:网站首页 python打开某个程序 python启动进程并获取pid

python启动进程并获取pid

2023-07-18 02:05| 来源: 网络整理| 查看: 265

我正在研究一个漂亮的小功能:

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

问题是process.pid不是正确的PID.它似乎总是比正确的PID低1.例如,它表示该过程始于31729,但ps表示它正在31730运行.每次我尝试将其关闭1.我猜它返回的PID是当前进程的PID,而不是已启动的PID ,并且新进程获得的“下一个”pid高出1.如果是这种情况,我不能仅仅依赖于返回process.pid 1,因为我无法保证它始终是正确的.

为什么process.pid不返回新进程的PID,我怎样才能实现我之后的行为?



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有