首页 > 解决方案 > Python subprocess.Popen 没有给我正确的 pid?

问题描述

看起来 job=subprocess.Popen, job.pid 没有给出正确的 PID,而是 PID+1 似乎工作

我检查了线程: Python subprocess.popen wrong pid python - subprocess.Popen().pid return the pid of the parent script

但他们都没有真正回答我的问题。

我的工作(据说'PID+1'的成功率是99%,我还没有失败,但感觉有可能)代码如下:

def Running(PID):
    try:
        os.kill(PID, 0)
    except OSError:
        return False
    else:
        return True

job=subprocess.Popen('cfl3d_seq<cfl3d.inp &', shell=True)
Runs.append(int(job.pid)+1)
print(int(job.pid)+1)
print(Runs)
time.sleep(10)

while len(Runs)>=TotalRun:
    for run in Runs:
        if Running(run)==False:
            print('kill ', run)
            Runs.remove(run)
    time.sleep(60)

这是在后台运行外部软件。有没有更好的清洁方法来获得正确的 PID?

如果它本身正在运行python代码,有没有更好的方法来做到这一点?

我在使用 time.sleep() 时没有使用任何 fork 线程,这会使外部 cfl3d 软件休眠吗?

如果通过 Popen() 执行的是 python 代码,这会使弹出的 python 休眠吗?

非常感谢!

标签: pythonsubprocess

解决方案


推荐阅读