Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. import time
  2. import asyncio
  3. from collections import deque
  4.  
  5. from attr import dataclass, ib
  6.  
  7.  
  8. @dataclass(slots=True, frozen=True)
  9. class AsyncThrottler:
  10. rate_limit: int
  11. period: float
  12. interval: float = 0.01
  13.  
  14. tasks_times: deque = ib(factory=deque, init=False)
  15.  
  16. def wait(self) -> None:
  17. now = time.time()
  18.  
  19. while self.tasks_times:
  20. task_wait_time = now - self.tasks_times[0]
  21.  
  22. if task_wait_time > self.period:
  23. self.tasks_times.popleft()
  24. continue
  25.  
  26. break
  27.  
  28. async def acquire(self) -> None:
  29. while True:
  30. self.wait()
  31.  
  32. if len(self.tasks_times) < self.rate_limit:
  33. break
  34.  
  35. await asyncio.sleep(self.interval)
  36.  
  37. self.tasks_times.append(time.time())
  38.  
  39. async def __aenter__(self) -> None:
  40. await self.acquire()
  41.  
  42. async def __aexit__(self, exc_type, exc, tb) -> None:
  43. pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement