On this page
threading.Barrier
Prev: threading.active_count | Up: threading | Next: threading.BoundedSemaphore
Rel: threading.active_count, threading.BoundedSemaphore, threading.BrokenBarrierError, threading.Condition
Ref: docs.python.org — threading.Barrier
Tags: #public #python3 #stdlib #threading
Import
import threading
from threading import Barrier
Signature & Overview
threading.Barrier(parties, action=None, timeout=None)
Implements a Barrier.
Methods & Attributes
Barrier.abort(self)— Place the barrier into a 'broken' state.Barrier.broken(...)— Return True if the barrier is in a broken state.Barrier.n_waiting(...)— Return the number of threads currently waiting at the barrier.Barrier.parties(...)— Return the number of threads required to trip the barrier.Barrier.reset(self)— Reset the barrier to the initial state.Barrier.wait(self, timeout=None)— Wait for the barrier.
Usage
import threading
barrier = threading.Barrier(2)
log: list[str] = []
def worker(name: str) -> None:
log.append(f"{name}:before")
barrier.wait(timeout=2)
log.append(f"{name}:after")
threads = [threading.Thread(target=worker, args=(n,)) for n in ("A", "B")]
for t in threads: t.start()
for t in threads: t.join()
assert len(log) == 4
Gotchas
- Always pass
timeout=: If one thread crashes before reachingwait(), other threads block forever unless a timeout triggersBrokenBarrierErroror the failing thread callsbarrier.abort(). - Reusable: Once all
partiesarrive, the barrier resets automatically for the next phase.
REPL Inspect
from threading import Barrier
help(Barrier)
Linked from
- tuts/python3/stdlib_module/threading
- tuts/python3/stdlib_module/threading/active_count
- tuts/python3/stdlib_module/threading/BoundedSemaphore
- tuts/python3/stdlib_module/threading/BrokenBarrierError
- tuts/python3/stdlib_module/threading/Condition
- tuts/python3/stdlib_module/threading/current_thread
- tuts/python3/stdlib_module/threading/enumerate
- tuts/python3/stdlib_module/threading/Event
- tuts/python3/stdlib_module/threading/excepthook
- tuts/python3/stdlib_module/threading/ExceptHookArgs
- tuts/python3/stdlib_module/threading/get_ident
- tuts/python3/stdlib_module/threading/get_native_id
- tuts/python3/stdlib_module/threading/getprofile
- tuts/python3/stdlib_module/threading/gettrace
- tuts/python3/stdlib_module/threading/local
- tuts/python3/stdlib_module/threading/Lock
- tuts/python3/stdlib_module/threading/main_thread
- tuts/python3/stdlib_module/threading/RLock
- tuts/python3/stdlib_module/threading/Semaphore
- tuts/python3/stdlib_module/threading/setprofile
- tuts/python3/stdlib_module/threading/setprofile_all_threads
- tuts/python3/stdlib_module/threading/settrace
- tuts/python3/stdlib_module/threading/settrace_all_threads
- tuts/python3/stdlib_module/threading/stack_size
- tuts/python3/stdlib_module/threading/Thread
- tuts/python3/stdlib_module/threading/ThreadError
- tuts/python3/stdlib_module/threading/Timer
Related notes
- tuts/python3/stdlib_module/threading/active_count Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BoundedSemaphore Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/BrokenBarrierError Linked here; links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Condition 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/current_thread links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/enumerate links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/Event links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/excepthook links to this page; shared tags: #stdlib, #threading
- tuts/python3/stdlib_module/threading/ExceptHookArgs links to this page; shared tags: #stdlib, #threading