Celery 中的PeriodicTask run() 方法拥有自己的参数
我正在编写一个小型 Django 应用程序,我应该能够创建 对于每个模型对象,其定期任务将被执行 一定的间隔。我使用 Celery 应用程序,但我无法理解一件事:
class ProcessQueryTask(PeriodicTask):
run_every = timedelta(minutes=1)
def run(self, query_task_pk, **kwargs):
logging.info('Process celery task for QueryTask %d' %
query_task_pk)
task = QueryTask.objects.get(pk=query_task_pk)
task.exec_task()
return True
然后我执行以下操作:
>>> from tasks.tasks import ProcessQueryTask
>>> result1 = ProcessQueryTask.delay(query_task_pk=1)
>>> result2 = ProcessQueryTask.delay(query_task_pk=2)
第一次调用成功,但其他定期调用返回错误 - TypeError: run() 恰好需要 2 个非关键字参数(给定 1 个) 芹菜服务器。 我可以将自己的参数传递给PeriodicTask run()
吗?
I am writing a small Django application and I should be able to create
for each model object its periodical task which will be executed with
a certain interval. I'm use for this a Celery application, but i can't understand one thing:
class ProcessQueryTask(PeriodicTask):
run_every = timedelta(minutes=1)
def run(self, query_task_pk, **kwargs):
logging.info('Process celery task for QueryTask %d' %
query_task_pk)
task = QueryTask.objects.get(pk=query_task_pk)
task.exec_task()
return True
Then i'm do following:
>>> from tasks.tasks import ProcessQueryTask
>>> result1 = ProcessQueryTask.delay(query_task_pk=1)
>>> result2 = ProcessQueryTask.delay(query_task_pk=2)
First call is success, but other periodical calls returning the error
- TypeError: run() takes exactly 2 non-keyword arguments (1 given) in
celeryd server.
Can I pass own params to PeriodicTask run()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Ask Solem 在对您的问题的回答中对此做出了精彩的回答 celery-users Google 群组。
周期性任务不使用参数,因此您需要创建多个
类或创建一个处理多个“模型”的周期性任务。
例如:
但您更有可能想要这样的东西:
This was answered wonderfully by Ask Solem in his response to your question on the celery-users Google group.
Periodic tasks doesn't use arguments, so you need to make several
classes or make a periodic task that processes more than one "model".
E.g.:
but it's more likely you want something like this: