跳转到主要内容

asyncio的Crontabs

项目描述

https://travis-ci.org/gawel/aiocron.svg?branch=master https://img.shields.io/pypi/v/aiocron.svg https://img.shields.io/pypi/dm/aiocron.svg

用法

aiocron 提供了一个装饰器,用于在指定时间运行函数

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

您也可以将其用作对象

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

您的函数仍然可在 attime.func 中使用

您还可以await一个crontab。在这种情况下,您的协程可以接受参数

>>> @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()

最后,您可以使用它作为一个sleep协程。以下将等待到下一个小时

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

如果您不喜欢装饰器的魔法,您可以自己设置函数

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

请注意,与标准的Unix crontab不同,您可以在第六个位置指定秒。

aiocron 使用 croniter。请参阅其文档以了解更多关于crontab格式的信息。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

aiocron-1.8.tar.gz (16.3 kB 查看哈希值)

上传时间:

构建分发

aiocron-1.8-py3-none-any.whl (4.8 kB 查看哈希值)

上传时间: Python 3

支持者