0

Моя цель: настроить регулярное выполнение задачи через celery.

Всё делаю по документации: https://docs.celeryq.dev/en/stable/django/first-steps-with-django.html#django-first-steps

При запуске celery через celery -A crmsouthost.crmsouthost beat -l INFO получаю ошибку:

(venv) PS C:\Users\Vladimir\Projects\crmsouthost> celery -A crmsouthost.crmsouthost beat -l INFO   
celery beat v5.2.7 (dawn-chorus) is starting.
Traceback (most recent call last):
  File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\Scripts\celery.exe\__main__.py", line 7, in <module>
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\__main__.py", line 15, in main
    sys.exit(_main())
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\bin\celery.py", line 217, in main
    return celery(auto_envvar_prefix="CELERY")
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\click\decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\bin\base.py", line 134, in caller
    return f(ctx, *args, **kwargs)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\bin\beat.py", line 72, in beat
    return beat().run()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\apps\beat.py", line 75, in run
    self.init_loader()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\apps\beat.py", line 124, in init_loader
    self.app.loader.init_worker()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\loaders\base.py", line 111, in init_worker
    self.import_default_modules()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\loaders\base.py", line 105, in import_default_modules
    raise response
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\utils\dispatch\signal.py", line 276, in send
    response = receiver(signal=self, sender=sender, **named)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\fixups\django.py", line 82, in on_import_modules
    self.worker_fixup.validate_models()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\fixups\django.py", line 120, in validate_models
    self.django_setup()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\celery\fixups\django.py", line 116, in django_setup
    django.setup()
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\Vladimir\Projects\crmsouthost\venv\lib\site-packages\django\apps\config.py", line 228, in create
    import_module(entry)
  File "C:\Users\Vladimir\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'


Структура проекта:

введите сюда описание изображения

Файлы:

# crmsouthost\__init__.py

from .celery import app as celery_app

__all__ = ('celery_app',)
# crmsouthost\celery.py

import os

from datetime import timedelta
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'crmsouthost.crmsouthost.settings')
app = Celery('crmsouthost')
app.config_from_object('django.conf:settings', namespace='CELERY')

app.autodiscover_tasks()

app.conf.beat_schedule = {
    'check-tasks-overdue': {
        'task': 'main.tasks.tasks_overdue_notify',
        'schedule': timedelta(seconds=10)
    },
}

# main\tasks.py

from crmsouthost.crmsouthost.celery import app


@app.task
def tasks_overdue_notify():
    print("Works")

# crmsouthost\settings.py
# ...
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    'rest_framework',
    'rest_framework_simplejwt',
]
# ...

Версии:

  • Python: 3.10.4
  • Django: 4.0.3
  • Celery: 5.2.7

Заранее спасибо

1 ответ 1

0

Для потомков:

Необходимо запускать celery из папки, в которой находится файл manage.py, созданный django

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.