redis pipeline

redis-py default execution every request to create ( connection pool application connection ) and off ( return connection pool ) a connecting operation,

If you want to specify multiple commands in a single request , you can use the pipline achieve a request to specify multiple commands,

And the default time pipline is atomicity operations.

import redis
 
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
 
r = redis.Redis(connection_pool=pool)
 
# pipe = r.pipeline(transaction=False)
pipe = r.pipeline(transaction=True)
pipe.multi()
pipe.set('name', 'alex')
pipe.set('role', 'sb')
 
pipe.execute()

own:

# Duct if you want to specify a plurality of command request 
Import redis 

# 1. Create a connection pool redis 
the pool redis.ConnectionPool = (Host = ' 127.0.0.1 ' , Port = 6379 ) 

# 2. Each instance will maintain Redis its own connection pool. 
redis.Redis = R & lt (= connection_pool the pool) 

# create a conduit 
pipe r.pipeline = (Transaction = True) 

# can execute a plurality of specified 
pipe.multi () 

# in the setting value pip 
pipe.set ( ' name ' , ' Riven ' ) 
pipe.set ( ' Role ' , ' Mark ' )


# Perform PIP 
pipe.execute ()

Implement counter

# Duct if you want to specify a plurality of command request 
Import redis 

# 1. Create a connection pool redis 
the pool redis.ConnectionPool = (Host = ' 127.0.0.1 ' , Port = 6379 ) 

# 2. Each instance will maintain Redis its own connection pool. 
redis.Redis = r (= connection_pool the pool) 

r.set ( ' COUNT ' , 1000 ) 


with r.pipeline () AS pipe: 

    # first monitor, their value has not been modified 
    r.watch ( ' COUNT ' ) 

    # Affairs start 
    pipe.multi () 

    # get set to the start value COUNT 
    old_count = r.get ( ' COUNT ' ) 

    #The count into a digital 
    count = int (old_count)
     IF count> 0:   # stock 
        pipe.set ( ' count ' , count - 1 ) 

    # execution, command all-time push past 
    pipe.execute ()

Publish and subscribe

 

 

Publisher: server

Subscribers: Dashboad and data processing

Demo as follows:

import redis


class RedisHelper:

    def __init__(self):
        self.__conn = redis.Redis(host='10.211.55.4')
        self.chan_sub = 'fm104.5'
        self.chan_pub = 'fm104.5'

    def public(self, msg):
        self.__conn.publish(self.chan_pub, msg)
        return True

    def subscribe(self):
        pub = self.__conn.pubsub()
        pub.subscribe(self.chan_sub)
        pub.parse_response()
        return pub

subscriber:

from Reade_test.RedisHelper_file Import RedisHelper 

# subscribers 
#    instantiate a class 
obj = RedisHelper () 

# call to subscribe to get information publisher sent 
redis_sub = obj.subscribe () 

# loop to get the value of the sender 
the while True:
     # parse_response "resolved response from the publish / subscribe command " 
    msg = redis_sub.parse_response ()
     # print publisher's 
    Print (msg)

announcer:

# 发布者
from Reade_test.RedisHelper_file import RedisHelper
obj = RedisHelper()

obj.public('mark')

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/Rivend/p/12021885.html