39
39
self.set_sync_event(sync_event)
40
40
self.exception = None
41
41
self.ignored_exceptions = None # see set_ignored_exceptions
42
self.lock = threading.Lock()
43
44
# compatibility thunk for python-2.4 and python-2.5...
44
45
if sys.version_info < (2, 6):
55
56
Some threads require multiple events and should set the relevant one
58
Note that the event should be cleared so the caller can wait() on him
59
and be released when the thread set the event.
59
Note that the event should be initially cleared so the caller can
60
wait() on him and be released when the thread set the event.
62
Also note that the thread can use multiple events, setting them as it
63
progress, while the caller can chose to wait on any of them. What
64
matters is that there is always one event set so that the caller is
65
always released when an exception is caught. Re-using the same event is
66
therefore risky as the thread itself has no idea about which event the
67
caller is waiting on. If the caller has already been released then a
68
cleared event won't guarantee that the caller is still waiting on it.
61
70
self.sync_event = event
72
def switch_and_set(self, new):
73
"""Switch to a new ``sync_event`` and set the current one.
75
Using this method protects against race conditions while setting a new
78
Note that this allows a caller to wait either on the old or the new
79
event depending on whether it wants a fine control on what is happening
82
:param new: The event that will become ``sync_event``
86
try: # Always release the lock
88
self.set_sync_event(new)
89
# From now on, any exception will be synced with the new event
91
# Unlucky, we couldn't set the new sync event, try restoring a
93
self.set_sync_event(cur)
95
# Setting the current ``sync_event`` will release callers waiting
96
# on it, note that it will also be set in run() if an exception is
63
102
def set_ignored_exceptions(self, ignored):
64
103
"""Declare which exceptions will be ignored.