comparison env/lib/python3.7/site-packages/galaxy/util/monitors.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 from __future__ import absolute_import
2
3 import logging
4 import threading
5
6 from .sleeper import Sleeper
7 from .web_compat import register_postfork_function
8
9 log = logging.getLogger(__name__)
10
11 DEFAULT_MONITOR_THREAD_JOIN_TIMEOUT = 5
12
13
14 class Monitors(object):
15
16 def _init_monitor_thread(self, name, target_name=None, target=None, start=False, config=None):
17 self.monitor_join_sleep = getattr(config, "monitor_thread_join_timeout", DEFAULT_MONITOR_THREAD_JOIN_TIMEOUT)
18 self.monitor_join = self.monitor_join_sleep > 0
19 self.monitor_running = True
20
21 if target is not None:
22 assert target_name is None
23 monitor_func = target
24 else:
25 target_name = target_name or "monitor"
26 monitor_func = getattr(self, target_name)
27 self.sleeper = Sleeper()
28 self.monitor_thread = threading.Thread(name=name, target=monitor_func)
29 self.monitor_thread.setDaemon(True)
30 self._start = start
31 register_postfork_function(self.start_monitoring)
32
33 def _init_noop_monitor(self):
34 self.sleeper = None
35 self.monitor_join = False
36
37 def start_monitoring(self):
38 if self._start:
39 self.monitor_thread.start()
40
41 def stop_monitoring(self):
42 self.monitor_running = False
43
44 def _monitor_sleep(self, sleep_amount):
45 self.sleeper.sleep(sleep_amount)
46
47 def shutdown_monitor(self):
48 self.stop_monitoring()
49 if self.sleeper is not None:
50 self.sleeper.wake()
51 if self.monitor_join:
52 log.debug("Joining monitor thread")
53 self.monitor_thread.join(self.monitor_join_sleep)