Junior — Middle
Have you worked with request rate limiting mechanisms in your projects?
sobes.tech AI
Answer from AI
Yes, in Python projects, I often used rate limiting mechanisms to protect APIs from overload and prevent abuse.
One common method is to use libraries such as ratelimit or limits, which allow setting a maximum number of requests within a certain period.
An example using a decorator to limit to 10 requests per minute:
from ratelimit import limits, sleep_and_retry
@sleep_and_retry
def call_api():
# call API
pass
@sleep_and_retry
@limits(calls=10, period=60)
def limited_function():
call_api()
You can also implement your own mechanism using Redis or other storage to count requests by user key or IP.