【问题标题】:How to execute Celery periodic tasks on Django project如何在 Django 项目中执行 Celery 定期任务
【发布时间】:2021-04-14 21:05:50
【问题描述】:

用 Celery 实现周期性任务似乎很容易,只需执行以下操作:

from celery.schedules import crontab
from celery.task import periodic_task

@periodic_task(run_every=crontab(hour=7, minute=30, day_of_week="mon"))
def every_monday_morning():
    print("This is run every Monday morning at 7:30")

一些教程建议将这些任务放在一个名为“tasks.py”的文件中,但是现在这样做之后呢?我如何告诉 django 或 celery “激活”或“运行”这些任务?。

此外,这类任务仅在 Celery 上中继,或者需要使用 Celery + Redis(我已经配置过),但 Redis 在周期性任务的教程或文档中从未提及。

【问题讨论】:

    标签: django redis celery


    【解决方案1】:

    celery documentation 说:

    如果您想使用周期性任务,您需要启动celerybeat 服务。您必须确保在任何时候都只运行此服务器的一个实例,否则您最终将多次执行同一任务。

    继续阅读整个文档页面,我认为它直接回答了您的问题。我不相信 Redis 会帮助您运行定期任务,但是您的服务器将需要运行 celerybeat

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 2013-06-30
      • 1970-01-01
      相关资源
      最近更新 更多