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.1.tar.gz
(3.2 kB
view hashes)
Built Distribution
Close
Hashes for aiodecorator-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eada68a55652313dc5a4352c4818e29afb676c3ff0d73c9496cb5ca220d0f68b |
|
MD5 | 87c0acc155f89110f5003c596b97c350 |
|
BLAKE2b-256 | 565382b554c338a76323a94f30840c1648bf24336032d01e8b5f07557927b0dc |