##// END OF EJS Templates
svn-support: Use a queue and worker thread to serialize execution of event subscriber.
Martin Bornhold -
r1017:9910c00d default
parent child Browse files
Show More
@@ -19,6 +19,7 b''
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 import Queue
22 23 import subprocess32
23 24 from threading import Thread
24 25
@@ -51,21 +52,44 b' class Subscriber(object):'
51 52
52 53
53 54 class AsyncSubscriber(Subscriber):
54 def __init__(self, *args, **kwargs):
55 self._init_args = args
56 self._init_kwargs = kwargs
55 def __init__(self):
56 self._stop = False
57 self._eventq = Queue.Queue()
58 self._worker = self.create_worker()
59 self._worker.start()
57 60
58 61 def __call__(self, event):
59 kwargs = {'event': event}
60 kwargs.update(self._init_kwargs)
61 self.thread = Thread(
62 target=self.run, args=self._init_args, kwargs=kwargs)
63 self.thread.start()
62 self._eventq.put(event)
63
64 def create_worker(self):
65 worker = Thread(target=self.do_work)
66 worker.daemon = True
67 return worker
68
69 def stop_worker(self):
70 self._stop = False
71 self._eventq.put(None)
72 self._worker.join()
73
74 def do_work(self):
75 while not self._stop:
76 event = self._eventq.get()
77 if event is not None:
78 self.run(event)
64 79
65 80
66 81 class AsyncSubprocessSubscriber(AsyncSubscriber):
67 def run(self, event, cmd, timeout=None):
82
83 def __init__(self, cmd, timeout=None):
84 super(AsyncSubprocessSubscriber, self).__init__()
85 self._cmd = cmd
86 self._timeout = timeout
87
88 def run(self, event):
89 cmd = self._cmd
90 timeout = self._timeout
68 91 log.debug('Executing command %s.', cmd)
92
69 93 try:
70 94 output = subprocess32.check_output(
71 95 cmd, timeout=timeout, stderr=subprocess32.STDOUT)
General Comments 0
You need to be logged in to leave comments. Login now