rabbitmq 运行celery worker时出错:未找到模块错误:没有名为'mysite'的模块

1szpjjfi  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(238)

我使用celery 和rabbitmq发送电子邮件给已登录的用户。但是,当我运行celery 工作时,我得到:
我的项目名称是myproject,应用程序名称是app

celery -A myproject worker -l info

运行celery 时出错

Traceback (most recent call last):
  File "/etc/myprojectenv/bin/celery", line 8, in <module>
    sys.exit(main())
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/__main__.py", line 15, in main
    sys.exit(_main())
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/bin/celery.py", line 217, in main
    return celery(auto_envvar_prefix="CELERY")
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args,**kwargs)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 1655, in invoke
    sub_ctx = cmd.make_context(cmd_name, args, parent=ctx)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 920, in make_context
    self.parse_args(ctx, args)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 1378, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 2360, in handle_parse_result
    value = self.process_value(ctx, value)
  File "/etc/myprojectenv/lib/python3.8/site-packages/click/core.py", line 2322, in process_value
    value = self.callback(ctx, self, value)
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/bin/worker.py", line 160, in <lambda>
    value: value or ctx.obj.app.conf.worker_state_db,
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/utils/collections.py", line 112, in __getattr__
    return self[k]
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/utils/collections.py", line 392, in __getitem__
    return getitem(k)
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/utils/collections.py", line 250, in __getitem__
    return mapping[_key]
  File "/usr/lib/python3.8/collections/__init__.py", line 1006, in __getitem__
    if key in self.data:
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/utils/objects.py", line 30, in __get__
    return super().__get__(instance, owner)
  File "/usr/lib/python3.8/functools.py", line 967, in __get__
    val = self.func(instance)
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/app/base.py", line 138, in data
    return self.callback()
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/app/base.py", line 967, in _finalize_pending_conf
    conf = self._conf = self._load_config()
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/app/base.py", line 977, in _load_config
    self.loader.config_from_object(self._config_source)
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/loaders/base.py", line 128, in config_from_object
    self._conf = force_mapping(obj)
  File "/etc/myprojectenv/lib/python3.8/site-packages/celery/utils/collections.py", line 43, in force_mapping
    if isinstance(m, (LazyObject, LazySettings)):
  File "/etc/myprojectenv/lib/python3.8/site-packages/django/utils/functional.py", line 258, in inner
    self._setup()
  File "/etc/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 74, in _setup
    self._wrapped = Settings(settings_module)
  File "/etc/myprojectenv/lib/python3.8/site-packages/django/conf/__init__.py", line 183, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mysite'

设置.py

CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
CELERY_BROKER_URL = 'amqp://hpoddar:password@IPADDRESS/vhostcheck'

初始化.py

from .celery import app as celery_app

__all__ = ('celery_app',)

在我的应用程序app中,我有tasks.py正在发送邮件的www.example.com

import time
from celery import shared_task
from django.core.mail import send_mail

@shared_task
def send_email_task(email):
    "background task to send an email asynchronously"
    subject = 'Hello from Celery'
    message = 'This is a test email sent asynchronously with Celery.'
    time.sleep(1)
    return send_mail(
        subject,
        message,
        'stackoverflow@gmail.com',
        [email],
        fail_silently=False
    )
qoefvg9y

qoefvg9y1#

这个错误是在我的celery.py文件中出现在项目的myproject文件夹中。我的项目名是myproject,但是我从mysite.settings读取配置。更正了相同的错误,现在可以正常工作了

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

app = Celery('myproject')

app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django apps.

app.autodiscover_tasks()

相关问题