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

hup*_*xue 8 python pipeline response redis 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()
Run Code Online (Sandbox Code Playgroud)

在redis中,set"key"中有很多成员.它始终返回错误,如下所示:

error Error while reading from socket: ('timed out',)
Run Code Online (Sandbox Code Playgroud)

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

hup*_*xue 21

我在github上问了redis-py的作者andymccurdy,答案如下:

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

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


小智 3

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

有关详细信息,请参阅此处:https://docs.python.org/2/library/socket.html#socket.socket.settimeout