如何设置redis超时等待redis-py中的管道响应?

时间:2014-05-29 09:32:44

标签: python redis response pipeline redis-py

在下面的代码中,管道超时是2秒吗?

client = redis.StrictRedis(host=host, port=port, db=0, socket_timeout=2)
pipe = client.pipeline(transaction=False)
for name in namelist:
    key = "%s-%s-%s-%s" % (key_sub1, key_sub2, name, key_sub3)
    pipe.smembers(key)
pipe.execute()

在redis中,“key”中有很多成员。它始终返回错误,如下所示:最后一段代码:

error Error while reading from socket: ('timed out',)

如果我将socket_timeout值修改为10,则返回ok 参数“socket_timeout”不是指连接超时吗?但它看起来像响应超时。
redis-py版本是2.6.7。

2 个答案:

答案 0 :(得分:21)

我在github上问了redis-py的作者andymccurdythe answer如下:

  

如果您正在使用redis-py< = 2.9.1,则socket_timeout都是超时   用于套接字连接和读取/写入的超时   插座。我最近推出了一个改变(465e74d),引入了一个新的   选项,socket_connect_timeout。这允许您指定不同的   socket.connect()的超时值与   socket.send / socket.recv()。此更改将包含在2.10中   将于本周晚些时候发布。

redis-py版本是2.6.7,因此它既是套接字连接的超时,也是读取/写入套接字的超时。

答案 1 :(得分:1)

它不是连接超时,而是操作超时。在内部,StrictRedis()上的socket_timeout参数将传递给套接字的settimeout方法。

详情请见此处:https://docs.python.org/2/library/socket.html#socket.socket.settimeout