test_transaction.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
import redis
import time
from multiprocessing import Process
r = redis.StrictRedis(host='localhost', port=6379, db=0)
key = "abc"
def do(name, value):
with r.pipeline() as pipe:
try:
pipe.watch(key)
pipe.multi()
pipe.set(key, value)
time.sleep(1)
pipe.execute()
print "{} Success!!!".format(name)
except redis.exceptions.WatchError:
print "{} WatchError!!!".format(name)
p1 = Process(target=do, args=("p1", 1))
p1.start()
p2 = Process(target=do, args=("p2", 2))
p2.start()
time.sleep(3) #Wait until the process is finished
print r.get(key)
Execution result: % python test_redis.py p2 WatchError!!! p1 Success!!! 1
It feels like canceling the error. It's not as good to use as RDBS, so I feel like I can do it a little.
Recommended Posts