Asynchronous tasks scheduler and executor
Project description
kaiju-scheduler is a simple asynchronous tasks scheduler / executor for asyncio functions. It adds a bit of extra such as retries, timeouts, execution policies etc.
Installation
With pip and python 3.8+:
pip3 install kaiju-scheduler
How to use
See the user guide for more info.
Initialize a scheduler and schedule your procedure for periodic execution. Then start the scheduler.
from kaiju_scheduler import Scheduler
async def call_async_procedure(*args, **kws):
...
async def main():
scheduler = Scheduler()
scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})
await scheduler.start()
...
await scheduler.stop()
Alternatively you can use the scheduler contextually.
async def main():
async with Scheduler() as scheduler:
scheduler.schedule_task(call_async_procedure, interval_s=10, args=(1, 2), kws={'value': True})
Scheduler.schedule_task
returns a task object which you can enable / disable or supress the task execution in
your code temporarily using task.suspend
context. You can also access the previous call results from task.result
attribute.
class Cache:
def __init__(self, scheduler: Scheduler):
self._scheduler = scheduler
self._cache_task = self._scheduler.schedule_task(
self.cache_all, interval_s=600, policy=scheduler.ExecPolicy.WAIT)
async def cache_all(self):
...
async def reconfigure_cache(self):
async with self._cache_task.suspend():
"Do something while the caching is suspended"
You can specify retries for common types of errors such as IOError
or ConnectionError
using retries
parameter.
The scheduler will try to retry the call on such type of error.
scheduler.schedule_task(call_async_procedure, interval_s=300, retries=3, retry_interval_s=1)
There are various policies considering task execution. See the reference for more info on that.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for kaiju_scheduler-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e9b24365ccb4409b84c8d4039da4306a83a1526b1a51d12b62b2861c5b5b7a5 |
|
MD5 | 660aa082177803e9d35059e09c1f3521 |
|
BLAKE2b-256 | 24207ac06d67c19b2e90985a4f31718e711fa3f222f41002c908951b89ec65e5 |