pythonadvanced
Async Connection Lock Context
Thread-safe asynchronous resource lease with exception cleanup
By ApeType Staff·13 lines·367 characters·1,420 total attempts
import asyncio
from contextlib import asynccontextmanager
class ResourcePool:
def __init__(self, max_connections: int = 5):
self._semaphore = asyncio.Semaphore(max_connections)
@asynccontextmanager
async def acquire(self):
await self._semaphore.acquire()
try:
yield self
finally:
self._semaphore.release()
TRANSCRIBE
Ready when you are.
Test your muscle memory and transcription speed against this exact snippet.
Start Typing
Tip: Press Esc anytime to restart
Best runs on this snippet
No recorded runs yet. Be the first to type this snippet!