Celery-Django as Daemon: Settings Not Found

Following this tutorial , I now have a Celery-Django application that works fine if I run this worker with this command: celery -A myapp worker -n worker1.% H

in my Django.py settings, I set all the parameters for Celery (IP message broker, etc.). Everything works well.

Now my next step is to run this application as Daemon. So I followed this second tutorial , and everything is simple, except that now the Celery options included in settings.py are not loading. For example, broker IP messages are set to 127.0.0.1, but in my settings.py I set it to a different IP address.

The textbook says:

make sure the module that defines the Celery application instance also sets the default value for DJANGO_SETTINGS_MODULE, as shown in the Django sample project in First Steps with Django .

Therefore, I was convinced. I have in / etc / default / celeryd this:

export DJANGO_SETTINGS_MODULE = "myapp.settings"

Still not working ... So I, too, this line in /etc/init.d/celeryd did not work again. I do not know what to do next. Does anyone have a key?

EDIT: Here is my celery.py:

from __future__ import absolute_import import os from django.conf import settings from celery import Celery # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings') app = Celery('myapp') # Using a string here means the worker will not have to # pickle the object when using Windows. app.config_from_object('django.conf:settings') app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request)) 

EDIT # 2: Here is my / etc / default / celeryd:

 # Names of nodes to start # most will only start one node: CELERYD_NODES="worker1.%h" # Absolute or relative path to the 'celery' command: CELERY_BIN="/usr/local/bin/celery" # App instance to use # comment out this line if you don't use an app CELERY_APP="myapp" # Where to chdir at start. CELERYD_CHDIR="/home/ubuntu/myapp-folder/" # Extra command-line arguments to the worker CELERYD_OPTS="" # %N will be replaced with the first part of the nodename. CELERYD_LOG_FILE="/var/log/celery/%N.log" CELERYD_PID_FILE="/var/run/celery/%N.pid" # Workers should run as an unprivileged user. # You need to create this user manually (or you can choose # a user/group combination that already exists, eg nobody). CELERYD_USER="ubuntu" CELERYD_GROUP="ubuntu" # If enabled pid and log directories will be created if missing, # and owned by the userid/group configured. CELERY_CREATE_DIRS=1 # Name of the projects settings module. export DJANGO_SETTINGS_MODULE=myapp.settings export PYTHONPATH=$PYTHONPATH:/home/ubuntu/myapp-folder 
+7
python django daemon celery
source share
4 answers

All answers here may be part of the solution, but in the end it still doesn't work. But I finally managed to get it to work.

First of all, in /etc/init.d/celeryd I changed this line:

 CELERYD_MULTI=${CELERYD_MULTI:-"celeryd-multi"} 

by:

 CELERYD_MULTI=${CELERYD_MULTI:-"celery multi"} 

The first was marked obsolete, it may be a problem.

In addition, I put this as an option: CELERYD_OPTS = "- application = MyApp"

And don't forget to export some environment variables:

 # Name of the projects settings module. export DJANGO_SETTINGS_MODULE="myapp.settings" export PYTHONPATH="$PYTHONPATH:/home/ubuntu/myapp-folder" 

With all this, he now works on my side.

+5
source share

It is likely that celeryd cannot find your Django settings file, since myapp.settings not in $PYTHONPATH , then the application starts.

From what I remember, Python will look in $PYTHONPATH , as well as in a local folder when importing files. When celeryd is celeryd , it most likely checks the path for the app module, doesn't find it, and then looks at the current folder for the app folder using __init__.py (i.e. the python module).

I think all you have to do is add /etc/default/celeryd to your file:

 export $PYTHONPATH:path/to/your/app 
+4
source share

There is no method below that helps to start celeryd, but helps to work with celery as a service that will be launched at boot time.

commands like this sudo service celery status also work.

celery.conf

 # This file sits in /etc/init description "Celery for example" start on runlevel [2345] stop on runlevel [!2345] #Send KILL after 10 seconds kill timeout 10 script #project(working_ecm) and Vitrualenv(working_ecm/env) settings chdir /home/hemanth/working_ecm exec /home/hemanth/working_ecm/env/bin/python manage.py celery worker -B -c 2 -f /var/log/celery-ecm.log --loglevel=info >> /tmp/upstart-celery-job.log 2>&1 end script respawn 
+1
source share

In your second tutorial, they set the django_settings variable:

 export DJANGO_SETTINGS_MODULE="settings" 

This may be the reason that your settings are not found if they change to the directory "/ Home / ubuntu / MyApp folder /" Then you defined your application using "myapp" and then say that the settings are in "myapp.settings "

This may cause it to look for the settings file in

"/ main / ubuntu / MyApp folder / MyApp / MyApp / Settings"

So my suggestion is to remove "myapp". in the DJANGO_SETTINGS_MODULE variable and don't forget the quotation marks

+1
source share

All Articles