Do something like Redis transactions in Python

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

Do something like Redis transactions in Python
Something like JS setTimeout in python
Something like tail -f in Python
I want to do something like sort uniq in Python
Do something like a Python interpreter in Visual Studio Code
Try something like Python for-else in Ruby
I wanted to do something like an Elixir pipe in Python
Implement Redis Mutex in Python
Redis pipe lining in Python
I want to do something in Python when I finish
[Python] How to do PCA in Python
Do something object-oriented in GO language
Expand a Python nested dictionary to do something like Pandas' MultiIndex
Something like 40-32 / 2 = 4!
Do a non-recursive Euler Tour in Python
How to do R chartr () in Python
Find files like find on linux in Python
Do you make something like a rocket?
#I tried something like Vlookup with Python # 2
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
Quadtree in Python --2
Python in optimization
CURL in python
Convert Unicode escape sequences like \ u in Python
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Minimal implementation to do Union Find in Python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
FizzBuzz in Python
Sqlite in python
StepAIC in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
What to do to get google spreadsheet in python
Puyo Puyo in python
python in virtualenv
PPAP in Python
Redis Shortest Introduction (3) API in 5 minutes even in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python