import time from threading import Lock class RateTracker: def __init__(self, rate: int = 1000): """ rate is max number of calls per second.""" self.rate = rate self.delay = 1.0/rate self._last_call = time.time() - self.delay self._lock = Lock() def done(self): """ call to indicate processing is done""" pass def create_wait(self): """ Generation of tasks is hard-limited, no matter the execution rate """ with self._lock: passed = time.time() - self._last_call to_wait = self.delay - passed if to_wait > 0: time.sleep(to_wait) self._last_call += to_wait else: self._last_call = time.time() def start(self): """ indicate start of the call """ pass