aiodecorator
Python decorators for asyncio, including
- timeout: Set the timeout for a function
- throttle: Throttle a (coroutine) function that return an
Awaitable - repeat: Repeat a function
- schedule_naturally: Schedule a function to run from the next time moment
Install
$ pip install aiodecorator
Usage
import time
import asyncio
from aiodecorator import (
throttle
)
async def run(throttle_type):
now = time.time()
# -----------------------------------------------------
# The throttled function is only called twice a second
@throttle(2, 1, throttle_type)
async def throttled(index: int):
await asyncio.sleep(0.1)
diff = format(time.time() - now, '.0f')
print(index, f'{diff}s')
# -----------------------------------------------------
loop = asyncio.get_running_loop()
tasks = [
loop.create_task(throttled(index))
for index in range(5)
]
await asyncio.wait(tasks)
asyncio.run(run('wait'))
# Output: queued
# 0 0s
# 1 0s
# 2 1s
# 3 1s
# 4 2s
asyncio.run(run('ignore'))
# Output: 2, 3, 4 are ignored
# 0 0s
# 1 0s
asyncio.run(run('replace'))
# Output: 1, 2, 3 are canceled
# 0 0s
# 4 0s
APIs
throttle(limit: int, interval: Union[float, int], throttle_type)
- limit
intMaximum number of calls within aninterval. - interval
Union[int, float]Timespan for limit in seconds. - throttle_type
Literal['ignore', 'wait', 'replace'] = 'ignore'- 'ignore': ignore the function call and return
Noneif it exceeds the limit. - 'wait': wait for the next tick to execute the function.
- 'replace': try to cancel the last function call, let it return
Noneand execute the current function call. (New in3.1.0)
- 'ignore': ignore the function call and return
Returns a decorator function
schedule_naturally(unit, delay, weekday)
- unit
Literal['secondly', 'minutely', 'hourly', 'daily', 'weekly', 'monthly', 'yearly'] - delay
timedelta = timedelta(seconds=0) - weekday
Weekdayonly used whenunitis'weekly'
Returns a decorator function that schedule a function fn to run from the next time moment with a delay delay
For example:
@schedule_natually('daily', delay = timedelta(seconds=50))
async def run():
print('hello')
await run()
# It will print 'hello' at 00:00:50 in the next day
@repeat(-1)
@schedule_naturally(
'weekly',
delay = timedelta(minutes=5),
weekday = 'Wednesday'
)
async def run_weekly():
print('hello')
await run_weekly()
# It will print 'hello' at 00:05 every Wednesday
repeat(times: int, interval: float = 0.)
- times
intthe number of times to repeat the function - interval
float = 0.the interval between each call
@repeat(7)
@schedule_naturally('daily')
@repeat(3, 0.1)
async def run()
print('hello')
await run()
# It will schedule a one-week plan, at 00:00:00 each day, it prints "hello" three times, with 100 ms between each print.
timeout(seconds: int | None, at: float | None)
New in 3.1.0
- seconds
int | None = Noneseconds to time out. Ifseconds <= 0orsecondsisNone, there will be no timeout. - at (float | None = None): deadline to timeout,
athas higher priority thanseconds
Make the function automatically cancel itself if it takes longer than seconds seconds.
@timeout(1)
async def my_function():
await asyncio.sleep(2)
try:
await my_function():
except asyncio.TimeoutError:
print('timeout')
# It will print 'timeout'
License
Metadata
Release files for aiodecorator 3.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| aiodecorator-3.2.2.tar.gz | 10.4 kB | Without provenance | Details for aiodecorator-3.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| aiodecorator-3.2.2-py3-none-any.whl | Python 3 | none | any | Details for aiodecorator-3.2.2-py3-none-any.whl |
Total release size: 19.3 kB
Release files / aiodecorator-3.2.2.tar.gz
| Download URL | aiodecorator-3.2.2.tar.gz |
|---|---|
| Size | 10.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fb4696d266ea3ceb783f9dbcc599e04ac9803f9569adbebe28bd3163d42278dc
|
|
BLAKE2b-256 checksum How to use checksums |
10ae52b5b62584e3bca9c119c026a4ef15b29d6ce909db6c3568cccf5cfab2f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / aiodecorator-3.2.2-py3-none-any.whl
| Download URL | aiodecorator-3.2.2-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a8b8cdc46565450b50df1267fd33e7f7f8d7701b0e023b38969451087fe2614e
|
|
BLAKE2b-256 checksum How to use checksums |
d3100f59b8bd07b9de8fc43aa320b7766d49043b46f250445b4629a9b143339b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Security for aiodecorator 3.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.