Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to WATCH multiple Redis KEYs in python?

Tags:

python

redis

I've been playing with Redis lately, wondering how to accomplish watching multiple keys at once. Would something like below be atomic?

Following code uses redis-py;

 while True:            
        try:
            pipe.watch(key)
            pipe.watch(another_key)
            pipe.multi()
            pipe.set(key, value)
            pipe.set(another_key, another_value)
            pipe.execute()

            break
        except redis.WatchError:
            continue

        finally:
            pipe.reset()
like image 819
xzvkm Avatar asked Mar 19 '13 15:03

xzvkm


1 Answers

redis has support for multiple keys, yes: http://redis.io/commands/watch

although the docs for the python client say that pipelined commands are executed atomically, I would still use a single WATCH call with multiple arguments:

pipe.watch(key, another_key)
like image 180
akonsu Avatar answered Oct 26 '22 14:10

akonsu