簡體   English   中英

Django celery 擊敗 attributeError: 'NoneType' 對象沒有屬性 'localize'

[英]Django celery beat attributeError: 'NoneType' object has no attribute 'localize'

我正在嘗試按照本教程使用我的 Django 項目運行定期任務。

使用以下命令運行 celery beat 時:

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

我的項目/我的項目/設置/__init__.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 *

我的項目/我的項目/__init__.py

# myproject/myproject/__init__.py

from __future__ import absolute_import

from myproject.settings.celery import app as celery_app 

芹菜.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()
    }
}

我通過添加設置成功地克服了這個問題:

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的芹菜拍1.1.0版本沒有這個具體問題,但使用的crontab將不再發送信號。 所以你應該使用更高的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM