Python decorators for asyncio
Project description
aiodecorator
Python decorators for asyncio, including
- throttle: Throttle a (coroutine) function that return an
Awaitable
Install
$ pip install aiodecorator
Usage
import time
import asyncio
from aiodecorator import (
throttle
)
now = time.time()
# -----------------------------------------------------
# The throttled function is only called twice a second
@throttle(2, 1)
async def throttled(index: int):
diff = format(time.time - now, '.0f')
print(index, f'{diff}s')
# -----------------------------------------------------
async def main():
loop = asyncio.get_running_loop()
tasks = [
loop.create_task(throttled(index))
for index in range(5)
]
await asyncio.wait(tasks)
asyncio.run(main())
# Output
# 0 0s
# 1 0s
# 2 1s
# 3 1s
# 4 2s
APIs
throttle(limit: int, interval: Union[float, int])
- limit
int
Maximum number of calls within aninterval
. - interval
Union[int, float]
Timespan for limit in seconds.
Returns a decorator function
License
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
aiodecorator-1.0.2.tar.gz
(3.2 kB
view details)
Built Distribution
File details
Details for the file aiodecorator-1.0.2.tar.gz
.
File metadata
- Download URL: aiodecorator-1.0.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ba0f188777ed3ba1df167dff4383a382785d8eaedda185620f36f68c42a4506 |
|
MD5 | e4bd302c01e4a504f4a5b83e6f9dd1e1 |
|
BLAKE2b-256 | 13a4c6d56ea4f353114fd9d7408d8dc43ebf93635dc75f24c2ea72f3537efccc |
File details
Details for the file aiodecorator-1.0.2-py3-none-any.whl
.
File metadata
- Download URL: aiodecorator-1.0.2-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2a367e0a6da437013c8f6952c1abe26b3876d923a6e7494a68a571b41da5d8a |
|
MD5 | 26c9d5617b7bb07eabb7b6310132d827 |
|
BLAKE2b-256 | 7b7846e660aab3d355d23bb19140083d4e3157bc8d42818b212e17de62696757 |