1
2
3
4
5
6
7
8
9 import os
10 import sys
11 import time
12 import uuid
13 import atexit
14 import logging
15 import threading
16 import omero.util
17 import exceptions
18 import logging.handlers
19
21 """
22 Returns a threading.Event instance which is registered to be
23 "set" (Event.set()) on system exit.
24 """
25 event = threading.Event()
26 atexit.register(event.set)
27 return event
28
29 -class Timer(threading._Timer):
30 """Based on threading._Thread but allows for resetting the Timer.
31
32 t = Timer(30.0, f, args=[], kwargs={})
33 t.start()
34 t.cancel() # stop the timer's action if it's still waiting
35
36 # or
37
38 t.reset()
39
40 After excecution, the status of the run can be checked via the
41 "completed" and the "exception" Event instances.
42 """
43
44 - def __init__(self, interval, function, args=[], kwargs={}):
45 threading._Timer.__init__(self, interval, function, args, kwargs)
46 self.log = logging.getLogger(omero.util.make_logname(self))
47 self.completed = threading.Event()
48 self.exception = threading.Event()
49 self._reset = threading.Event()
50
52 self.log.debug("Reset called")
53 self._reset.set()
54 self.finished.set()
55
57 while True:
58 self.finished.wait(self.interval)
59 if self._reset.isSet():
60 self.finished.clear()
61 self._reset.clear()
62 self.log.debug("Resetting")
63 continue
64 if not self.finished.isSet():
65 try:
66 self.log.debug("Executing")
67 self.function(*self.args, **self.kwargs)
68 self.completed.set()
69 self.finished.set()
70 except:
71 self.exception.set()
72 self.finished.set()
73 raise
74 break
75