Mercurial > hg > orthanc-tests
comparison Tests/ExternalCommandThread.py @ 3:2dbba2e6aa4b
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 17 Jun 2015 10:03:49 +0200 |
parents | ExternalCommandThread.py@08dadea8f40a |
children |
comparison
equal
deleted
inserted
replaced
2:a15734e7f0af | 3:2dbba2e6aa4b |
---|---|
1 #!/usr/bin/python | |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
4 # Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics | |
5 # Department, University Hospital of Liege, Belgium | |
6 # | |
7 # This program is free software: you can redistribute it and/or | |
8 # modify it under the terms of the GNU General Public License as | |
9 # published by the Free Software Foundation, either version 3 of the | |
10 # License, or (at your option) any later version. | |
11 # | |
12 # This program is distributed in the hope that it will be useful, but | |
13 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 # General Public License for more details. | |
16 # | |
17 # You should have received a copy of the GNU General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 import os | |
22 import signal | |
23 import subprocess | |
24 import threading | |
25 | |
26 class ExternalCommandThread: | |
27 @staticmethod | |
28 def ExternalCommandFunction(arg, stop_event, command, env): | |
29 external = subprocess.Popen(command, env = env) | |
30 | |
31 while (not stop_event.is_set()): | |
32 error = external.poll() | |
33 if error != None: | |
34 # http://stackoverflow.com/a/1489838/881731 | |
35 os._exit(-1) | |
36 stop_event.wait(0.1) | |
37 | |
38 print 'Stopping the external command' | |
39 external.terminate() | |
40 | |
41 def __init__(self, command, env = None): | |
42 self.thread_stop = threading.Event() | |
43 self.thread = threading.Thread(target = self.ExternalCommandFunction, | |
44 args = (10, self.thread_stop, command, env)) | |
45 self.daemon = True | |
46 self.thread.start() | |
47 | |
48 def stop(self): | |
49 self.thread_stop.set() | |
50 self.thread.join() |