我想使用 celery 执行周期性任务。
根据文档:http://docs.celeryproject.org/en/latest/userguide/periodic-tasks.html#entries ,我写下了以下代码:
app.conf.beat_schedule = {
'test_celery': {
'task': 'tasks.login.test_timertask',
'schedule': 60.0,
}
}
但是当我按如下方式运行时:
celery beat -A tasks.login --loglevel=info
第一个任务在 60 秒后执行。
我想在工作人员启动后立即启动任务,而不是延迟 60 秒。我该怎么办?
最佳答案
您可以使用crontab schedules为此:
from celery.schedules import crontab
app.conf.beat_schedule = {
'test_celery': {
'task': 'tasks.login.test_timertask',
'schedule': crontab(), # default, executes every minute
}
}
但您必须了解以下内容(如文档所述):
A Crontab like schedule also exists, see the section on Crontab schedules.
Like with cron, the tasks may overlap if the first task doesn’t complete before the next. If that’s a concern you should use a locking strategy to ensure only one instance can run at a time (see for example Ensuring a task is only executed one at a time).
关于python - celery如何立即执行周期性任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43659404/