Skip to content

Instantly share code, notes, and snippets.

@pcustic
Created February 14, 2024 15:26
Show Gist options
  • Save pcustic/2e5c5a461a0435f2d24cf870adb01f6d to your computer and use it in GitHub Desktop.
Save pcustic/2e5c5a461a0435f2d24cf870adb01f6d to your computer and use it in GitHub Desktop.
aiolimiter lib example
from aiolimiter import AsyncLimiter
max_requests = 100
time_period = 60 # in seconds
limiter = AsyncLimiter(max_requests, time_period)
# You can use it with context manager:
async with limiter:
# This section will, at most, be entered max_requests times / time_period.
print("Doing something...")
# Or call acquire() method directly.
await limiter.acquire() # blocks until there is capacity
# Or you can test if there is capacity first.
if limiter.has_capacity():
print("Do something")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment