all Python 3multiprocessingterm
multiprocessing.BoundedSemaphore
synchronization
A semaphore shared between processes that will not let its counter rise above the initial value: an extra release() raises ValueError.
Syntax
sem = ctx.BoundedSemaphore(value=1)
An extra release() is caught at once instead of an hour later, when a "pool of three connections" limit has quietly become four. The exception is ValueError "released too many times"; threading.BoundedSemaphore also raises ValueError, with a different message. As with Semaphore, the acquire() argument is named block. Pass the primitive to the child as a Process argument at creation; it cannot go through a Queue or Pool arguments — RuntimeError "should only be shared between processes through inheritance".
Examples
import multiprocessing as mp
if __name__ == '__main__':
sem = mp.get_context('spawn').BoundedSemaphore(2)
sem.acquire()
sem.release()
try:
sem.release()
except ValueError as e:
print(e) # → semaphore or lock released too many times
See also
Open in the full glossarydocs.python.orgReport an inaccuracy