Skip to content

Instantly share code, notes, and snippets.

# Thread-safe (or async-safe) rate limiter for Python
#
# Very simple but effective: no buckets, tokens, queues, or sleep
# Allows for both smooth limiting and windowed bursts
# Intended for a single client (not distributed) with no long-term memory
import threading
from functools import wraps
from threading import Thread, Timer
from typing import Callable, ParamSpec, TypeVar