Skip to content

Instantly share code, notes, and snippets.

@shirish87
Created March 3, 2016 09:29
Show Gist options
  • Save shirish87/8ca22918a918b29bd330 to your computer and use it in GitHub Desktop.
Save shirish87/8ca22918a918b29bd330 to your computer and use it in GitHub Desktop.
#!/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