class asyncio.Semaphore(value=1, *, loop=None)
A Semaphore implementation.
A semaphore manages an internal counter which is decremented by each acquire()
call and incremented by each release()
call. The counter can never go below zero; when acquire()
finds that it is zero, it blocks, waiting until some other coroutine calls release()
.
Semaphores also support the context management protocol.
The optional argument gives the initial value for the internal counter; it defaults to 1
. If the value given is less than 0
, ValueError
is raised.
This class is not thread safe.
-
coroutine acquire()
-
Acquire a semaphore.
If the internal counter is larger than zero on entry, decrement it by one and return
True
immediately. If it is zero on entry, block, waiting until some other coroutine has calledrelease()
to make it larger than0
, and then returnTrue
.This method is a coroutine.
-
locked()
-
Returns
True
if semaphore can not be acquired immediately.
-
release()
-
Release a semaphore, incrementing the internal counter by one. When it was zero on entry and another coroutine is waiting for it to become larger than zero again, wake up that coroutine.
Please login to continue.