{"id":4738,"library":"ratelimiter","title":"Ratelimiter","description":"The `ratelimiter` library provides a simple mechanism for rate-limiting operations in Python, both synchronously (using decorators or context managers) and asynchronously (using `async with`). It allows developers to control the frequency of function calls, often useful when interacting with APIs that have usage limits. The current version is 1.2.0.post0, and the project has not seen updates since 2017, suggesting it is no longer actively maintained.","status":"maintenance","version":"1.2.0.post0","language":"en","source_language":"en","source_url":"https://github.com/RazerM/ratelimiter","tags":["rate-limiting","concurrency","api-throttling","decorator","context-manager","asyncio"],"install":[{"cmd":"pip install ratelimiter","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"RateLimiter","correct":"from ratelimiter import RateLimiter"}],"quickstart":{"code":"import time\nfrom ratelimiter import RateLimiter\n\ndef limited_function():\n    print(f\"Function called at {time.time()}\")\n\ndef callback_function(until):\n    duration = int(round(until - time.time()))\n    print(f'Rate limited, sleeping for {duration} seconds')\n\n# Using as a decorator\n@RateLimiter(max_calls=2, period=3, callback=callback_function)\ndef do_something_decorated(item):\n    print(f\"Processing item {item} (decorated)\")\n\nprint(\"--- Decorator Example ---\")\nfor i in range(5):\n    do_something_decorated(i)\n    time.sleep(0.5) # Simulate some work between calls\n\n# Using as a context manager\nrate_limiter_cm = RateLimiter(max_calls=2, period=3, callback=callback_function)\n\nprint(\"\\n--- Context Manager Example ---\")\nfor i in range(5):\n    with rate_limiter_cm:\n        print(f\"Processing item {i} (context manager)\")\n    time.sleep(0.5) # Simulate some work between calls","lang":"python","description":"Demonstrates both decorator and context manager usage of `RateLimiter`. It defines a limit of 2 calls per 3-second period and includes a callback to inform when rate-limiting occurs. The `time.sleep` calls simulate work and illustrate how the limiter pauses execution."},"warnings":[{"fix":"For new projects, evaluate actively maintained alternatives. For existing projects, thoroughly test on your target Python versions.","message":"The `ratelimiter` library has not been updated since December 2017. While it may still function, it is not actively maintained, meaning it might not receive bug fixes, security patches, or compatibility updates for newer Python versions or evolving best practices. Consider more actively maintained alternatives like `pyrate-limiter` or `limits` for new projects.","severity":"deprecated","affected_versions":"<=1.2.0.post0"},{"fix":"Ensure your callback implementation correctly handles the execution context (thread for sync, coroutine for async) and potential blocking behavior. If using `async with`, make sure the callback is `async def`.","message":"The behavior of callbacks differs between synchronous and asynchronous usage. For synchronous decorators/context managers, the `callback` function is executed in a *separate thread*, allowing it to `sleep` without blocking the main program. For `async with` usage, the `callback` must be a *coroutine* and is *not* called in a a separate thread.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For distributed systems, consider libraries or custom implementations that integrate with shared backends like Redis or databases (e.g., `limits` or `pyrate-limiter` with appropriate backends).","message":"This library is designed for in-process rate limiting. It does not inherently support distributed rate limiting across multiple processes or machines without additional external coordination (e.g., a shared Redis backend). Using it directly in a distributed system will lead to each process having its own independent rate limit.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}