Celery with multiple django sites

馋奶兔 提交于 2020-01-16 20:02:05

问题


I have a one django backend for few customer's sites:

my_proj
    |- my_proj
        |- __init__.py
        |- settings.py
        |- settings_development.py
        |- settings_production_1.py
        |- settings_production_2.py
        |- settings_production_3.py
    |- my_app_1
    |- my_app_2
    ...

settings_production_1.py:

from settings import *

DEBUG = False
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'customer_1_db',
        'USER': 'some_user',
        'PASSWORD': 'some_passw',
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}
MEDIA_ROOT = 'media/customer_1'

Each site is a separate proccess managed by supervisord and uses separate database. Also I have a redis on a separate server.

I need some celery background tasks with database access.

How can I do that?

UPDATE

Ok, I can run multiple celery workers. I can do that from console

$ export DJANGO_SETTINGS_MODULE=my_proj.settings_production_2
$ /home/.../my_vitrual_env/bin/celery -A my_proj worker -l info

But I can't run it from supervisord

[program:celery2]
directory=/home/.../my_proj
command=/home/.../my_vitrual_env/bin/celery -A asl worker -l info
environment=DJANGO_SETTINGS_MODULE=my_proj.settings_production_2
...

回答1:


You configure an app for your project . e.g.:


    my_proj
      | - my_proj
        |- __init__.py
        |- celery.py
        |- settings.py
        |- settings_development.py
        |- settings_production_1.py
        |- settings_production_2.py
        |- settings_production_3.py

etc.

In the celery.py you configure the celery app from the appropriate settings object by setting the DJANGO_SETTINGS_MODULE env variable and use that to load the appropriate settings.

And then in supervisord, you give each site its own celery by specifying the start line as celery multi -A my_proj and with the correct DJANGO_SETTINGS_MODULE env variable.



来源:https://stackoverflow.com/questions/53041596/celery-with-multiple-django-sites

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!