Celery unable to use redis
Asked Answered
V

3

66

Trying to start Celery first time but issues error as below, i have installed redis and its starting fine , but still somehow django seems to have issues with it ,

File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/atif/Documents/celery_test/celery-env/lib/python3.8/site-packages/kombu/transport/redis.py", line 263, in <module>
    class PrefixedStrictRedis(GlobalKeyPrefixMixin, redis.Redis):
AttributeError: 'NoneType' object has no attribute 'Redis'

Celery.py

from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_test.settings')



    app = Celery('celery_test',)
    
    app.config_from_object('django.conf:settings')
    
    # Load task modules from all registered Django apps.
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
    
    @app.task(bind=True)
    def debug_task(self):
        print(f'Request: {self.request!r}')

Settings

#celery stuff ---------------
BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'

celery_module/tasks.py

from celery import Celery

app = Celery('tasks',)

@app.task
def add(x, y):
    return x + y
Villasenor answered 30/11, 2021 at 3:54 Comment(1)
pip install -U celery[redis]Decolonize
L
135

Try to install Redis as in your virtual environment as well:

pip install Redis
Leath answered 30/11, 2021 at 19:11 Comment(0)
J
7

Install redis globally with

pip install redis

OR (if you use pipenv)

The error in my case occurs as the redis package in the environment is not used when running

python -m celery -A *django_app* worker

If you use pipenv to manage you python environment you could run the command above as:

pipenv run python -m celery -A *django_app* worker
Juice answered 8/4, 2023 at 11:41 Comment(0)
B
1

If you use Docker Compose, me helped to add redis to file requirements.txt

Biforate answered 16/10, 2023 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.