import threading import time class RateGate: def __init__(self, max_concurrent: int, min_interval: float): self._sem = threading.Semaphore(max_concurrent) self._min_interval = min_interval self._lock = threading.Lock() self._last_call = 0.0 def __enter__(self): self._sem.acquire() with self._lock: wait = self._last_call + self._min_interval - time.monotonic() if wait > 0: time.sleep(wait) self._last_call = time.monotonic() return self def __exit__(self, *exc): self._sem.release() return False