Repository navigation
Expand file tree
/
Copy paththreadutil.py
More file actions
51 lines (39 loc) · 1.44 KB
/
Copy paththreadutil.py
File metadata and controls
51 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""Threading utilities."""
from threading import Event, Condition
class EventGroup(object):
"""EventGroup that can be waited with an OR condition."""
class Event(Event):
"""Threading event meant to be used in an group."""
def __init__(self, shared_condition):
"""
Construct an event.
:param shared_condition: shared condition variable.
:type shared_condition: threading.Condition
"""
Event.__init__(self)
self._shared_cond = shared_condition
def set(self):
"""Set the event."""
Event.set(self)
with self._shared_cond:
self._shared_cond.notify()
def __init__(self):
"""Construct an event group."""
self._cond = Condition()
def make_event(self):
"""
Make a new event associated to this waitable group.
:returns: an event that can be awaited as part of a group
:rtype: EventGroup.Event
"""
return EventGroup.Event(self._cond)
def wait(self, timeout=None):
"""
Wait until one of the events is triggered.
:param timeout: how many seconds to wait. None means forever.
:type timeout: int
:returns: True if the condition was notified within the specified timeout. False otherwise.
:rtype: bool
"""
with self._cond:
return self._cond.wait(timeout)