Last active
September 19, 2021 19:29
-
-
Save napsternxg/817e1031e2d52e7ef91714c60333b4d7 to your computer and use it in GitHub Desktop.
Rate limiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
def rate_limited_caller(max_rps=100, sleep_time=10, cache=None): | |
num_calls = 1 | |
using_cache = False | |
if isinstance(cache, dict): | |
using_cache = True | |
print(f"Using a cache") | |
def caller(func): | |
def wrapper(*args, **kwargs): | |
nonlocal num_calls | |
nonlocal cache | |
output = None | |
func_call = True | |
if using_cache: | |
key = tuple(args) + tuple(kwargs.items()) | |
if key in cache: | |
print(f"Cache hit: reusing key=({key})") | |
func_call = False | |
else: | |
cache[key] = func(*args, **kwargs) | |
output = cache[key] | |
else: | |
output = func(*args, **kwargs) | |
num_calls += 1 | |
if func_call and (num_calls % max_rps) == 0: | |
print(f"[{time.strftime('%X %x %Z')}] Total calls: {num_calls}. Num calls > {max_rps}. Sleeping for {sleep_time} secs.") | |
time.sleep(sleep_time) | |
return output | |
return wrapper | |
return caller | |
# Example | |
@rate_limited_caller() | |
def square(x): | |
return x**2 | |
print(list(map(square, range(230)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment