Skip to content

Instantly share code, notes, and snippets.

@seckcoder
Created June 14, 2013 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seckcoder/5782977 to your computer and use it in GitHub Desktop.
Save seckcoder/5782977 to your computer and use it in GitHub Desktop.
A demo for redis transation's strong exception guarantee.
"""
A demo for redis transation's strong exception guarantee.
"""
import logging
import redis
client = redis.StrictRedis(host="127.0.0.1",
port=6379,
db=1)
client.set("hello", 1)
def foo():
with client.pipeline() as pipe:
pipe.set("hello", 2)
raise Exception("An exception throwed")
def bar():
def transaction_func(pipe):
hello_value = int(pipe.get("hello"))
pipe.multi()
pipe.set("hello", hello_value+1)
raise Exception("Another exception throwed")
client.transaction(transaction_func, "hello")
try:
foo()
except Exception as e:
logging.exception(e)
print client.get("hello") # Note the value will not be changed since the operation failed. Redis is still in valid state
try:
bar()
except Exception as e:
logging.exception(e)
print client.get("hello") # this is the same
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment