0

my version: Django==3.2 celery==5.1.2

my settings.local: CELERY_RESULT_BACKEND = 'redis://@127.0.0.1:6379/1'

celery.py:

from __future__ import absolute_import, unicode_literals

import os
from celery import Celery
from django.conf import settings
# # 设置环境变量
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cmdb.settings.local')
# 实例化
# app = Celery('celeryPro', include=['message.tasks'])
app = Celery('celeryPro', backend='redis://127.0.0.1:6379/1')
# app = Celery('cmdb')

# namespace='CELERY'作用是允许你在Django配置文件中对Celery进行配置
# 但所有Celery配置项必须以CELERY开头,防止冲突
app.config_from_object('django.conf:settings', namespace='CELERY')
# app.config_from_object(config, namespace='CELERY')
# 自动从Django的已注册app中发现任务
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request:{0!r}'.format(self.request))

always get the error

0

1 Answer 1

1

Your setup is incorrect in two ways.

  1. You are adding the backend only when creating the instance of celery and also calling the config_from_object, as per the docs, any previous configuration is reset.
  2. You are passing the incorrect config file to the config_from_object method. You need to send the file that Celery should use and not the one that Django uses. You can find more info in the configuration docs.

As an example, you can have your celery.py file configured as below:

from __future__ import absolute_import, unicode_literals

import os
from celery import Celery
from django.conf import settings
# # 设置环境变量
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cmdb.settings.local')
# 实例化
# app = Celery('celeryPro', include=['message.tasks'])
# app = Celery('celeryPro', backend='redis://127.0.0.1:6379/1')
app = Celery('cmdb')

# namespace='CELERY'作用是允许你在Django配置文件中对Celery进行配置
# 但所有Celery配置项必须以CELERY开头,防止冲突
app.config_from_object('celery_config', namespace='CELERY')
# app.config_from_object(config, namespace='CELERY')
# 自动从Django的已注册app中发现任务
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request:{0!r}'.format(self.request))

and your celery_config.py file could be something like below:

broker_url = 'redis://localhost:6379/1'
result_backend = 'redis://localhost:6379/1'

Having your configuration for celery in a different file allows for more flexibility when you want to extend the configuration.

NOTE - you should keep the celery_config.py file in the root directory of the project i.e., in the same location as the manage.py file.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. This really needs to be improved. In addition, I found that I executed the command on the windows machine without adding "- P eventlet". The problem has been solved
Do to mean the answer needs to be improved? Sorry, your comment wasn't clear to me.
My celery read the configuration from Django's settings configuration , and I added the "- P eventlet" parameter to start celery worker , which can also work well. And I find what you said is also a good improvement for my test project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.