python - 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 休眠吗?
非常感谢!
解决方案
推荐阅读
- amazon-ec2 - 如何从 terraform 中的 EC2 实例列表中提取 ID 以在 ALB 中使用?
- azure - 将 X-FD-HealthProbe 与应用程序网关一起使用
- permissions - Lightning 不允许权限策略
- python - 分别提取每个类的预测概率
- html - 如何在 EPUB 文件中的每个句子或文本行的末尾插入特定的 HTML 标记?
- android-studio - Android Studio shell 脚本构建配置独立运行,但不作为 Windows 上的前置步骤
- javascript - 身份验证的firebase问题
- node.js - 无论 cookie 在数组中的位置如何,以相同的顺序从 cookie 数组中写入 cookie 字符串
- python - 我究竟做错了什么?我制作了一个爆炸我的水泥块精灵的动画,它可以工作,但问题是动画没有通过
- python - 为什么使用相同的 Keras 模型和输入进行预测时会得到不同的结果?