On this page
asyncio.BoundedSemaphore
Prev: asyncio.BaseTransport | Up: asyncio | Next: asyncio.BrokenBarrierError
Rel: asyncio.AbstractEventLoop, asyncio.AbstractServer, asyncio.all_tasks, asyncio.as_completed
Ref: docs.python.org — asyncio.BoundedSemaphore
Tags: #public #python3 #stdlib #asyncio
Import
import asyncio
from asyncio import BoundedSemaphore
Signature & Overview
asyncio.BoundedSemaphore(value=1)
A bounded semaphore implementation.
Methods & Attributes
BoundedSemaphore.acquire(self)— Acquire a semaphore.BoundedSemaphore.locked(self)— Returns True if semaphore cannot be acquired immediately.BoundedSemaphore.release(self)—BoundedSemaphore.releasemethod/property.
Usage
import asyncio
async def main():
sem = asyncio.BoundedSemaphore(2)
async with sem:
assert True
asyncio.run(main())
Gotchas
- Prevents over-releasing: Raises
ValueErrorif.release()is called more times than.acquire().
REPL Inspect
from asyncio import BoundedSemaphore
help(BoundedSemaphore)
Linked from
Related notes
- tuts/python3/stdlib_module/asyncio/BaseTransport Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BrokenBarrierError Linked here; links to this page; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractEventLoop Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/AbstractServer Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/all_tasks Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/as_completed Linked here; shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/Barrier shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseEventLoop shared tags: #asyncio, #stdlib
- tuts/python3/stdlib_module/asyncio/BaseProtocol shared tags: #asyncio, #stdlib