Created
January 5, 2016 01:36
-
-
Save junhua/80169f43d0c3547ed224 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
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