python - Django celery beat 属性错误 : 'NoneType' object has no attribute 'localize'

标签 python django redis celery

我正在尝试按照 this tutorial 使用我的 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 *

我的项目/myproject/__init__.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()
    }
}

最佳答案

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

DJANGO_CELERY_BEAT_TZ_AWARE=False

然后,我得到了一个异常(exception)
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 发送信号。所以你应该使用更高的版本。

关于python - Django celery beat 属性错误 : 'NoneType' object has no attribute 'localize' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963659/

相关文章:

python - 在 RTSP 设置后接收 RTP 数据包

python - WSGI 中间件可以修改请求体然后传递它吗?

python - 如何在 html 中创建搜索部分并在 django 中使用多个查询创建搜索 View

redis - StackExchange Redis 的分区键空间

Python 类型错误 : can't multiply sequence by non-int of type 'float'

python - Django Rest 嵌套序列化器的 AttributeError

django - 如何同时使用 WordPress 和 Django

spring - 获取 RedisConnectionFailureException

playframework - 适用于 Java Play 2.x 的 Redis?

python - torch torch .load ModuleNotFoundError : No module named 'utils'