I have some problem to use Redis pipeline. I need to execute 2 commands (incr()
and set()
) by a Redis transaction.
Environment info: redis-py version 3.5.3; the version of the Redis server is v=5.0.5.
So I have tried to use the following code (the code example is reproducible):
import redis
r = redis.Redis()
pipe = r.pipeline()
def redis_insertion():
pipe.multi()
testIndex = pipe.incr('testIndex')
pipe.set('new_key:' + str(testIndex), 'new_value')
pipe.execute()
pipe.reset()
redis_insertion()
By Monitor I can see the real commands executed by the redis server:
> redis-cli monitor
OK
1684750490.821375 [0 127.0.0.1:47322] "MULTI"
1684750490.821394 [0 127.0.0.1:47322] "INCRBY" "testIndex" "1"
1684750490.821400 [0 127.0.0.1:47322] "SET" "new_key:Pipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>" "new_value"
1684750490.821411 [0 127.0.0.1:47322] "EXEC"
The problem is that the instruction pipe.set('new_key:' + str(testIndex), 'new_value')
return:
Pipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
instead of the value of the key testIndex
after the execution of the INCRBY
instruction.
Could someone explain me this behaviour?
2
Answers
By the answer of @Simon Prickett and especially thanks to his comment:
I have changed my code as below:
In this case the monitor shows:
The output of the monitor shows that in the Redis DB have been correctly inserted the keys:
"SET" "testIndex" "37"
)"SET" "new_key:37" "new_value"
)The
execute
function returns a list of command responses from Redis, so yourincr
response should be the first thing in that list.Example:
Output:
Versions: