diff options
author | andy <andy@whiskeymedia.com> | 2011-07-17 15:43:46 -0700 |
---|---|---|
committer | andy <andy@whiskeymedia.com> | 2011-07-17 15:43:46 -0700 |
commit | 4c750ee8b7385ca19a82d15c8d5597f84cbf7961 (patch) | |
tree | 53b77afba73d5655bb652d2fc09315d6e0070436 /redis/client.py | |
parent | 2e185683e310513d4efdcf9ec212115383f03aff (diff) | |
download | redis-py-4c750ee8b7385ca19a82d15c8d5597f84cbf7961.tar.gz |
Added a `transaction` convenience method that eliminates boilerplate when
using pipelines while WATCHing variables.
Diffstat (limited to 'redis/client.py')
-rw-r--r-- | redis/client.py | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/redis/client.py b/redis/client.py index 9ab467f..1c245bf 100644 --- a/redis/client.py +++ b/redis/client.py @@ -203,6 +203,22 @@ class Redis(object): transaction, shard_hint) + def transaction(self, func, *watches, **kwargs): + """ + Convenience method for executing the callable `func` as a transaction + while watching all keys specified in `watches`. The 'func' callable + should expect a single arguement which is a Pipeline object. + """ + shard_hint = kwargs.pop('shard_hint', None) + with self.pipeline(True, shard_hint) as pipe: + while 1: + try: + pipe.watch(*watches) + func(pipe) + return pipe.execute() + except WatchError: + continue + def lock(self, name, timeout=None, sleep=0.1): """ Return a new Lock object using key ``name`` that mimics |