From 128f48702a8da155d4b856aa91d2f473103999c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 31 Jul 2018 09:14:33 +0200 Subject: [PATCH] Make AsyncRateLimiter thread safe --- lib/tfw/decorators/rate_limiter.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/tfw/decorators/rate_limiter.py b/lib/tfw/decorators/rate_limiter.py index e0c87d1..371e732 100644 --- a/lib/tfw/decorators/rate_limiter.py +++ b/lib/tfw/decorators/rate_limiter.py @@ -59,17 +59,23 @@ class RateLimiter: return 0 +# TODO document this class AsyncRateLimiter(RateLimiter): def __init__(self, rate_per_second, ioloop_factory): self._ioloop_factory = ioloop_factory self._ioloop = None self._last_callback = None + self._make_action_thread_safe() + super().__init__( rate_per_second=rate_per_second, action=self.async_action ) + def _make_action_thread_safe(self): + self.async_action = partial(self.ioloop.add_callback, self.async_action) + @lazy_property def ioloop(self): return self._ioloop_factory()