Skip to content Skip to sidebar Skip to footer

How To Make A Subprocess.call Timeout Using Python 2.7.6?

This has probably been asked but I cannot find anything regarding a subprocess.call timeout when using python 2.7

Solution 1:

A simple way I've always done timeouts with 2.7 is utilizing subprocess.poll() alongside time.sleep() with a delay. Here's a very basic example:

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 yetwhile task.poll() isNoneand timeout > 0:
     #do other things too if necessary e.g. print, check resources, etc.
     time.sleep(delay)
     timeout -= delay

If you set x = 600, then your timeout would amount to 10 minutes. While task.poll() will query whether or not the process has terminated. time.sleep(delay) will sleep for 1 second in this case, and then decrement the timeout by 1 second. You can play around with that part to your heart's content, but the basic concept is the same throughout.

Hope this helps!

subprocess.poll()https://docs.python.org/2/library/subprocess.html#popen-objects

Solution 2:

You could install subprocess32 modulementioned by @gps -- the backport of the subprocess module from Python 3.2/3.3 for use on 2.x. It works on Python 2.7 and it includes timeout support from Python 3.3.

subprocess.call() is just Popen().wait() and therefore to interrupt a long running process in timeout seconds:

#!/usr/bin/env pythonimport time
from subprocess import Popen

p = Popen(*call_args)
time.sleep(timeout)
try:
    p.kill()
except OSError:
    pass# ignore
p.wait()

If the child process may end sooner then a portable solution is to use Timer() as suggested in @sussudio's answer:

#!/usr/bin/env pythonfrom subprocess import Popen
from threading import Timer

defkill(p):
    try:
        p.kill()
    except OSError:
        pass# ignore

p = Popen(*call_args)
t = Timer(timeout, kill, [p])
t.start()
p.wait()
t.cancel()

On Unix, you could use SIGALRM as suggested in @Alex Martelli's answer:

#!/usr/bin/env pythonimport signal
from subprocess import Popen

classAlarm(Exception):
    passdefalarm_handler(signum, frame):
    raise Alarm

signal.signal(signal.SIGALRM, alarm_handler)


p = Popen(*call_args)
signal.alarm(timeout)  # raise Alarm in 5 minutestry:
    p.wait()
    signal.alarm(0)  # reset the alarmexcept Alarm:
    p.kill()
    p.wait()

To avoid using threads and signals here, subprocess module on Python 3 uses a busy loop with waitpid(WNOHANG) calls on Unix and winapi.WaitForSingleObject() on Windows.

Solution 3:

Solution 4:

You can use subprocess32mentioned by @gps, which is backport of the subprocess standard library module from Python 3.2 - 3.5 for use on Python 2.

Firstly, install the subprocess32 module:

pip install subprocess32

Here's a code snippet:

>>> 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

Notice, default timeout=None, which means never timeout.

Solution 5:

Post a Comment for "How To Make A Subprocess.call Timeout Using Python 2.7.6?"