【发布时间】:2011-04-13 14:14:45
【问题描述】:
是否有任何参数或选项可以为 Python 的 subprocess.Popen 方法设置超时?
类似这样的:
subprocess.Popen(['..'], ..., timeout=20)?
【问题讨论】:
标签: python timeout pipe subprocess popen
是否有任何参数或选项可以为 Python 的 subprocess.Popen 方法设置超时?
类似这样的:
subprocess.Popen(['..'], ..., timeout=20)?
【问题讨论】:
标签: python timeout pipe subprocess popen
我建议查看threading 模块中的Timer class。我用它来实现Popen 的超时。
首先,创建一个回调:
def timeout( p ):
if p.poll() is None:
print 'Error: process taking too long to complete--terminating'
p.kill()
然后打开进程:
proc = Popen( ... )
然后创建一个将调用回调的计时器,将进程传递给它。
t = threading.Timer( 10.0, timeout, [proc] )
t.start()
t.join()
在程序后面的某个地方,您可能想要添加以下行:
t.cancel()
否则,python 程序将一直运行,直到计时器完成运行。
编辑:有人告诉我,subprocess p 可能会在 p.poll() 和 p.kill() 调用之间终止。我相信下面的代码可以解决这个问题:
import errno
def timeout( p ):
if p.poll() is None:
try:
p.kill()
print 'Error: process taking too long to complete--terminating'
except OSError as e:
if e.errno != errno.ESRCH:
raise
虽然您可能希望清理异常处理以专门处理子进程已正常终止时发生的特定异常。
【讨论】:
print 'Error: process taking too long to complete--terminating' 可以运行,即使它是一个谎言并且你的进程在你没有杀死它的情况下终止(因为它在你的 poll 和 kill 调用之间的时刻执行它)。
if p.poll() is None 比使用等式关系 (==) 更好
subprocess.Popen 不会阻塞,因此您可以执行以下操作:
import time
p = subprocess.Popen(['...'])
time.sleep(20)
if p.poll() is None:
p.kill()
print 'timed out'
else:
print p.communicate()
它的缺点是您必须始终等待至少 20 秒才能完成。
【讨论】:
Popen/sleep() 是一种简单的可移植且高效的方法,可以在超时的情况下运行外部永无止境的程序。虽然 p.communicate() 在这段代码中的使用是有问题的(它可能会导致丢失一些输出)。当Popen/sleep() 有用时,请参阅Stop reading process output in Python without hang? 问题。
wait = 0 while p.poll() is None and wait < 15: wait += 1 sleep(1) if p.poll() is None: p.kill()
import subprocess, threading
class Command(object):
def __init__(self, cmd):
self.cmd = cmd
self.process = None
def run(self, timeout):
def target():
print 'Thread started'
self.process = subprocess.Popen(self.cmd, shell=True)
self.process.communicate()
print 'Thread finished'
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print 'Terminating process'
self.process.terminate()
thread.join()
print self.process.returncode
command = Command("echo 'Process started'; sleep 2; echo 'Process finished'")
command.run(timeout=3)
command.run(timeout=1)
这个输出应该是:
Thread started
Process started
Process finished
Thread finished
0
Thread started
Process started
Terminating process
Thread finished
-15
可以看出,在第一次执行中,进程正确完成(返回代码 0),而在第二次执行中,进程被终止(返回代码 -15)。
我没有在 Windows 中测试过;但是,除了更新示例命令之外,我认为它应该可以工作,因为我在文档中没有找到任何说明 thread.join 或 process.terminate 不受支持的内容。
【讨论】:
你可以这样做
from twisted.internet import reactor, protocol, error, defer
class DyingProcessProtocol(protocol.ProcessProtocol):
def __init__(self, timeout):
self.timeout = timeout
def connectionMade(self):
@defer.inlineCallbacks
def killIfAlive():
try:
yield self.transport.signalProcess('KILL')
except error.ProcessExitedAlready:
pass
d = reactor.callLater(self.timeout, killIfAlive)
reactor.spawnProcess(DyingProcessProtocol(20), ...)
使用 Twisted 的异步进程 API。
【讨论】:
没有内置 Python 子进程自动超时功能,因此您必须自己构建。
这适用于我在运行 python 2.7.3 的 Ubuntu 12.10 上
把它放在一个名为 test.py 的文件中
#!/usr/bin/python
import subprocess
import threading
class RunMyCmd(threading.Thread):
def __init__(self, cmd, timeout):
threading.Thread.__init__(self)
self.cmd = cmd
self.timeout = timeout
def run(self):
self.p = subprocess.Popen(self.cmd)
self.p.wait()
def run_the_process(self):
self.start()
self.join(self.timeout)
if self.is_alive():
self.p.terminate() #if your process needs a kill -9 to make
#it go away, use self.p.kill() here instead.
self.join()
RunMyCmd(["sleep", "20"], 3).run_the_process()
保存并运行它:
python test.py
sleep 20 命令需要 20 秒才能完成。如果它没有在 3 秒内终止(它不会),则该进程终止。
el@apollo:~$ python test.py
el@apollo:~$
从进程运行到终止之间有 3 秒的时间。
【讨论】:
很遗憾,没有这样的解决方案。我设法使用线程计时器来执行此操作,该计时器将与在超时后将其终止的进程一起启动,但由于僵尸进程或诸如此类的原因,我确实遇到了一些陈旧的文件描述符问题。
【讨论】:
不,没有超时。我想,您正在寻找的是在一段时间后杀死子进程。既然您能够向子进程发出信号,那么您也应该能够杀死它。
向子进程发送信号的通用方法:
proc = subprocess.Popen([command])
time.sleep(1)
print 'signaling child'
sys.stdout.flush()
os.kill(proc.pid, signal.SIGUSR1)
您可以使用此机制在超时后终止。
【讨论】:
从 Python 3.3 开始,子进程模块中的阻塞辅助函数还有一个 timeout 参数。
【讨论】:
是的,https://pypi.python.org/pypi/python-subprocess2 将为 Popen 模块扩展两个附加功能,
Popen.waitUpTo(timeout=seconds)
这将等待特定秒数以完成该过程,否则返回无
还有,
Popen.waitOrTerminate
这将等待一个点,然后调用 .terminate(),然后调用 .kill(),一个或另一个或两者的某种组合,请参阅文档以获取完整详细信息:
【讨论】:
对于 Linux,您可以使用信号。这取决于平台,因此 Windows 需要另一种解决方案。不过它可能适用于 Mac。
def launch_cmd(cmd, timeout=0):
'''Launch an external command
It launchs the program redirecting the program's STDIO
to a communication pipe, and appends those responses to
a list. Waits for the program to exit, then returns the
ouput lines.
Args:
cmd: command Line of the external program to launch
time: time to wait for the command to complete, 0 for indefinitely
Returns:
A list of the response lines from the program
'''
import subprocess
import signal
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
lines = []
if not launch_cmd.init:
launch_cmd.init = True
signal.signal(signal.SIGALRM, alarm_handler)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
signal.alarm(timeout) # timeout sec
try:
for line in p.stdout:
lines.append(line.rstrip())
p.wait()
signal.alarm(0) # disable alarm
except:
print "launch_cmd taking too long!"
p.kill()
return lines
launch_cmd.init = False
【讨论】: