Celery beat with method tasks not working

丶灬走出姿态 提交于 2019-12-01 08:35:31

问题


I'm trying to run celerybeat on a method task, and can't get anything to work out properly. Here's an example setup:

from celery.contrib.methods import task_method
from celery import Celery, current_app

celery=celery('tasks', broker='amqp://guest@localhost//')
celery.config_from_object("celeryconfig")
class X(object):
    @celery.task(filter=task_method, name="X.ppp")
    def ppp(self):
        print "ppp"

and my celeryconfig.py file is

from datetime import timedelta
CELERYBEAT_SCHEDULE = {
      'test' : {
               'task' : 'X.ppp', 
               'schedule' : timedelta(seconds=5)
               }, 
 }

When I run celery beat, I'm getting errors like:

 task X.ppp raised exception, TypeError('ppp() takes exactly 1 argument, (0 given)  

When I turn the method into a normal function and decorate it with `@celery.task', it does work, so the rest of the setup seems to be working. I see the caveats in the docs about method tasks, but can't really sort out where the problem is. Does anyone know how to resolve this?


回答1:


The problem is that Celerybeat will not instantiate X before calling the method. The task_method filter defaults to calling the unbound method if the method is not bound to an object.

My question is, what are you trying to accomplish here? X has no state, so why not use a module-evel function?



来源:https://stackoverflow.com/questions/14515218/celery-beat-with-method-tasks-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!