如何在 Django 项目上执行 Celery 周期性任务
How to execute Celery periodic tasks on Django project
貌似用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 + Redis(我已经配置好了),但是Redis从来没有在周期性任务的教程或文档中提及。
If you want to use periodic tasks you need to start the celerybeat
service. You have to make sure only one instance of this server is running at any time, or else you will end up with multiple executions of the same task.
继续阅读整页文档,我认为它直接回答了您的问题。我不相信 Redis 会帮助你 运行 定期任务,但是你的服务器需要 运行 celerybeat
.
貌似用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 + Redis(我已经配置好了),但是Redis从来没有在周期性任务的教程或文档中提及。
If you want to use periodic tasks you need to start the
celerybeat
service. You have to make sure only one instance of this server is running at any time, or else you will end up with multiple executions of the same task.
继续阅读整页文档,我认为它直接回答了您的问题。我不相信 Redis 会帮助你 运行 定期任务,但是你的服务器需要 运行 celerybeat
.