Django芹菜击败属性错误:' NoneType'对象没有属性' localize'

时间:2018-02-24 14:00:42

标签: python django redis celery

我按照this tutorial尝试使用我的Django项目运行定期任务。

使用以下命令运行芹菜时:

celery -A proj beat -l info -S django

我收到以下错误:

celery beat v4.0.2 (latentcall) is starting.
    __    -    ... __   -        _
    LocalTime -> 2018-02-24 13:42:37
    Configuration ->
        . broker -> redis://localhost:6379//
        . loader -> celery.loaders.app.AppLoader
        . scheduler -> django_celery_beat.schedulers.DatabaseScheduler
        . logfile -> [stderr]@%INFO
        . maxinterval -> 5.00 seconds (5s)
    [2018-02-24 13:42:37,244: INFO/MainProcess] beat: Starting...
    [2018-02-24 13:42:37,245: INFO/MainProcess] Writing entries...
    [2018-02-24 13:42:37,255: CRITICAL/MainProcess] beat raised exception <type 'exceptions.AttributeError'>: AttributeError("'NoneType' object has no attribute 'localize'",)
    Traceback (most recent call last):
      File "...lib/python2.7/site-packages/celery/apps/beat.py", line 107, in start_scheduler
        service.start()
      File "...lib/python2.7/site-packages/celery/beat.py", line 528, in start
        humanize_seconds(self.scheduler.max_interval))
      File "...lib/python2.7/site-packages/kombu/utils/objects.py", line 44, in __get__
        value = obj.__dict__[self.__name__] = self.__get(obj)
      File "...lib/python2.7/site-packages/celery/beat.py", line 572, in scheduler
        return self.get_scheduler()
      File "...lib/python2.7/site-packages/celery/beat.py", line 567, in get_scheduler
        lazy=lazy,
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 183, in __init__
        Scheduler.__init__(self, *args, **kwargs)
      File "...lib/python2.7/site-packages/celery/beat.py", line 204, in __init__
        self.setup_schedule()
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 191, in setup_schedule
        self.install_default_entries(self.schedule)
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 290, in schedule
        self._schedule = self.all_as_schedule()
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 199, in all_as_schedule
        s[model.name] = self.Entry(model, app=self.app)
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 88, in __init__
        model.last_run_at = self._default_now()
      File "...lib/python2.7/site-packages/django_celery_beat/schedulers.py", line 106, in _default_now
        return now.tzinfo.localize(now.replace(tzinfo=None))
    AttributeError: 'NoneType' object has no attribute 'localize'

研究其他StackOverflow帖子表明某个对象没有通过导致NoneType属性错误的地方传递。我怀疑这个问题可能与我的嵌套local.py和production.py设置模块有关,这些模块与Celery对项目应用程序目录中更典型的设置模块的期望不同。

以下是我的项目结构和代码。我的virtualenv已启用,所有依赖项已安装并正在运行。任何帮助或建议将不胜感激。

目录结构:

├── myproject
│   ├── myproject
│   │   ├── __init__.py
│   │   ├── settings
│   │   │   ├── __init__.py
│   │   │   ├── base.py
│   │   │   ├── celery.py
│   │   │   ├── local.py
│   │   │   ├── production.py
│   ├── manage.py

的myproject / myproject的/设置/ __初始化__。PY

# myproject/myproject/settings/__init__.py

from __future__ import absolute_import, unicode_literals

from .base import *

try:
    from .local import *
    live = False
except: 
    live = True

if live:
    from .production import *

的myproject / myproject的/ __初始化__。PY

# myproject/myproject/__init__.py

from __future__ import absolute_import

from myproject.settings.celery import app as celery_app 

celery.py

# myproject/myproject/settings/celery.py

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', 'myproject.settings.local')

app = Celery('myproject')

# 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.task(bind=True)
def debug_task(self):
  print('Request: {0!r}'.format(self.request))



from celery.schedules import crontab
app.conf.beat_schedule = {
    'add-every-minute-crontab': {
        'task': 'fetch_news',
        'schedule': crontab()
    }
}

1 个答案:

答案 0 :(得分:0)

我通过添加设置成功克服了这个问题:

DJANGO_CELERY_BEAT_TZ_AWARE=False

然后,我有一个例外

kombu.exceptions.VersionMismatch: Redis transport requires redis-py versions

此问题由https://github.com/celery/celery/issues/5369#issuecomment-469302960

解决

我还注意到django-celery-beat 1.1.0版没有此特定问题,但是它将使用crontabs发送信号。因此,您应该使用更高的版本。