aiocron

aiocron

Crontabs for asyncio

Stars: 371

Visit
 screenshot

aiocron is a Python library that provides crontab functionality for asyncio. It allows users to schedule functions to run at specific times using a decorator or as an object. Users can also await a crontab, use it as a sleep coroutine, and customize functions without decorator magic. aiocron has switched from croniter to cronsim for cron expression parsing since Dec 31, 2024.

README:

================================================ aiocron - Crontabs for asyncio

.. image:: https://img.shields.io/pypi/v/aiocron.svg :target: https://pypi.python.org/pypi/aiocron .. image:: https://img.shields.io/pypi/dm/aiocron.svg :target: https://pypi.python.org/pypi/aiocron

Usage

aiocron provides a decorator to run function at time::

>>> import aiocron
>>> import asyncio
>>>
>>> @aiocron.crontab('*/30 * * * *')
... async def attime():
...     print('run')
...
>>> asyncio.get_event_loop().run_forever()

You can also use it as an object::

>>> @aiocron.crontab('1 9 * * 1-5', start=False)
... async def attime():
...     print('run')
...
>>> attime.start()
>>> asyncio.get_event_loop().run_forever()

Your function will still be available at attime.func

You can also await a crontab. In this case, your coroutine can accept arguments::

>>> @aiocron.crontab('0 9,10 * * * mon,fri', start=False)
... async def attime(i):
...     print('run %i' % i)
...
>>> async def once():
...     try:
...         res = await attime.next(1)
...     except Exception as e:
...         print('It failed (%r)' % e)
...     else:
...         print(res)
...
>>> asyncio.get_event_loop().run_forever()

Finally you can use it as a sleep coroutine. The following will wait until next hour::

>>> await crontab('0 * * * *').next()

If you don't like the decorator magic, you can set the function by yourself::

>>> cron = crontab('0 * * * *', func=yourcoroutine, start=False)

aiocron uses cronsim <https://github.com/cuu508/cronsim>_. Refer to its documentation to know more about the crontab format.

Since Dec 31, 2024, aiocron has switched from croniter to cronsim for cron expression parsing (PR #39 <https://github.com/gawel/aiocron/pull/39>). Please ensure that your cron expressions are valid in cronsim. For a comparison of features between croniter and cronsim, refer to the cronsim documentation <https://github.com/cuu508/cronsim?tab=readme-ov-file#cron-expression-feature-matrix>.

For Tasks:

Click tags to check more tools for each tasks

For Jobs:

Alternative AI tools for aiocron

Similar Open Source Tools

For similar tasks

For similar jobs