Skip to content

Instantly share code, notes, and snippets.

@junhua
Created January 5, 2016 01:36
Show Gist options
  • Save junhua/80169f43d0c3547ed224 to your computer and use it in GitHub Desktop.
Save junhua/80169f43d0c3547ed224 to your computer and use it in GitHub Desktop.
import subprocess as sub
import threading
class RunCmd(threading.Thread):
def __init__(self, cmd, timeout):
threading.Thread.__init__(self)
self.cmd = cmd
self.timeout = timeout
def run(self):
self.p = sub.Popen(self.cmd, stdout=sub.PIPE, stderr=sub.STDOUT)
self.p.wait()
def Run(self):
self.start()
self.join(self.timeout)
if self.is_alive():
self.p.terminate() #use self.p.kill() if process needs a kill -9
self.join()
return None
else:
return self.p
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment