Celery 4 not auto-discovering tasks

2024/10/11 1:20:36

I have a Django 1.11 and Celery 4.1 project, and I've configured it according to the setup docs. My celery_init.py looks like

from __future__ import absolute_importimport osfrom celery import Celery# set the default Django settings module for the 'celery' program.
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings.settings'app = Celery('myproject')app.config_from_object('django.conf:settings', namespace='CELERY')#app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) # does nothing
app.autodiscover_tasks() # also does nothingprint('Registering debug task...')
@app.task(bind=True)
def debug_task(self):print('Request: {0!r}'.format(self.request))

However, when I launch a worker with:

.env/bin/celery worker -A myproject -l info

it shows no tasks being found except for the sample "debug_task", even though I have several installed apps with Celery tasks, with should have been found via the call to app.autodiscover_task(). This is the initial output my worker generates:

 -------------- celery@localhost v4.1.0 (latentcall)
---- **** ----- 
--- * ***  * -- Linux-4.13.0-16-generic-x86_64-with-Ubuntu-16.04-xenial 2017-10-31 15:56:42
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         myproject:0x7f952856d650
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     amqp://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** ----- -------------- [queues].> celery           exchange=celery(direct) key=celery[tasks]. myproject.celery_init.debug_task[2017-10-31 15:56:42,180: INFO/MainProcess] Connected to amqp://guest:**@127.0.0.1:5672//
[2017-10-31 15:56:42,188: INFO/MainProcess] mingle: searching for neighbors
[2017-10-31 15:56:43,211: INFO/MainProcess] mingle: all alone
[2017-10-31 15:56:43,237: INFO/MainProcess] celery@localhost ready.

All my legacy tasks in my app tasks.py files were defined like:

from celery.task import task@task(name='mytask')
def mytask():blah

The docs suggest using the shared_task decorator, so instead I tried:

from celery import shared_task@shared_task
def mytask():blah

But my Celery worker still doesn't see it. What am I doing wrong?

Edit: I've been able to get tasks to show up by explicitly listing them in my setting's CELERY_IMPORTS list, but even then I have to heavily edit the tasks.py to remove all imports of my Django project (models.py, etc) or it raises the exception Apps aren't loaded yet. This is better than nothing, but requires a huge amount of refactoring. Is there a better way?

Answer

I had a similar issue, and the solution was to add the include kwarg to your celery call.

The include argument is a list of modules to import when the worker starts. You need to add our tasks module here so that the worker is able to find our tasks.

app = Celery('myproject', backend = settings.CELERY.get('backend'),broker = settings.CELERY.get('broker'),include = ['ingest.tasks.web', ... ])

Check out http://docs.celeryproject.org/en/latest/getting-started/next-steps.html#proj-celery-py for more information

https://en.xdnf.cn/q/69829.html

Related Q&A

Is it possible to use a custom filter function in pandas?

Can I use my helper function to determine if a shot was a three pointer as a filter function in Pandas? My actual function is much more complex, but i simplified it for this question.def isThree(x, y…

what is request in Django view

In the Django tutorial for the first app in Django we havefrom django.http import HttpResponsedef index(request):return HttpResponse("Hello, world. Youre at the polls index.")And then the url…

Cannot update python package on anaconda to latest version

Some of my python packages on anaconda cannot be updated to the latest version.For instance, beautifulsoup4 latest version on anaconda is v4.71 as seen in the release notes. https://docs.anaconda.com/a…

How does tensorflow.pad work?

There is the example of tensorflow.pad():# t = is [[1, 2, 3], [4, 5, 6]]. # paddings is [[1, 1,], [2, 2]]. # rank of t is 2.tf.pad(t, paddings, "CONSTANT") ==> [[0, 0, 0, 0, 0, 0, 0],[…

Installing PyPotrace on Windows 10

I would like to install Potrace on my Windows 10 Computer and I will be using it with python 2.7.5. I am following the installation instruction from this site.( https://pypi.python.org/pypi/pypotrace) …

dateutil 2.5.0 is the minimum required version

Im running the jupyter notebook (Enthought Canopy python distribution 2.7) on Mac OSX (v 10.13.6). When I try to import pandas (import pandas as pd), I am getting the complaint: ImportError: dateutil …

pytest fixture - get value and avoid error Fixture X called directly

I have updated pytest to 4.3.0 and now I need to rework test code since calling fixtures directly is deprecated. I have an issue with fixtures used in an unittest.TestCase, how do I get the value retur…

Django limit the number of requests per minute

Im trying to limit the number of requests from an IP in case I get too many requests from it. For example: if I will get more than 50 requests per minute I want to block that IP for 5 minutes. When I u…

How to add template variable in the filename of an EmailOperator task? (Airflow)

I cant seem to get this to work.I am trying to send daily a given file, whose name is like file_{{ds_nodash}}.csv.The problem is that I cant seem to add this name as the filename, since it seems it can…

Disadvantage of Python eggs?

Are there any disadvantages about using eggs through easy-install compared to the "traditional" packages/modules/libs?