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
20
29
30
32 """
33 threading.Event extension which provides an additional method
34 setAtExit() which sets "atexit" to true.
35
36 This class was introduced in 4.2.1 to work around issue #3260
37 in which logging from background threads produced error
38 messages.
39 """
40
41 - def __init__(self, verbose = None, name = "Unknown"):
45
46 name = property(lambda self: self.__name)
47 atexit = property(lambda self: self.__atexit)
48
52
55
56
57 -class Timer(threading._Timer):
58 """Based on threading._Thread but allows for resetting the Timer.
59
60 t = Timer(30.0, f, args=[], kwargs={})
61 t.start()
62 t.cancel() # stop the timer's action if it's still waiting
63
64 # or
65
66 t.reset()
67
68 After excecution, the status of the run can be checked via the
69 "completed" and the "exception" Event instances.
70 """
71
72 - def __init__(self, interval, function, args=[], kwargs={}):
73 threading._Timer.__init__(self, interval, function, args, kwargs)
74 self.log = logging.getLogger(omero.util.make_logname(self))
75 self.completed = threading.Event()
76 self.exception = threading.Event()
77 self._reset = threading.Event()
78
83
85 while True:
86 self.finished.wait(self.interval)
87 if self._reset.isSet():
88 self.finished.clear()
89 self._reset.clear()
90 self.log.debug("Resetting")
91 continue
92 if not self.finished.isSet():
93 try:
94 self.log.debug("Executing")
95 self.function(*self.args, **self.kwargs)
96 self.completed.set()
97 self.finished.set()
98 except:
99 self.exception.set()
100 self.finished.set()
101 raise
102 break
103