django芹菜计划任务django.core.exceptions.improperlyconfigured



我试图使用以下命令运行计划的任务:

celery -A Htweetprod2 beat

根据芹菜4.0文档,要启动该命令应起作用的计划任务,但是我遇到了此错误:

C:Usershisg316DesktopHtweetprod2>celery -A Htweetprod2 beat
Traceback (most recent call last):
   File "c:python27librunpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
   File "c:python27librunpy.py", line 72, in _run_code
exec code in run_globals
  File "C:Python27Scriptscelery.exe__main__.py", line 9, in <module>
  File "c:python27libsite-packagescelery__main__.py", line 14, in main
_main()
  File "c:python27libsite-packagescelerybincelery.py", line 326, in main
cmd.execute_from_commandline(argv)
  File "c:python27libsite-packagescelerybincelery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:python27libsite-packagescelerybinbase.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
  File "c:python27libsite-packagescelerybincelery.py", line 480, in handle_argv
return self.execute(command, argv)
  File "c:python27libsite-packagescelerybincelery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "c:python27libsite-packagescelerybinbase.py", line 285, in run_from_argv
sys.argv if argv is None else argv, command)
  File "c:python27libsite-packagescelerybinbase.py", line 367, in handle_argv
*self.parse_options(prog_name, argv, command))
  File "c:python27libsite-packagescelerybinbase.py", line 403, in parse_options
self.parser = self.create_parser(prog_name, command)
  File "c:python27libsite-packagescelerybinbase.py", line 419, in create_parser
self.add_arguments(parser)
  File "c:python27libsite-packagescelerybinbeat.py", line 114, in add_arguments
'-s', '--schedule', default=c.beat_schedule_filename)
  File "c:python27libsite-packagesceleryutilscollections.py", line 130, in __getattr__
return self[k]
  File "c:python27libsite-packagesceleryutilscollections.py", line 431, in __getitem__
return getitem(k)
  File "c:python27libsite-packagesceleryutilscollections.py", line 280, in __getitem__
return mapping[_key]
  File "c:python27libUserDict.py", line 36, in __getitem__
if key in self.data:
  File "c:python27libsite-packageskombuutilsobjects.py", line 44, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
  File "c:python27libsite-packagesceleryappbase.py", line 148, in data
return self.callback()
  File "c:python27libsite-packagesceleryappbase.py", line 910, in _finalize_pending_conf
conf = self._conf = self._load_config()
  File "c:python27libsite-packagesceleryappbase.py", line 920, in _load_config
self.loader.config_from_object(self._config_source)
  File "c:python27libsite-packagesceleryloadersbase.py", line 133, in config_from_object
self._conf = force_mapping(obj)
  File "c:python27libsite-packagesceleryutilscollections.py", line 52, in force_mapping
return DictAttribute(m) if not isinstance(m, Mapping) else m
  File "c:python27libabc.py", line 131, in __instancecheck__
subclass = getattr(instance, '__class__', None)
  File "c:python27libsite-packagesdjangoutilsfunctional.py", line 234, in inner
self._setup()
  File "c:python27libsite-packagesdjangoconf__init__.py", line 39, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested settings, but settings are not configured. You must either define the environment variable 
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

我已经仔细遵循文档中的说明,并将设置配置为正确的应用程序。我想念什么吗?

这是我的tasks.py

from celery import Celery
from Htweets2.models import Htweets2
from celery.schedules import crontab
app = Celery('Htweetprod2')
@app.task
def delete_tweets():
    oldtweets = Htweets2.objects.all()
    oldtweets.delete()
CELERYBEAT_SCHEDULE = {
    "delete_tweeets_eachday": {
        'task': "tasks.delete_tweets",
        # Every 1 hour
        'schedule': crontab(hour=1, minute=0),
        'args': (16, 16),
    },
}

在将django与芹菜一起使用时,最好的做法是由Django的设置文件配置您的芹菜应用程序。

http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
app = Celery('proj')
# Using a string here means the worker don't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

这使用app.config_from_object(),还设置了您的项目,以自动从Django安装应用程序中的tasks.py文件中发现任务。

错误消息明确指出:

django.core.exceptions.improperlyconfigured:请求的设置,但未配置设置。您必须定义环境变量 django_settings_module或在访问设置之前调用settings.configure((。

这意味着您要么必须定义一个称为DJANGO_SETTINGS_MODULE的环境变量,要么在访问设置之前在代码中调用方法settings.configure

我建议设置环境变量DJANGO_SETTINGS_MODULE

文档:https://docs.djangoproject.com/en/1.11/topics/settings/#designating-the-settings

相关内容

  • 没有找到相关文章

最新更新