Mercurial > hg > orthanc-tests
diff Tests/Toolbox.py @ 4:292a46fe374c
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 17 Jun 2015 10:05:27 +0200 |
parents | 2dbba2e6aa4b |
children | 6d645b3011e1 |
line wrap: on
line diff
--- a/Tests/Toolbox.py Wed Jun 17 10:03:49 2015 +0200 +++ b/Tests/Toolbox.py Wed Jun 17 10:05:27 2015 +0200 @@ -21,9 +21,11 @@ import hashlib import httplib2 import json -import os.path +import os import re +import signal import subprocess +import threading import time import zipfile @@ -190,3 +192,32 @@ return 'localhost' else: return m.groups()[0] + + + + +class ExternalCommandThread: + @staticmethod + def ExternalCommandFunction(arg, stop_event, command, env): + external = subprocess.Popen(command, env = env) + + while (not stop_event.is_set()): + error = external.poll() + if error != None: + # http://stackoverflow.com/a/1489838/881731 + os._exit(-1) + stop_event.wait(0.1) + + print 'Stopping the external command' + external.terminate() + + def __init__(self, command, env = None): + self.thread_stop = threading.Event() + self.thread = threading.Thread(target = self.ExternalCommandFunction, + args = (10, self.thread_stop, command, env)) + self.daemon = True + self.thread.start() + + def stop(self): + self.thread_stop.set() + self.thread.join()