Skip to content

Instantly share code, notes, and snippets.

@txomon
txomon / ratelimit.py
Created August 12, 2018 21:24
Rate limit python asyncio
import asyncio
import collections
async def ratelimit(*, max_request, in_interval):
slots = collections.deque()
while True:
slots.append(time() + in_interval)
yield
while len(slots) >= max_request:
left = slots[0] - time()