import threading import time class APIRateLimiter: """API rate limiter""" def __init__(self, calls_per_second=2): self.min_interval = 1.0 / calls_per_second self.last_call = {} self.lock = threading.Lock() def wait(self, api_name): """Wait until API can be called""" with self.lock: current_time = time.time() if api_name in self.last_call: elapsed = current_time - self.last_call[api_name] if elapsed < self.min_interval: time.sleep(self.min_interval - elapsed) self.last_call[api_name] = current_time