[英]How to make a subprocess.call timeout using python 2.7.6?
可能有人问过,但是在使用python 2.7时我找不到任何关于subprocess.call超时的信息
我总是使用2.7进行超时的一种简单方法是使用subprocess.poll()
和time.sleep()
以及延迟。 这是一个非常基本的例子:
import subprocess
import time
x = #some amount of seconds
delay = 1.0
timeout = int(x / delay)
args = #a string or array of arguments
task = subprocess.Popen(args)
#while the process is still executing and we haven't timed-out yet
while task.poll() is None and timeout > 0:
#do other things too if necessary e.g. print, check resources, etc.
time.sleep(delay)
timeout -= delay
如果设置x = 600
,则超时将达到10分钟。 而task.poll()
将查询进程是否已终止。 在这种情况下, time.sleep(delay)
将休眠1秒,然后将超时减少1秒。 你可以根据自己的内容来玩这个部分,但基本概念始终如一。
希望这可以帮助!
subprocess.poll()
https://docs.python.org/2/library/subprocess.html#popen-objects
你可以安装subprocess32
模块 由@gps提到的的反向移植- subprocess
在Python 3.2 / 3.3模块上使用2.x的 它适用于Python 2.7,它包括Python 3.3的超时支持。
subprocess.call()
只是Popen().wait()
,因此在timeout
秒内中断一个长时间运行的进程:
#!/usr/bin/env python
import time
from subprocess import Popen
p = Popen(*call_args)
time.sleep(timeout)
try:
p.kill()
except OSError:
pass # ignore
p.wait()
如果子进程可能会更快结束,那么便携式解决方案是使用@ sussudio的答案中建议的Timer()
:
#!/usr/bin/env python
from subprocess import Popen
from threading import Timer
def kill(p):
try:
p.kill()
except OSError:
pass # ignore
p = Popen(*call_args)
t = Timer(timeout, kill, [p])
t.start()
p.wait()
t.cancel()
在Unix上,您可以按照@Alex Martelli的回答中的建议使用SIGALRM
:
#!/usr/bin/env python
import signal
from subprocess import Popen
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal.signal(signal.SIGALRM, alarm_handler)
p = Popen(*call_args)
signal.alarm(timeout) # raise Alarm in 5 minutes
try:
p.wait()
signal.alarm(0) # reset the alarm
except Alarm:
p.kill()
p.wait()
为了避免在这里使用线程和信号,Python 3上的subprocess
模块使用一个繁忙的循环,在Unix上使用waitpid(WNOHANG)
调用, 在Windows 上使用 winapi.WaitForSingleObject()
。
您可以使用subprocess32 通过@gps提到的 ,这是在Python 3.2的子标准库模块的反向移植- 3.5上使用的Python 2。
首先,安装subprocess32模块:
pip install subprocess32
这是一段代码片段:
>>> import subprocess32
>>> print subprocess32.check_output(["python", "--version"])
Python 2.7.12
>>> subprocess32.check_output(["sleep", "infinity"], timeout=3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/subprocess32.py", line 340, in check_output
raise TimeoutExpired(process.args, timeout, output=output)
subprocess32.TimeoutExpired: Command '['sleep', 'infinity']' timed out after 3 seconds
注意,默认timeout=None
,这意味着永不超时。
在python 3.3中添加了超时参数。
https://docs.python.org/3/library/subprocess.html#subprocess.call
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.