pferd/PFERD/limiter.py
Joscha 7e127cd5cc Clean up and fix conductor and limiter
Turns out you have to await an async lock, who knew...
2021-04-29 13:44:04 +02:00

17 lines
402 B
Python

import asyncio
from contextlib import asynccontextmanager
from typing import AsyncIterator
class Limiter:
def __init__(self, limit: int = 10):
self._semaphore = asyncio.Semaphore(limit)
@asynccontextmanager
async def limit(self) -> AsyncIterator[None]:
await self._semaphore.acquire()
try:
yield
finally:
self._semaphore.release()