"AsyncIO-enabled rate limiter - limit the rate of API calls!"
Project description
AsyncRateLimiter
AsyncIO-enabled rate limiter - limit the rate of API calls!
Public APIs such as Wiktionary may apply rate limits at which one may call any part of their API. A generous API will inform you of this rate limit through a well structured error. Other cases, however, may not be so graceous. It may respond with a general purpose status code 500; a generic, non-informative HTML error page; or even outright close the connection lacking any response whatsoever. For these cases, a rate limiter can help avoid triggering such a response altogether.
AsyncRateLimiter is partly inspired by ratelimiter. The main difference is that AsyncRateLimiter is designed for asynchronous usage, whereas ratelimiter is designed for sequential API calls - further wrap-around code is needed to enable asynchronous code. AsyncRateLimiter integrates with AsyncIO for convenience.
Installation
Simply install via pip install asyncratelimiter
Dependencies
There is a single dependency for convenience: requests
Styles
AsyncRateLimiter supports two distinct usage styles:
- Threaded style - incorporates sequential APIs such as requests with rate-limited AsyncIO
- Async style - limits calls to AsyncIO-based APIs
Threaded Style
Threaded style revolves around the ThreadedRateLimiter.dispatch(fun, /, *args, **kwargs)
method. fun
is enqueued with Python's standard concurrent.futures.ThreadPoolExecutor
, which is particularly useful to dispatch multiple I/O-dependent tasks and waiting on their completion simultaneously.
from asyncratelimiter import ThreadedRateLimiter
import asyncio
async def main():
with ThreadedRateLimiter(max_rate=10, interval=1) as limiter:
futures = []
for _ in range(100):
futures.append(limiter.dispatch())
asyncio.run(main())
Requests Integration
A simple derivation of ThreadedRateLimiter
exposing get
, post
, put
, delete
, and request
methods; one may still access ThreadedRateLimiter.dispatch
as normal.
from asyncratelimiter import RequestLimiter
import asyncio
async def main():
futures = []
with RequestLimiter(max_rate=10, interval=2) as requests:
for _ in range(100):
futures.append(requests.get('https://google.com'))
await asyncio.gather(*futures)
# Should take ~18-20 seconds - the last batch need not necessarily take the full 2s interval
asyncio.run(main())
Async Style
Async style behaves much like ratelimiter, except AsyncIO-integrated.
from asyncratelimiter import AsyncRateLimiter
import asyncio
async def foo():
return 42
async def main():
ratelimiter = AsyncRateLimiter(max_rate=10, interval=1)
for _ in range(100):
async with ratelimiter:
await foo()
asyncio.run(main())
For concurrently running tasks, async style still exposes a dispatch(aw: Awaitable)
method:
from asyncratelimiter import AsyncRateLimiter
import asyncio
async def foo(delay: float):
await asyncio.sleep(delay)
return 42
async def main():
ratelimiter = AsyncRateLimiter(max_rate=10, interval=1)
aws = []
for _ in range(100):
aws.append(ratelimiter.dispatch(foo(0.5)))
await asyncio.gatcher(*aws)
License
MIT License
Copyright (c) 2021 Kiruse
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file asyncratelimiter-1.0.0.tar.gz
.
File metadata
- Download URL: asyncratelimiter-1.0.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9e57d1a5f4281353cbaa59381bae72118e58b621f1982917d16dede3dd49f59 |
|
MD5 | 3ecfefdca908d2b492cafff2535fa65d |
|
BLAKE2b-256 | 83feaca69a09973ef3fa56efec3c91d13ef8a3fb56fb6abf139d7c92fab0c82b |
File details
Details for the file asyncratelimiter-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: asyncratelimiter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/56.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b22e80110bae6c959f770ee1daf1627e1fe7bdfe9d66953dc937aeac67f2b048 |
|
MD5 | c9d4221762d4fb47a31c01e04edb8b2d |
|
BLAKE2b-256 | a922a8208925695f8161029c8dcd8b01b20de9607332d588a962d3b5b2cbd8ef |