0
0 комментариев

Доброго времени суток!
Появились проблемы с подключением selery с использованием брокера redis к джанго приложению. Вот подробный ход моих действий:

1) Установил сам redis

  brew install redis
 
  brew services start redis

2) Проверил работоспособность

$redis-cli ping
 
PONG
 
redis-server

редсервер

3) Установил redis и celery на виртуальную машину

 pip install celery
 pip install redis
 pip install django-celery-beat
 pip install django-celery-results
 pip freeze > requirements.txt

4) Обновил список приложений в `DJANGO_APPS’

 INSTALLED_APPS = [
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_celery_beat',
 'django_celery_results',
 'selerytest',
 ]
 
 CELERY_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 = TIME_ZONE

5) Cоздал файл ‘selery.py’ в корне проекта ( там где лежит settings.py )

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
 
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'red.settings')
 
app = Celery('proj')
 
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
 
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
 
@app.task(bind=True)
def debug_task(self):
  print('Request: {0!r}'.format(self.request))

6) Обновил __init__.py файл

 # cfehome/src/cfehome/__init__.py
 from __future__ import absolute_import
 
 # This will make sure the app is always imported when
 # Django starts so that shared_task will use this app.
 from .celery import app as celery_app  # noqa

7) Создал tasks.py в папке приложения celerytest

 from __future__ import absolute_import, unicode_literals
 import random
 from celery.decorators import task
 
 @task(name="sum_two_numbers")
 def add(x, y):
     return x + y
 
 @task(name="multiply_two_numbers")
 def mul(x, y):
     total = x * (y * random.randint(3, 100))
     return total
 
 @task(name="sum_list_numbers")
 def xsum(numbers):
     return sum(numbers)

8) Сделал миграции

 python manage.py makemigrations
 python manage.py migrate

9) Запускаю тест в коре папки ( там где manage.py)

 celery -A yourproject worker -l info
 # like
 celery -A cfehome worker -l info

и на этом шаге получаю ошибку когда запускаю celery -A celerybot worker -l info

(env) Milkiweeds-MacBook-Pro:celerybot milkiweed$  celery -A celerybot worker -l info
Traceback (most recent call last):
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/kombu/utils/objects.py", line 42, in __get__
    return obj.__dict__[self.__name__]
KeyError: 'data'
 
During handling of the above exception, another exception occurred:
 
Traceback (most recent call last):
  File "/Users/milkiweed/Desktop/projects/celerybot/env/bin/celery", line 11, in <module>
    sys.exit(main())
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/__main__.py", line 14, in main
    _main()
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/celery.py", line 326, in main
    cmd.execute_from_commandline(argv)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
    super(CeleryCommand, self).execute_from_commandline(argv)))
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/base.py", line 281, in execute_from_commandline
    return self.handle_argv(self.prog_name, argv[1:])
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/celery.py", line 480, in handle_argv
    return self.execute(command, argv)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/celery.py", line 412, in execute
    ).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/worker.py", line 219, in run_from_argv
    *self.parse_options(prog_name, argv, command))
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/base.py", line 403, in parse_options
    self.parser = self.create_parser(prog_name, command)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/base.py", line 419, in create_parser
    self.add_arguments(parser)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/bin/worker.py", line 275, in add_arguments
    default=conf.worker_state_db,
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/utils/collections.py", line 130, in __getattr__
    return self[k]
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/utils/collections.py", line 431, in __getitem__
    return getitem(k)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/utils/collections.py", line 280, in __getitem__
    return mapping[_key]
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/collections/__init__.py", line 987, in __getitem__
    if key in self.data:
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/kombu/utils/objects.py", line 44, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/app/base.py", line 148, in data
    return self.callback()
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/app/base.py", line 911, in _finalize_pending_conf
    conf = self._conf = self._load_config()
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/app/base.py", line 921, in _load_config
    self.loader.config_from_object(self._config_source)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/loaders/base.py", line 133, in config_from_object
    self._conf = force_mapping(obj)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/celery/utils/collections.py", line 50, in force_mapping
    if isinstance(m, (LazyObject, LazySettings)):
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/django/utils/functional.py", line 238, in inner
    self._setup()
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/milkiweed/Desktop/projects/celerybot/env/lib/python3.6/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'red'

Прошу помощи :(

UPD

В коде файла views.py по всей видимости был старый код, который я удалил
теперь при вводе этой же самой команды пишет подобное
ModuleNotFoundError: No module named

Подскажите пожалуйста, что вставить вместо celery -A yourproject worker -l info

я в этом деле совсем новичек просто


Добавить комментарий