我有一个Django应用程序在linux机器(Debian)上运行。在过去的一段时间里,这个应用程序运行得很好。最近,我需要重新启动机器,因为芹菜任务开始挂起,清除任务没有达到预期的效果。当我现在尝试使用
启动芹菜时sudo celery -A myapp.tasks worker -Ofair
我看到了下面的回溯
Traceback (most recent call last):
File "/usr/local/bin/celery", line 9, in <module>
load_entry_point('celery==3.1.11', 'console_scripts', 'celery')()
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
main()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 304, in execute_from_commandline
argv = self.setup_app_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 464, in setup_app_from_commandline
self.app = self.find_app(app)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 484, in find_app
return find_app(app, symbol_by_name=self.symbol_by_name)
File "/usr/local/lib/python2.7/dist-packages/celery/app/utils.py", line 225, in find_app
sym = imp(app)
File "/usr/local/lib/python2.7/dist-packages/celery/utils/imports.py", line 101, in import_from_cwd
return imp(module, package=package)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/this/gce_app/myapp/tasks.py", line 8, in <module>
from django.views.decorators.csrf import csrf_exempt
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 1, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
File "/usr/local/lib/python2.7/dist-packages/django/middleware/csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "/usr/local/lib/python2.7/dist-packages/django/utils/cache.py", line 26, in <module>
from django.core.cache import get_cache
File "/usr/local/lib/python2.7/dist-packages/django/core/cache/__init__.py", line 69, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must
either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing set
tings.
我已经添加了导出到bashrc(在最后),这在过去解决了这个问题,但现在我仍然提出了相同的错误:
sudo nano ~/.bashrc
export DJANGO_SETTINGS_MODULE='myproject.settings'
有办法让它再次工作吗?
您使用sudo启动芹菜,这意味着您是作为根用户运行的,而不是您为其设置环境变量的用户。不要这样做——绝对没有好的理由用sudo来运行守护进程。
(与您的问题无关,但您也不应该将bashrc编辑为sudo)