Django Celery 计划任务 django.core.exceptions.ImproperlyConfigured

Posted

技术标签:

【中文标题】Django Celery 计划任务 django.core.exceptions.ImproperlyConfigured【英文标题】:Django Celery scheduled task django.core.exceptions.ImproperlyConfigured 【发布时间】:2017-09-22 14:30:21 【问题描述】:

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

celery -A Htweetprod2 beat

根据 Celery 4.0 文档启动计划任务,此命令应该可以工作,但我收到此错误:

C:\Users\hisg316\Desktop\Htweetprod2>celery -A Htweetprod2 beat
Traceback (most recent call last):
   File "c:\python27\lib\runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
   File "c:\python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
  File "C:\Python27\Scripts\celery.exe\__main__.py", line 9, in <module>
  File "c:\python27\lib\site-packages\celery\__main__.py", line 14, in main
_main()
  File "c:\python27\lib\site-packages\celery\bin\celery.py", line 326, in main
cmd.execute_from_commandline(argv)
  File "c:\python27\lib\site-packages\celery\bin\celery.py", line 488, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
  File "c:\python27\lib\site-packages\celery\bin\base.py", line 281, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
  File "c:\python27\lib\site-packages\celery\bin\celery.py", line 480, in handle_argv
return self.execute(command, argv)
  File "c:\python27\lib\site-packages\celery\bin\celery.py", line 412, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
  File "c:\python27\lib\site-packages\celery\bin\base.py", line 285, in run_from_argv
sys.argv if argv is None else argv, command)
  File "c:\python27\lib\site-packages\celery\bin\base.py", line 367, in handle_argv
*self.parse_options(prog_name, argv, command))
  File "c:\python27\lib\site-packages\celery\bin\base.py", line 403, in parse_options
self.parser = self.create_parser(prog_name, command)
  File "c:\python27\lib\site-packages\celery\bin\base.py", line 419, in create_parser
self.add_arguments(parser)
  File "c:\python27\lib\site-packages\celery\bin\beat.py", line 114, in add_arguments
'-s', '--schedule', default=c.beat_schedule_filename)
  File "c:\python27\lib\site-packages\celery\utils\collections.py", line 130, in __getattr__
return self[k]
  File "c:\python27\lib\site-packages\celery\utils\collections.py", line 431, in __getitem__
return getitem(k)
  File "c:\python27\lib\site-packages\celery\utils\collections.py", line 280, in __getitem__
return mapping[_key]
  File "c:\python27\lib\UserDict.py", line 36, in __getitem__
if key in self.data:
  File "c:\python27\lib\site-packages\kombu\utils\objects.py", line 44, in __get__
value = obj.__dict__[self.__name__] = self.__get(obj)
  File "c:\python27\lib\site-packages\celery\app\base.py", line 148, in data
return self.callback()
  File "c:\python27\lib\site-packages\celery\app\base.py", line 910, in _finalize_pending_conf
conf = self._conf = self._load_config()
  File "c:\python27\lib\site-packages\celery\app\base.py", line 920, in _load_config
self.loader.config_from_object(self._config_source)
  File "c:\python27\lib\site-packages\celery\loaders\base.py", line 133, in config_from_object
self._conf = force_mapping(obj)
  File "c:\python27\lib\site-packages\celery\utils\collections.py", line 52, in force_mapping
return DictAttribute(m) if not isinstance(m, Mapping) else m
  File "c:\python27\lib\abc.py", line 131, in __instancecheck__
subclass = getattr(instance, '__class__', None)
  File "c:\python27\lib\site-packages\django\utils\functional.py", line 234, in inner
self._setup()
  File "c:\python27\lib\site-packages\django\conf\__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),
    ,

【问题讨论】:

【参考方案1】:

错误信息明确指出:

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

【讨论】:

在解决方案方面:这种方式为我做到了:import sys, os, django sys.path.append("/home//Documents/Htweetprod2") os.environ.setdefault("DJANGO_SETTINGS_MODULE ", "Htweetprod2.settings") django.setup()【参考方案2】:

将 Django 与 Celery 一起使用时的最佳做法是让您的 Celery 应用程序由 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 给我一个参数错误,期望 1 给定 2 在 app.config_from_object() 方面 上面的示例代码有 1 个位置参数和 1 个关键字参数。听起来您正在传递 2 个位置参数。

以上是关于Django Celery 计划任务 django.core.exceptions.ImproperlyConfigured的主要内容,如果未能解决你的问题,请参考以下文章

Celery学习---Celery 与django结合实现计划任务功能

不同服务器上的 Django 和 celery,一旦任务完成,celery 能够向 django 发送回调

Django Celery 获取任务计数

如何在任务中获取芹菜结果模型(使用 django-celery-results)

django+django-celery+celery的整合实战

如何使用 Celery 和 Django 将任务路由到不同的队列