我有一个django 1.11.4,python3.5,gunicorn 19.7.1服务器带有主管,redis 2.10.6和芹菜4.1.0。
我的文件结构就像:
samy_python
├── celery.py
├── __init__.py
├── settings
├── urls.py
├── wsgi.py
website
├── static
├── templates
├── samy
| ├── samy_firebase.py
| ├── tasks.py
├── views.py
我得到了我的秘密:
# settings.py
SECRET_KEY = os.environ.get("secret_KEY")
我的芹菜主管conf文件是:
[program:gunicorn]
....
environment = secret_KEY="12345"
.....
[program:celery]
directory=/home/username/Projects/samy/samy_python
command=/home/username/Projects/samy/samy_python/env/bin/celery --app=samy_python.celery.app worker -B -l info
user=username
stdout_logfile=/var/log/celery/celery.log
stderr_logfile=/var/log/celery/celery.log
autostart=false
autorestart=false
startsecs=10
我的芹菜文件:
# celery.py
from __future__ import absolute_import
import os
import logging
logger = logging.getLogger('myapp.celery.py')
from celery import Celery
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "samy_python.settings")
# Celery App
# TODO
# [START Celery App]
app = Celery('samy_python')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
# [END Celery App]
我的初始文件:
# __init__.py
from __future__ import absolute_import
from .celery import app as celery_app
我的设置中的配置。芹菜的py文件:
BROKER_URL = 'redis://localhost:6379/1'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
可能来自我的设置中的错误来自我的设置。Py文件我使用Env变量来设置环境。(生产,开发或测试):
DEBUG = False
DJANGO_TESTING = False
DJANGO_DEVELOPMENT = False
DJANGO_ENV = os.environ.get('DJANGO_ENV')
if DJANGO_ENV == 'DJANGO_TESTING':
DJANGO_TESTING = True
elif DJANGO_ENV == 'DJANGO_DEVELOPMENT':
DJANGO_DEVELOPMENT = True
然后,我设置了Firebase凭据和Google API凭据。然后,我从内部获得凭据,例如samy_firebase.py with:
from django.conf import settings
if settings.DJANGO_TESTING == True:
logger.info('Firebase Config: Test DB')
我不确定最后一部分是否相关,但这是我一直收到的错误:
Traceback (most recent call last):
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/kombu/utils/objects.py", line 42, in __get__
return obj.__dict__[self.__name__]
KeyError: 'data'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/dorian/Projects/samy/samy_python/env/bin/celery", line 11, in <module>
sys.exit(main())
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/__main__.py", line 14, in main
_main()
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 326, in main
cmd.execute_from_commandline(argv)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 480, in handle_argv
return self.execute(command, argv)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/celery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/worker.py", line 219, in run_from_argv
*self.parse_options(prog_name, argv, command))
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 403, in parse_options
self.parser = self.create_parser(prog_name, command)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/base.py", line 419, in create_parser
self.add_arguments(parser)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/bin/worker.py", line 275, in add_arguments
default=conf.worker_state_db,
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 130, in __getattr__
return self[k]
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 431, in __getitem__
return getitem(k)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 280, in __getitem__
return mapping[_key]
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/collections/__init__.py", line 982, in __getitem__
if key in self.data:
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/kombu/utils/objects.py", line 44, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 148, in data
return self.callback()
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 911, in _finalize_pending_conf
conf = self._conf = self._load_config()
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/app/base.py", line 921, in _load_config
self.loader.config_from_object(self._config_source)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/loaders/base.py", line 133, in config_from_object
self._conf = force_mapping(obj)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/celery/utils/collections.py", line 50, in force_mapping
if isinstance(m, (LazyObject, LazySettings)):
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/utils/functional.py", line 238, in inner
self._setup()
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/dorian/Projects/samy/samy_python/env/lib/python3.5/site-packages/django/conf/__init__.py", line 129, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
此外,枪师的工作正常,因此如果我不启动芹菜程序,它会接收环境变量。另外,如果我将键设置为settings.py文件中的" 12345",则启动。
在Env中设置Secret_key:
export SECRET_KEY="#mysecret_key"
和检查:
echo $SECRET_KEY
以及在您的Django设置中,更改为:
SECRET_KEY = os.environ.get("SECRET_KEY")
有关详细参考检查:读取和设置环境变量
将环境变量添加到主管conf文件的[program:celery]
部分。目前,它仅设置为gunicorn
。