Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved sleep cycle in acquire func #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions asyncio_throttle/throttler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@


class Throttler:
def __init__(self, rate_limit: int, period=1.0, retry_interval=0.01):
def __init__(self, rate_limit: int, period=1.0):
self.rate_limit = rate_limit
self.period = period
self.retry_interval = retry_interval

self._task_logs: Deque[float] = deque()

Expand All @@ -25,7 +24,8 @@ async def acquire(self):
self.flush()
if len(self._task_logs) < self.rate_limit:
break
await asyncio.sleep(self.retry_interval)
time_to_release = self._task_logs[0] + self.period - time.monotonic()
await asyncio.sleep(time_to_release)

self._task_logs.append(time.monotonic())

Expand Down