0

I was using celery and rabbitmq to send email to the user who has logged in. However when I am running my celery worker I am getting :

My project name is myproject and the appname is app

celery -A myproject worker -l info

On running celery I am getting error

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'

settings.py

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

__init__.py

from .celery import app as celery_app

__all__ = ('celery_app',)

Inside my app app I have tasks.py which is sending mail

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,
        '[email protected]',
        [email],
        fail_silently=False
    )

1 Answer 1

0

The error was in my celery.py file present inside the project myproject folder. My project name was myproject but I was reading configuration from mysite.settings. Corrected the same and its working now

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()
Sign up to request clarification or add additional context in comments.

Comments

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.