What are equivalent functions of MULTI and EXEC commands in redis-py?
12,441
In redis-py MULTI and EXEC can only be used through a Pipeline object.
Try the following:
r = redis.Redis()
p = r.pipeline()
p.set("transError", var)
p.execute()
With the monitor
command through the redis-cli
you can see MULTI, SET, EXEC sent when p.execute()
is called.
To omit the MULTI/EXEC pair, use r.pipeline(transaction=False)
.
Related videos on Youtube
Author by
Ahsanul Haque
Curiosity is my specialty. I love to err, search and learn.
Updated on July 06, 2022Comments
-
Ahsanul Haque almost 2 years
I tested all the transaction commands (MULTI, EXEC, WATCH, DISCARD) in redis-cli. But when i tried with redis-py the following error occurred:
AttributeError: 'Redis' object has no attribute 'multi'
I have tried the following code snippet:
import redis,time r = redis.Redis() try: r.set("transError",10) r.watch("transError") var = r.get("transError") var = int(var) + 1 print "Run other client to simulate an error without transaction" time.sleep(4) r.multi() r.set("transError",var) r.execute() print "Value in first client",r.get("transError") except redis.WatchError: print "Value Altered"
I have seen code example that is using multi() and execute() but they are not working for me. Any help?
-
Tushar J Dudhatra over 6 yearsThis answer looks perfect. Though I have one question: How many commands can be efficiently executed together ? is it okay if I pass 500000 commands in one pipeline OR it will be too much for redis ?
-
ntki over 6 years@TusharJDudhatra You really have to measure that for yourself for your individual application. In my experience though it is usually preferable to batch only a sane number of commands together... like 10000 of them. Also in redis-py the pipeline sends all commands at once when you execute it, not continuously, regardless of transaction=False. See: redis.io/topics/pipelining github.com/schlitzered/pyredis/issues/2
-
Tushar J Dudhatra over 6 yearsThank you for your response. Yes I tried executing 500K commands in one pipeline and it went smoothly for me.
-
Oleg Yablokov over 2 yearsWhat if I need to perform a read request? I try to atomically increment a value in a json which is stored as a string in Redis. Is it possible? Btw I cannot use RedisJSON as my vendor doesn't support it yet.
-
Oleg Yablokov over 2 yearsI found a solution: just store the value I want to increment in a different key! :)