我对 subprocess.popen 有疑问。调用方法 ".poll()" 后创建的进程返回代码 "3221226505"
根据任务我需要运行多个 subprocess.popen 代码不会抛出异常。
if __name__ == "__main__":
random_values = []
processes = {}
for i in range(10):
random_value = random.randint(0, 100000)
process = subprocess.Popen(["python3", "test.py"],
env={"random_value": str(random_value)})
random_values.append(random_value)
processes[random_value] = process
while True:
for random_value in random_values:
status_code = processes[random_value].poll()
if status_code is not None:
print(status_code)
processes[random_value] = subprocess.Popen(["python3", "test.py"],
env={"random_value": str(random_value)})
elif random_value not in processes:
processes[random_value] = subprocess.Popen(["python3", "test.py"],
env={"random_value": str(random_value)})
if processes[random_value].stdout:
print(processes[random_value].stdout.readlines())
time.sleep(1)
测试.py
if __name__ == "__main__":
random_value= os.getenv("random_value")
sys.stdout.write(random_value)
sys.exit(0)