Closed niloch closed 6 years ago
If you print until
, you should see that sometimes it's milliseconds between them, because one rate limited call put it beyond the limit.
Hmm. maybe I am confused. It seems the actual rate limiting is working correctly, but when using requests.get
the callback is triggered too often. Are you saying that the decorated function is behaving quasi asynchronously?
one call to thing2
will result in 0 or 1 calls to callback_message
I ended up being able to solve my issue by modifying the __enter__
function to reset the calls queue after sleeping, so the call back was called exactly 3 times in 90 requests.s
def __enter__(self):
with self._lock:
# We want to ensure that no more than max_calls were run in the allowed
# period. For this, we store the last timestamps of each call and run
# the rate verification upon each __enter__ call.
if len(self.calls) >= self.max_calls:
until = time.time() + self.period - self._timespan
if self.callback:
t = threading.Thread(target=self.callback, args=(until,))
t.daemon = True
t.start()
sleeptime = until - time.time()
if sleeptime > 0:
time.sleep(sleeptime)
self.calls = collections.deque()
return self
I have a function that uses the
requests
library that I am decorating with a callback. The callback seems to be triggered more frequently than one would expect. The behavior works as expected when the decorated function returns a string.This is the output: