Add docstrings to RateLimiter

This commit is contained in:
Kristóf Tóth 2018-07-31 05:19:15 +02:00
parent ee7adb10be
commit 4679a3494c

View File

@ -7,8 +7,32 @@ from time import time, sleep
from tfw.decorators.lazy_property import lazy_property from tfw.decorators.lazy_property import lazy_property
# TODO: add this to sphinx docs
class RateLimiter: class RateLimiter:
"""
Decorator class for rate limiting.
When applied to a function this decorator will apply rate limiting
if the function is invoked more frequently than rate_per_seconds.
By default rate limiting means sleeping until the next invocation time
as per __init__ parameter rate_per_seconds.
Note that this decorator BLOCKS THE THREAD it is being executed on,
so it is only acceptable for stuff running on a separate thread.
If this is no good for you please refer to AsyncRateLimiter in this module,
which is designed not to block and use the IOLoop it is being called from,
or redefine the action argument of __init__ (which defaults to time.sleep).
"""
def __init__(self, rate_per_second, action=sleep): def __init__(self, rate_per_second, action=sleep):
"""
:param rate_per_second: max frequency the decorated method should be
invoked with
:param action: what to do when rate limiting. defaults to time.sleep,
receives the number of seconds until the next invocation
should occour as an argument
"""
self.min_interval = 1 / float(rate_per_second) self.min_interval = 1 / float(rate_per_second)
self.action = action self.action = action
self.fun = None self.fun = None