Skip to content

Instantly share code, notes, and snippets.

@pcustic
Created February 15, 2024 11:23
Show Gist options
  • Save pcustic/540f5ea3634bd77d107f283e65513ff8 to your computer and use it in GitHub Desktop.
Save pcustic/540f5ea3634bd77d107f283e65513ff8 to your computer and use it in GitHub Desktop.
PyrateLimiter example basic
from pyrate_limiter import Duration, Rate, Limiter, BucketFullException
max_requests = 5
time_period = Duration.SECOND * 2
rate = Rate(max_requests, time_period)
limiter = Limiter(rate)
# Or you can pass multiple rates
# rates = [Rate(5, Duration.SECOND * 2), Rate(10, Duration.MINUTE)]
# limiter = Limiter(rates)
for request in range(max_requests + 1):
try:
limiter.try_acquire(request)
except BucketFullException as err:
print(err)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment