On this page
threading.BoundedSemaphore
Prev: threading.Barrier | Up: threading | Next: threading.BrokenBarrierError
Rel: threading.active_count, threading.Barrier, threading.BrokenBarrierError, threading.Condition
Ref: docs.python.org — threading.BoundedSemaphore
Tags: #public #python3 #stdlib #threading
Import
import threading
from threading import BoundedSemaphore
Signature & Overview
threading.BoundedSemaphore(value=1)
Implements a bounded semaphore.
Methods & Attributes
BoundedSemaphore.acquire(self, blocking=True, timeout=None)— Acquire a semaphore, decrementing the internal counter by one.BoundedSemaphore.release(self, n=1)— Release a semaphore, incrementing the internal counter by one or more.
Usage
import threading
sem = threading.BoundedSemaphore(2)
with sem:
assert True
try:
sem.release() # releasing above initial value raises ValueError
assert False
except ValueError:
assert True
Gotchas
- Safer than plain
Semaphore: RaisesValueErrorif you accidentally call.release()more times than.acquire()—catches mismatched release bugs early.
REPL Inspect
from threading import BoundedSemaphore
help(BoundedSemaphore)
Linked from
- tuts/python3/stdlib_module/threading
- tuts/python3/stdlib_module/threading/active_count
- tuts/python3/stdlib_module/threading/Barrier
- 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/Barrier 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