【发布时间】:2016-09-24 12:14:27
【问题描述】:
我使用以下代码在我的 Redis 队列中放置/检索项目,但有时在解码 json 转储时会出错,因为返回的项目不是元组而是完整的 json。
这是课程:
class RedisQueue(object):
"""Simple Queue with Redis Backend"""
def __init__(self, namespace, redis_url='redis://127.0.0.1:6379'):
self.__db = redis.from_url(redis_url)
self.redis_url = redis_url
self.namespace = namespace
def put(self, queue, item):
"""Put item into the queue."""
self.__db.rpush('{0}:{1}'.format(self.namespace, queue), json.dumps(item))
def get(self, queue, block=True, timeout=None):
"""Remove and return an item from the queue.
If optional args block is true and timeout is None (the default), block
if necessary until an item is available."""
key = '{0}:{1}'.format(self.namespace, queue)
if block:
item = self.__db.blpop(key, timeout=timeout)
else:
item = self.__db.lpop(key)
if item is not None:
try:
item = json.loads(item[1])
except ValueError as e:
sys.stderr.write("[ERROR JSON (in queue)] - {1} => {0}\n".format(str(e), str(item)))
return None
return item
我有时会在以下位置遇到异常:
if item is not None:
try:
item = json.loads(item[1])
except ValueError as e:
sys.stderr.write("[ERROR JSON (in queue)] - {1} => {0}\n".format(str(e), str(item)))
return None
也就是说:
[ERROR JSON (in queue)] - {"ip": null, "domain": "somedomain.com", "name": "Some user name", "contact_id": 12345, "signature":
"6f496a4eaba2c1ea4e371ea2c4951ad92f41ddf45ff4949ffa761b0648a22e38"} => end is out of bounds
这是因为 item 是完整的 json,所以 json.loads(item[₁]) 会导致错误。但它只是偶尔发生,而不是每次都发生。当我手动检查 item 的值时,我有一个元组,其中键为 0,值(json 字符串)为 1,这是预期的。
为什么redis有时会返回item中的值,有时返回带有key,value的元组?
【问题讨论】:
-
lpop总是只返回值。 -
ahahahhaaah ...但是..认真的吗?!为什么有区别?!
-
哦,如果你愿意,请解释差异作为答案,我会接受它:)