【发布时间】:2018-12-08 17:35:33
【问题描述】:
我需要运行可能会产生其他进程的进程(程序、bash 脚本等),但我还需要应用超时以防某些事情挂起,并且我希望程序在超时到期后继续执行。我正在使用以下 run() 函数:
import subprocess, shlex, threading
from threading import Timer
def run(cmd, timeout_sec=5):
def kill_proc(proc, timedout):
timedout['value'] = True
proc.kill()
try:
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
timedout = {'value': False}
timer = threading.Timer(timeout_sec, kill_proc, [proc, timedout])
timer.start()
stdout, stderr = proc.communicate()
timer.cancel()
return stdout.decode('utf-8'), stderr.decode('utf-8'), proc.returncode, timedout['value']
except Exception as e:
print('Could not execute: %s - with error: %s' % (cmd, e))
return '', '', 1, False
在大多数情况下,它能够正常地超时调用,例如:
run(cmd='sleep 1m', timeout_sec=2)
它似乎挂断的地方是假设我有以下名为 sleeper.sh 的 bash 脚本:
#!/bin/bash
sleep 1m
如果我发出以下命令:
run(cmd='sleeper.sh', timeout_sec=2)
它将坐在那里等待 1 分钟,直到命令完成。有没有办法让我重写我的 run() 函数以强制使进程超时,而不管产生了什么子进程?
【问题讨论】:
标签: python bash timeout subprocess popen