subprocess hang on - Sun, Jan 22, 2023
subprocess hang on
python调用子进程,发生锁住主进程的问题.
try:
process = subprocess.run(
f"{DOBF}",
input=program.encode('utf-8'),
shell=False,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
executable="/bin/bash",
timeout=20,
)
except subprocess.TimeoutExpired:
print(f"Error subprocess timeout \n")
process.kill()
process.wait()
return default_return
except Exception as e:
print(f"Error subprocess exception {e} \n")
return default_return
out = process.stdout.decode()
- 添加超时时间timeout
- 注意pipe管道是否会满
pipe管道默认使用系统缓存大小65535, stdin和stdout默认每次传输大小为65535, 如果当填满之后不清空就会导致进程hang on.