Create a combined event that is set when any input events are set.
Note
The creator can wait on the combined event, but must still check
each individual event to see which was set.
Warning
This works by dynamically replacing methods on the inputs events
with custom methods that trigger callbacks.
Note
Based on this Stack Overflow
answer.
Parameters:
-
events
(Event
, default:
()
)
–
One or more events to combine.
Returns:
-
Event
–
A single event that is set when any of the input events is set.
Source code in academy/event.py
| def or_event(*events: threading.Event) -> threading.Event:
"""Create a combined event that is set when any input events are set.
Note:
The creator can wait on the combined event, but must still check
each individual event to see which was set.
Warning:
This works by dynamically replacing methods on the inputs events
with custom methods that trigger callbacks.
Note:
Based on this Stack Overflow
[answer](https://stackoverflow.com/a/12320352).
Args:
events: One or more events to combine.
Returns:
A single event that is set when any of the input events is set.
"""
combined = threading.Event()
def changed() -> None:
bools = [e.is_set() for e in events]
if any(bools):
combined.set()
else:
combined.clear()
for e in events:
_orify(e, changed)
changed()
return combined
|