from subprocess import Popen, PIPE import signal class ProcessTimeout(Exception): pass def timeout_handler(signum, frame): raise ProcessTimeout def run_timeout(cmd, timeout=None): if timeout: signal.signal(signal.SIGALRM, timeout_handler) signal.alarm(timeout) proc = Popen(cmd, stdout=PIPE, stderr=PIPE) stdout = stderr = '' try: stdout, stderr = proc.communicate() signal.alarm(0) except ProcessTimeout: proc.kill() stdout = 'Calculation was taking too long, so I killed it dead.' del proc return (stdout, stderr)
Then, just call run_timeout with a list of command arguments and move on.
No comments:
Post a Comment