报错(AttributeError: 'str' object has no attribute 'items')的解决办法

在启动Celery或启动后调用Celery实现异步任务的时候报错:AttributeError: ‘str’ object has no attribute ‘items’,经过百度一番,发现是redis版本过高引起的,以下进行报错的展示与解决。

一、报错演示

[2019-12-11 20:17:43,836: ERROR/MainProcess] Unrecoverable error: AttributeError("'str' object has no attribute 'items'",)
Traceback (most recent call last):
  File "/home/celery/venv/lib/python3.6/site-packages/celery/worker/__init__.py", line 206, in start
    self.blueprint.start(self)
  File "/home/celery/venv/lib/python3.6/site-packages/celery/bootsteps.py", line 123, in start
    step.start(parent)
  File "/home/celery/venv/lib/python3.6/site-packages/celery/bootsteps.py", line 374, in start
    return self.obj.start()
  File "/home/celery/venv/lib/python3.6/site-packages/celery/worker/consumer.py", line 280, in start
    blueprint.start(self)
  File "/home/celery/venv/lib/python3.6/site-packages/celery/bootsteps.py", line 123, in start
    step.start(parent)
  File "/home/celery/venv/lib/python3.6/site-packages/celery/worker/consumer.py", line 884, in start
    c.loop(*c.loop_args())
  File "/home/celery/venv/lib/python3.6/site-packages/celery/worker/loops.py", line 76, in asynloop
    next(loop)
  File "/home/celery/venv/lib/python3.6/site-packages/kombu/async/hub.py", line 340, in create_loop
    cb(*cbargs)
  File "/home/celery/venv/lib/python3.6/site-packages/kombu/transport/redis.py", line 1019, in on_readable
    self._callbacks[queue](message)
  File "/home/celery/venv/lib/python3.6/site-packages/kombu/transport/virtual/__init__.py", line 534, in _callback
    self.qos.append(message, message.delivery_tag)
  File "/home/celery/venv/lib/python3.6/site-packages/kombu/transport/redis.py", line 146, in append
    pipe.zadd(self.unacked_index_key, delivery_tag, time()) \
  File "/home/celery/venv/lib/python3.6/site-packages/redis/client.py", line 2388, in zadd
    for pair in iteritems(mapping):
  File "/home/celery/venv/lib/python3.6/site-packages/redis/_compat.py", line 161, in iteritems
    return iter(x.items())
AttributeError: 'str' object has no attribute 'items'

二、问题解决

导致报错的原因是redis版本过高,对redis进行降级

  1. 卸载原有redis

    pip uninstall redis
    
  2. 重新安装redis
    版本号可根据自己的项目进行改动

    pip install redis==2.10.6
    

欢迎留言吐槽

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页