On this page
threading.Event
Prev: threading.enumerate | Up: threading | Next: threading.excepthook
Rel: threading.active_count, threading.Barrier, threading.BoundedSemaphore, threading.BrokenBarrierError
Ref: docs.python.org — threading.Event
Tags: #public #python3 #stdlib #threading
Import
import threading
from threading import Event
Signature & Overview
threading.Event()
Class implementing event objects.
Methods & Attributes
Event.clear(self)— Reset the internal flag to false.Event.isSet(self)— Return true if and only if the internal flag is true.Event.is_set(self)— Return true if and only if the internal flag is true.Event.set(self)— Set the internal flag to true.Event.wait(self, timeout=None)— Block until the internal flag is true.
Usage
import threading
ready = threading.Event()
def setup() -> None:
ready.set()
t = threading.Thread(target=setup)
t.start()
assert ready.wait(timeout=2) is True
t.join()
Gotchas
- Cleanest cross-thread shutdown/ready flag: One thread calls
stop_event.set(); any number of workers checkstop_event.is_set()orstop_event.wait(timeout=...). - Stays set until
clear()is called: Subsequentwait()calls returnTrueimmediately.
REPL Inspect
from threading import Event
help(Event)
Linked from
Related notes
- tuts/python3/stdlib_module/threading/enumerate Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/excepthook Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/threading/active_count Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Barrier Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BoundedSemaphore Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BrokenBarrierError Linked here; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Condition shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/current_thread shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/ExceptHookArgs shared tags: #stdlib, #threading