Package: acron
Lightweight scheduler for python asyncioBased on croniter to support the crontab syntax.InstallationInstalling acron.$pipinstallacronUsageTo get started you need at least one job.
Use the top levelacron.runfunction for simple scheduling.importasyncioimportacronasyncdefdo_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, theSchedulerclass can be used as async context manager.
Callscheduler.wait()to keep it running forever.
To submit jobs callscheduler.update_jobs(jobs)with the complete set of jobs.Running a simple example running a function every hour…importasyncioimportdataclassesfromacron.schedulerimportScheduler,Job@dataclasses.dataclass(frozen=True)classThingData:foo:boolasyncdefdo_the_thing(data:ThingData):print(f"Doing the thing{data}")asyncdefrun_jobs_forever():do_thing=Job[ThingData](name="Do the thing",schedule="0/1 * * * *",data=ThingData(True),func=do_the_thing,)asyncwithScheduler()asscheduler:awaitscheduler.update_jobs({do_thing})awaitscheduler.wait()if__name__=="__main__":try:asyncio.run(run_jobs_forever())exceptKeyboardInterrupt:print("Bye.")Specifying a timezoneFor python 3.9+ you can use the standard library’szoneinfomodule to specify a timezone.importzoneinfoasyncwithScheduler(tz=zoneinfo.ZoneInfo("Europe/Berlin"))asscheduler:...For earlier python versions you can use a third party library likepytz.importpytzasyncwithScheduler(tz=pytz.timezone("Europe/Berlin"))asscheduler:...Job contextIt is possible to retrieve the context for the scheduled job from the running
job function usingjob_context(). This returns aJobContextcontaining
a reference to theScheduledJob. Thejob_context()function is implemented
using contextvars to provide the correct context to the matching asyncio task.asyncdefmy_job_func():job_id=acron.job_context().scheduled_job.idjob_name=acron.job_context().scheduled_job.job.nameprint(f"Running job{job_id!r}, scheduled with id{job_id}")Local developmentThe 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:$aliasacron-builder="docker run --rm -it -v$PWD/dist:/build/dist acron-builder"$acron-builderrunpytesttests=============================================================================================== 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-builderbuildBuilding acron (0.1.0)
  - Building sdist
  - Built acron-0.1.0.tar.gz
  - Building wheel
  - Built acron-0.1.0-py3-none-any.whl$lsdistacron-0.1.0-py3-none-any.whl  acron-0.1.0.tar.gzDebuggingDebug logging can be enabled by setting theACRON_DEBUGenvironment variable toTRUE.