简体   繁体   中英

Django: calling celery task declared in same module

I have a celery task that's declared on my Django project that I'm trying to call from the same module it's declared in. Right now, it looks like the following:

# myapp.admin.py

from myproject.celery import app as celery_app

@celery_app.task(name='myapp.admin.add')
def add(x, y):
   time.sleep(10000)
   return x + y

def my_custom_admin_action(modeladmin, request, queryset):
   add.delay(2, 4)

# action later declared in the ModelAdmin

Knowing that celery sometimes is complicated with relative imports, I've specified the name. I even added the following to my settings.py :

CELERY_IMPORTS = ('myapp.admin', )

But when I try to use the admin action, I get the following message in my manage.py celeryd output:

[2014-09-18 14:58:25,413: ERROR/MainProcess] Received unregistered task of type 'myapp.admin.add'.
The message has been ignored and discarded.

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

Traceback (most recent call last):
  File "/Users/JJ/.virtualenvs/TCJ/lib/python2.7/site-packages/celery/worker/consumer.py", line 455, in on_task_received
    strategies[name](message, body,
KeyError: 'myapp.admin.add'

What am I doing wrong here? I even tried importing within the action as from . import add from . import add , but that didn't seem to help.

Celery is not picking your add task. One alternate way to solve this is to modify the instance of your Celery.

In myproject/celery.py change instance of celery

app = Celery('name', backend='your_backend', broker='your_broker')

to

app = Celery('name', backend='your_backend', broker='your_broker', 
             include['myapp.admin',])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM