I found this rate-limiting python decorator based on redis classes. How can I write a similar decorator that uses only what's available in the standard library that can be used as follows?
def ratelimit(limit, every):
# python magic
@ratelimit(limit=1, every=2)
def printlimited(x):
print x
# print one number every two seconds
for x in range(10):
printlimited(x)
There are other answers on stackoverflow but they do not allow to specify the denominator.
You can use a
threading.Semaphore
to count and block the requests that are exceeding the limit, in combination withthreading.Timer
to schedule a function that releases the semaphore.I extended this idea and published a library on PyPI named limit.