Created
March 3, 2016 09:29
-
-
Save shirish87/8ca22918a918b29bd330 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/python | |
import subprocess | |
import os | |
from threading import Timer | |
proc = None | |
proc_timeout = 3.0 | |
def on_timeout(): | |
print "Timed out!" | |
if proc: | |
proc.kill() | |
def run_with_timeout(cmd, proc_timeout=10.0, on_timeout=on_timeout): | |
t = Timer(proc_timeout, on_timeout) | |
t.start() | |
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE) | |
out = proc.communicate()[0] | |
t.cancel() | |
print "Output: {}".format(out) | |
run_with_timeout(['sleep', '5'], proc_timeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment