-
-
Save JordanReiter/2415688 to your computer and use it in GitHub Desktop.
Enables to run subprocess commands in a different thread with TIMEOUT option!
This file contains hidden or 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, threading | |
class Command(object): | |
''' | |
Enables to run subprocess commands in a different thread | |
with TIMEOUT option! | |
Based on jcollado's solution: | |
http://stackoverflow.com/questions/1191374/subprocess-with-timeout/4825933#4825933 | |
''' | |
def __init__(self, cmd): | |
self.cmd = cmd | |
self.process = None | |
def run(self, timeout=None, **kwargs): | |
def target(**kwargs): | |
self.process = subprocess.Popen(self.cmd, **kwargs) | |
self.process.communicate() | |
thread = threading.Thread(target=target, kwargs=kwargs) | |
thread.start() | |
thread.join(timeout) | |
if thread.is_alive(): | |
self.process.terminate() | |
thread.join() | |
return self.process.returncode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment