mirror of
https://github.com/Garmelon/PFERD.git
synced 2026-04-12 15:35:05 +02:00
Implement limiter
This commit is contained in:
parent
3a74c23d09
commit
b915e393dd
1 changed files with 19 additions and 0 deletions
19
PFERD/limiter.py
Normal file
19
PFERD/limiter.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
import asyncio
|
||||||
|
from contextlib import AbstractAsyncContextManager, asynccontextmanager
|
||||||
|
from typing import AsyncIterator
|
||||||
|
|
||||||
|
|
||||||
|
class Limiter:
|
||||||
|
def __init__(self, limit: int = 10):
|
||||||
|
self._semaphore = asyncio.Semaphore(limit)
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def _context_manager(self) -> AsyncIterator[None]:
|
||||||
|
await self._semaphore.acquire()
|
||||||
|
try:
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
self._semaphore.release()
|
||||||
|
|
||||||
|
def limit(self) -> AbstractAsyncContextManager[None]:
|
||||||
|
return self._context_manager()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue