Lightweight scheduler for python asyncio
Based on croniter to support the crontab syntax.
Installation
Installing acron.
$ pip install acron
Usage
To get started you need at least one job. Use the top level acron.run function for simple scheduling.
import asyncio
import acron
async def do_the_thing():
print("Doing the thing")
do_thing = acron.SimpleJob(
name="Do the thing",
schedule="0/1 * * * *",
func=do_the_thing,
)
asyncio.run(acron.run({do_thing}))
For more advanced use cases, the Scheduler class can be used as async context manager. Call scheduler.wait() to keep it running forever. To submit jobs call scheduler.update_jobs(jobs) with the complete set of jobs.
Running a simple example running a function every hour…
import asyncio
import dataclasses
from acron.scheduler import Scheduler, Job
@dataclasses.dataclass(frozen=True)
class ThingData:
foo: bool
async def do_the_thing(data: ThingData):
print(f"Doing the thing {data}")
async def run_jobs_forever():
do_thing = Job[ThingData](
name="Do the thing",
schedule="0/1 * * * *",
data=ThingData(True),
func=do_the_thing,
)
async with Scheduler() as scheduler:
await scheduler.update_jobs({do_thing})
await scheduler.wait()
if __name__ == "__main__":
try:
asyncio.run(run_jobs_forever())
except KeyboardInterrupt:
print("Bye.")
Specifying a timezone
For python 3.9+ you can use the standard library’s zoneinfo module to specify a timezone.
import zoneinfo
async with Scheduler(tz=zoneinfo.ZoneInfo("Europe/Berlin")) as scheduler:
...
For earlier python versions you can use a third party library like pytz.
import pytz
async with Scheduler(tz=pytz.timezone("Europe/Berlin")) as scheduler:
...
Job context
It is possible to retrieve the context for the scheduled job from the running job function using job_context(). This returns a JobContext containing a reference to the ScheduledJob. The job_context() function is implemented using contextvars to provide the correct context to the matching asyncio task.
async def my_job_func():
job_id = acron.job_context().scheduled_job.id
job_name = acron.job_context().scheduled_job.job.name
print(f"Running job {job_id!r}, scheduled with id {job_id}")
Local development
The project uses poetry to run the test, the linter and to build the artifacts.
The easiest way to start working on acron is to use docker with the dockerfile included in the repository (manual usage of poetry is explained here: https://python-poetry.org/docs/ ).
To use docker, first generate the docker image. Run this command from the top level directory in the repository:
docker build -t acron-builder -f docker/Dockerfile .
Now you can use it to build or run the linter/tests:
$ alias acron-builder="docker run --rm -it -v $PWD/dist:/build/dist acron-builder"
$ acron-builder run pytest tests
=============================================================================================== test session starts ================================================================================================
platform linux -- Python 3.9.7, pytest-5.4.3, py-1.10.0, pluggy-0.13.1
rootdir: /build
plugins: asyncio-0.15.1
collected 4 items
tests/test_acron.py .... [100%]
================================================================================================ 4 passed in 0.04s =================================================================================================
$ acron-builder build
Building acron (0.1.0)
- Building sdist
- Built acron-0.1.0.tar.gz
- Building wheel
- Built acron-0.1.0-py3-none-any.whl
$ ls dist
acron-0.1.0-py3-none-any.whl acron-0.1.0.tar.gz
Debugging
Debug logging can be enabled by setting the ACRON_DEBUG environment variable to TRUE.
Metadata
Release files for acron 0.2.1
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 |
|---|---|---|---|---|
| acron-0.2.1.tar.gz | 8.1 kB | Without provenance | Details for acron-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| acron-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for acron-0.2.1-py3-none-any.whl |
Total release size: 16.0 kB
Release files / acron-0.2.1.tar.gz
| Download URL | acron-0.2.1.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ce71715f6bd1cce290d9b1b9e8f2b49dd2880b2e20affb957f950e595921364d
|
|
BLAKE2b-256 checksum How to use checksums |
364ca88fef0cca8b33df9c1669502d9d52c446d005996dfb50bfdf2f86f560cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.10 CPython/3.8.10 Linux/5.11.0-37-generic
|
Release files / acron-0.2.1-py3-none-any.whl
| Download URL | acron-0.2.1-py3-none-any.whl |
|---|---|
| Size | 7.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3c5e44c27737d6e1a69822be141680a99fe78cb1aea425dee2ea5a4f2799f250
|
|
BLAKE2b-256 checksum How to use checksums |
3362b4541ce7702c4351a42cd23a46d12850b6a271583d3d8ffa19aae5299b1d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.10 CPython/3.8.10 Linux/5.11.0-37-generic
|
Security for acron 0.2.1
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.