python - celery如何立即执行周期性任务?

标签 python celery

我想使用 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/

相关文章:

python - Celery是否使用链和组组合的结果后端?

python - 替换数据帧列中的整数 w/'Y'

python - 如何在 django 中执行操作后重定向到同一页面

python - 排队大量 celery 任务

postgresql - Airflow 1.9 - 任务卡在队列中

python - Django 和 celery : unable to pickle task

django - 如何在 Django 和 Celery 中配置多个代理?

python - 在 SQLAlchemy 中创建子对象时不可排序的类型

python - 索引错误 : list index out of range/AttributeError: 'list' object has no attribute 'get_attribute'

python - 如何使用 multiprocessing.Pool.apply_async 记录到单个文件