Django和Celery的例子:定期任务 [英] Examples of Django and Celery: Periodic Tasks
问题描述
我一直在打Django / Celery文档一段时间,需要一些帮助。
I have been fighting the Django/Celery documentation for a while now and need some help.
我希望能够使用django-celery运行定期任务。我已经看到互联网(和文档)的几种不同的格式和模式,应该如何使用Celery实现这一点...
I would like to be able to run Periodic Tasks using django-celery. I have seen around the internet (and the documentation) several different formats and schemas for how one should go about achieving this using Celery...
有人可以帮助一个基本一个django-celery定期任务的创建,注册和执行的功能实例?特别是,我想知道我是否应该编写一个扩展PeriodicTask类并注册的任务,或者是否应该使用@periodic_task装饰器,还是应该使用@task装饰器,然后为任务的执行。
Can someone help with a basic, functioning example of the creation, registration and execution of a django-celery periodic task? In particular, I want to know whether I should write a task that extends the PeriodicTask class and register that, or whether I should use the @periodic_task decorator, or whether I should use the @task decorator and then set up a schedule for the task's execution.
我不介意,如果这三种方式都是可行的,但我想看到一个至少有一种方法可行的例子。真的很感激你的帮助。
I don't mind if all three ways are possible, but I would like to see an example of at least one way that works. Really appreciate your help.
推荐答案
from celery.task import PeriodicTask
from clickmuncher.messaging import process_clicks
from datetime import timedelta
class ProcessClicksTask(PeriodicTask):
run_every = timedelta(minutes=30)
def run(self, **kwargs):
process_clicks()
您可以使用装饰器编写相同的任务:
You could write the same task using a decorator:
from celery.task.schedules import crontab
from celery.task import periodic_task
@periodic_task(run_every=crontab(minute="*/30"))
def process_clicks():
....
装饰器合成ax 只允许您将现有的功能/任务转换为定期任务,而无需直接修改。
The decorator syntax simply allows you to turn an existing function/task into a periodic task without modifying them directly.
要执行的任务 celerybeat必须正在运行。
这篇关于Django和Celery的例子:定期任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!