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

2024-09-26 18:03:44 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,管道超时是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',)

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


Tags: key代码nameredisclienthostdb管道
2条回答

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

详细信息请参见:https://docs.python.org/2/library/socket.html#socket.socket.settimeout

我问andymccurdy,redis py的作者,关于github和the answer如下:

If you're using redis-py<=2.9.1, socket_timeout is both the timeout for socket connection and the timeout for reading/writing to the socket. I pushed a change recently (465e74d) that introduces a new option, socket_connect_timeout. This allows you to specify different timeout values for socket.connect() differently from socket.send/socket.recv(). This change will be included in 2.10 which is set to be released later this week.

redis py版本是2.6.7,所以它既是套接字连接的超时,也是对套接字的读写超时。

相关问题 更多 >

    热门问题