How to set the redis timeout waiting for the response with pipeline in redis-py?

25,119

Solution 1

I asked andymccurdy , the author of redis-py, on github and the answer is as below:

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.

The redis-py version is 2.6.7, so it's both the timeout for socket connection and the timeout for reading/writing to the socket.

Solution 2

It is not connection timeout, it is operation timeout. Internally the socket_timeout argument on StrictRedis() will be passed to the socket's settimeout method.

See here for details: https://docs.python.org/2/library/socket.html#socket.socket.settimeout

Share:
25,119
hupantingxue
Author by

hupantingxue

Blog: http://giftbrandmaker.com/myblog github: http://github.com/hupantingxue

Updated on September 10, 2020

Comments

  • hupantingxue
    hupantingxue over 3 years

    In the code below, is the pipeline timeout 2 seconds?

    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()
    

    In the redis, there are a lot of members in the set "key". It always return the error as below with the code last:

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

    If I modify the socket_timeout value to 10, it returns ok.
    Doesn't the param "socket_timeout" mean connection timeout? But it looks like response timeout.
    The redis-py version is 2.6.7.

  • hupantingxue
    hupantingxue over 9 years
    It depends on the version of redis-py.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.