如何使用python 2.7.6进行subprocess.call超时?

时间:2014-04-25 08:13:15

标签: python python-2.7 timeout subprocess

可能有人问过,但是在使用python 2.7时我找不到任何关于subprocess.call超时的信息

5 个答案:

答案 0 :(得分:9)

我总是使用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

如果设置task.poll(),则超时时间为10分钟。虽然time.sleep(delay)将查询进程是否已终止。在这种情况下,subprocess.poll()将休眠1秒钟,然后将超时减少1秒。你可以根据自己的内容来玩这个部分,但基本概念始终如一。

希望这有帮助!

let urlStr: String = "yelp://" let url = URL(string: urlStr) if UIApplication.shared.canOpenURL(url!) { print("can open") if #available(iOS 10.0, *) { print("10") UIApplication.shared.open(url!, options: [:], completionHandler: nil) } else { print("NOT 10") UIApplication.shared.openURL(url!) } } else { // Can not open URL print("can not open url") } https://docs.python.org/2/library/subprocess.html#popen-objects

答案 1 :(得分:3)

您可以安装instructions subprocess32 module - 来自Python 3.2 / 3.3的subprocess模块的后端口,以便在2.x上使用。它适用于Python 2.7,它包括Python 3.3的超时支持。

mentioned by @gps因此要在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()

如果子进程可能会更快结束,那么便携式解决方案将subprocess.call() is just Popen().wait()

#!/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上,你可以use Timer() as suggested in @sussudio's answer

#!/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模块使用use SIGALRM as suggested in @Alex Martelli's answerbusy loop with waitpid(WNOHANG) calls on Unix

答案 2 :(得分:1)

您可以尝试使用" easyprocess"

https://github.com/ponty/EasyProcess

它有许多功能,例如"超时"

答案 3 :(得分:0)

您可以使用subprocess32 mentioned by @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,这表示永不超时。

答案 4 :(得分:-2)