aiocron is a Python library that enables scheduling asynchronous functions using crontab-like syntax within an asyncio event loop. It provides a decorator for coroutines, making it straightforward to define recurring tasks. Currently at version 2.1, the library has a moderate release cadence, with notable breaking changes between its 1.x and 2.x major versions.
pip install aiocronVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to schedule an asynchronous function to run every minute using the `@aiocron.crontab` decorator. The `main` function sets up the scheduler and then keeps the asyncio event loop running indefinitely with `asyncio.get_event_loop().run_forever()` to ensure tasks are executed.
Review your cron expressions for compatibility with the `cronsim` library's syntax. Refer to `cronsim` documentation for supported formats. If strict `croniter` compatibility is required, pin your `aiocron` dependency to `<2.0`.
Ensure the asyncio event loop is properly started and kept alive for the duration you intend your cron jobs to run, typically using `asyncio.get_event_loop().run_forever()` or a long-running `asyncio.run()` of a main coroutine. Always wrap the logic within your scheduled coroutines in `try...except` blocks to prevent unhandled exceptions from crashing the event loop or silently failing tasks.
Upgrade your Python environment to 3.9 or higher to use aiocron 2.x. If you must use Python 3.7 or 3.8, you will need to pin your aiocron dependency to a compatible 1.x version (e.g., `aiocron<2.0`).
If your code directly imports 'croniter', you need to either remove that import if it's no longer necessary with aiocron 2.x, or adjust to use 'cronsim'. If you are relying on older aiocron behavior, consider pinning your aiocron version to `<2.0`.
Ensure your main application entry point starts and keeps the event loop running, commonly with `asyncio.get_event_loop().run_forever()` within a `asyncio.run()` block, or by letting `asyncio.run()` manage the top-level async function that contains your aiocron setups. Also, add `try...except` blocks within your scheduled coroutines to catch and log exceptions, preventing them from silently breaking the loop.
Verify that any function decorated with `@aiocron.crontab` is indeed an `async def` function. If you set `start=False` in the decorator (e.g., `@aiocron.crontab('* * * * *', start=False)`), you must manually start the job by calling `.start()` on the returned crontab object: `my_scheduled_job = run_my_task; my_scheduled_job.start()`.