Use redis to publish and subscribe model in pythonpython development-implement the publish and subscribe function in redis

Publish subscriber model using redis in python

 

Redis publish subscriber model:

  Redis provides a publish and subscribe function, which can be used for message transmission. Redis' publish and subscribe mechanism includes three parts, publisher, subscriber and channel. Both the publisher and the subscriber are Redis clients, and the Channel is the Redis server. The publisher sends the message to a certain channel, and subscribers who subscribe to the channel can receive the message. Redis's publish and subscribe mechanism is similar to topic-based publish and subscribe, and Channel is equivalent to a topic.

            

announcer:

  pub.py

import redis

conn = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)


conn.publish("333", "18")

subscriber:

  sub.py

Import Redis 

conn = redis.Redis (Host = " 127.0.0.1 " , Port = 6379, decode_responses = True) 

# The first step in generating a subscriber object 
PubSub = conn.pubsub () 

# Step subscribe to a news 

pubsub.subscribe ( " gaoxin333 " ) 

# create a reception 

the while True:
     Print ( " Working ~ ~ ~ " ) 
    msg = pubsub.parse_response ()
     Print (msg)

 

(1) Sending a message 
Redis uses the PUBLISH command to send a message. The return value is the number of subscribers who received the message.

(2) Subscribe to a channel 

 

Redis uses the SUBSCRIBE command to subscribe to a channel, and its return value includes the channel that the client subscribed to, the number of channels currently subscribed, and the received message, where subscribe indicates that it has successfully subscribed to a channel.

(3) Pattern matching 

 

The pattern matching function allows clients to subscribe to channels that conform to a certain pattern. Redis uses PSUBSCRIBE to subscribe to all channels that conform to a certain pattern. "" Indicates the pattern, and "" can be replaced by any value. Assuming that the client subscribes to a certain mode and a channel that conforms to the mode at the same time, the message sent to this channel will be received twice by the client, but the types of these two messages are different, one is the message type, one It is pmessage type, but its content is the same. 

(4) Unsubscribe 
Redis uses the UNSUBSCRIBE and PUNSUBSCRIBE commands to cancel the subscription. The return value is similar to the subscription. 
Because Redis's subscription operation is blocking, once the client subscribes to a channel or mode, it will remain in the subscription state until it exits. In the SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands, the return value includes the number of channels and modes currently subscribed by the client. When this number becomes 0, the client will automatically exit the subscription status.

 

import redis
conn = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)

conn.publish("gaoxin333", "18")

---------------------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------

Python development-implement the publish and subscribe function in redis

 

Python3 learning (twenty-seven): Python realizes Redis subscription and publishing (sub-pub mechanism)

Pub / Sub function (means Publish, Subscribe) means publishing and subscribing functions. In an event-based system, Pub / Sub is a widely used communication model. It uses events as the basic communication mechanism to provide the loosely coupled interaction mode required by large-scale systems: subscribers (such as clients) subscribe to events It expresses an event or a type of event that it is interested in receiving; the publisher (such as a server) can notify the relevant subscriber of the event of interest to the subscriber at any time.

Generally speaking, it means that my sub-end (subscriber) has been listening. Once the pub-end (publisher) has published a message, then I will receive it. For example, the publisher:

Copy code
 
#coding:utf-8
import time
import redis
 
number_list = ['300033', '300032', '300031', '300030']
signal = ['1', '-1', '1', '-1']
 
rc = redis.StrictRedis(host='***', port='6379', db=3, password='********')
for i in range(len(number_list)):
    value_new = str(number_list[i]) + ' ' + str(signal[i])
    rc.publish("liao", value_new)  #发布消息到liao
 
Copy code

 

Then we take a look at the subscribers:

 

Copy code
 
#coding: utf-8 
import time 
import redis 
 
rc = redis.StrictRedis (host = '****', port = '6379', db = 3, password = '******') 
ps = rc. pubsub () 
ps.subscribe ('liao') 
#Subscribe message from liao for item in ps.listen (): #Listening status: take it 
    if there is a message published if item ['type'] == 'message': 
        print item ['channel'] 
        print item ['data']
 
Copy code

 

The data structure, which is the item, is similar to: {'pattern': None, 'type': 'message', 'channel': 'liao', 'data': '300033 1'}, so it can be passed Channel to determine which queue this message belongs to. (When running the program, first run the subscriber, and then run the publisher program)

In summary, there are two main points:

  • One is the connection method. There are three ways to connect to redis using python: ① Use the Redis class in the library (or StrictRedis class, in fact it is almost the same); ② Use the ConnectionPool connection pool (to maintain long connections); ③ Use the Sentinel class (if there are multiple redis clusters, The program will choose a suitable connection by itself).
  • The second is the subscription method. The pubsub method in the StrictRedis class is used here. After connecting, you can use the subscribe or psubscribe method to subscribe to redis messages. Among them, subscribe is to subscribe to a channel, and psubscribe can subscribe to multiple channels (when writing, the channel as a parameter should be a list). Then you can start listening.

The publish / subscribe model in redis is a message communication model. Today, let's talk about implementing a simple publish and subscribe function in python.

Implementation one:
redis_helper.py: encapsulate the publish and subscribe method

import redis
                
                
class RedisHelper(object): def __init__(self): self.__conn = redis.Redis(host="localhost") # 订阅频道 self.chan_sub = "fm104.5" def public(self, msg): """ 在指定频道上发布消息 :param msg: :return: """ # publish(): 在指定频道上发布消息,返回订阅者的数量 self.__conn.publish(self.chan_sub, msg) return True def subscribe(self): # 返回发布订阅对象,通过这个对象你能1)订阅频道 2)监听频道中的消息 pub = self.__conn.pubsub() # 订阅频道,与publish()中指定的频道一样。消息会发布到这个频道中 pub.subscribe(self.chan_sub) ret = pub.parse_response() # [b'subscribe', b'fm86', 1] print("ret:%s" % ret) return pub

redis_pub.py: publisher

from redis_helper import RedisHelper
                
                
obj = RedisHelper()
for i in range(5): obj.public("hello_%s" % i)

redis_sub.py: subscriber

from redis_helper import RedisHelper
                
                
obj = RedisHelper()
redis_sub = obj.subscribe() while True: msg = redis_sub.parse_response() print(msg)

Implementation method 2:
redis_helper.py: encapsulate the publish and subscribe method

import redis
                
                
class RedisHelper(object): def __init__(self): self.__conn = redis.Redis(host="localhost") # 频道名称 self.chan_sub = "orders" def public(self, msg): """ 在指定频道上发布消息 :param msg: :return: """ # publish(): 在指定频道上发布消息,返回订阅者的数量 self.__conn.publish(self.chan_sub, msg) return True def subscribe(self): # 返回发布订阅对象,通过这个对象你能1)订阅频道 2)监听频道中的消息 pub = self.__conn.pubsub() # 订阅某个频道,与publish()中指定的频道一样。消息会发布到这个频道中 pub.subscribe(self.chan_sub) return pub

redis_pub.py:

from redis_helper import RedisHelper


obj = RedisHelper()
for i in range(5): obj.public("hello_%s" % i)

redis_sub.py:

from redis_helper import RedisHelper

obj = RedisHelper()
redis_sub = obj.subscribe()
while True:
    # listen()函数封装了parse_response()函数 msg = redis_sub.listen() for i in msg: if i["type"] == "message": print(str(i["channel"], encoding="utf-8") + ":" + str(i["data"], encoding="utf-8")) elif i["type"] == "subscrube": print(str(i["chennel"], encoding="utf-8"))

The difference between the above two methods is that method one uses the parse_response () method of the publication subscription object to obtain subscription information, and method two uses the listen () method of the publication subscription object to obtain subscription information. The listen () method encapsulates the parse_response () method, adds blocking, and processes the results returned by parse_response () to make the results simpler.

 

Redis publish subscriber model:

  Redis provides a publish and subscribe function, which can be used for message transmission. Redis' publish and subscribe mechanism includes three parts, publisher, subscriber and channel. Both the publisher and the subscriber are Redis clients, and the Channel is the Redis server. The publisher sends the message to a certain channel, and subscribers who subscribe to the channel can receive the message. Redis's publish and subscribe mechanism is similar to topic-based publish and subscribe, and Channel is equivalent to a topic.

            

announcer:

  pub.py

import redis

conn = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)


conn.publish("333", "18")

subscriber:

  sub.py

Import Redis 

conn = redis.Redis (Host = " 127.0.0.1 " , Port = 6379, decode_responses = True) 

# The first step in generating a subscriber object 
PubSub = conn.pubsub () 

# Step subscribe to a news 

pubsub.subscribe ( " gaoxin333 " ) 

# create a reception 

the while True:
     Print ( " Working ~ ~ ~ " ) 
    msg = pubsub.parse_response ()
     Print (msg)

 

(1) Sending a message 
Redis uses the PUBLISH command to send a message. The return value is the number of subscribers who received the message.

(2) Subscribe to a channel 

 

Redis uses the SUBSCRIBE command to subscribe to a channel, and its return value includes the channel that the client subscribed to, the number of channels currently subscribed, and the received message, where subscribe indicates that it has successfully subscribed to a channel.

(3) Pattern matching 

 

The pattern matching function allows clients to subscribe to channels that conform to a certain pattern. Redis uses PSUBSCRIBE to subscribe to all channels that conform to a certain pattern. "" Indicates the pattern, and "" can be replaced by any value. Assuming that the client subscribes to a certain mode and a channel that conforms to the mode at the same time, the message sent to this channel will be received twice by the client, but the types of these two messages are different, one is the message type, one It is pmessage type, but its content is the same. 

(4) Unsubscribe 
Redis uses the UNSUBSCRIBE and PUNSUBSCRIBE commands to cancel the subscription. The return value is similar to the subscription. 
Because Redis's subscription operation is blocking, once the client subscribes to a channel or mode, it will remain in the subscription state until it exits. In the SUBSCRIBE, PSUBSCRIBE, UNSUBSCRIBE and PUNSUBSCRIBE commands, the return value includes the number of channels and modes currently subscribed by the client. When this number becomes 0, the client will automatically exit the subscription status.

 

import redis
conn = redis.Redis(host="127.0.0.1", port=6379, decode_responses=True)

conn.publish("gaoxin333", "18")

Python3 learning (twenty-seven): Python realizes Redis subscription and publishing (sub-pub mechanism)

Pub / Sub function (means Publish, Subscribe) means publishing and subscribing functions. In an event-based system, Pub / Sub is a widely used communication model. It uses events as the basic communication mechanism to provide the loosely coupled interaction mode required by large-scale systems: subscribers (such as clients) subscribe to events It expresses an event or a type of event that it is interested in receiving; the publisher (such as a server) can notify the relevant subscriber of the event of interest to the subscriber at any time.

Generally speaking, it means that my sub-end (subscriber) has been listening. Once the pub-end (publisher) has published a message, then I will receive it. For example, the publisher:

Copy code
 
#coding:utf-8
import time
import redis
 
number_list = ['300033', '300032', '300031', '300030']
signal = ['1', '-1', '1', '-1']
 
rc = redis.StrictRedis(host='***', port='6379', db=3, password='********')
for i in range(len(number_list)):
    value_new = str(number_list[i]) + ' ' + str(signal[i])
    rc.publish("liao", value_new)  #发布消息到liao
 
Copy code

 

Then we take a look at the subscribers:

 

Copy code
 
#coding: utf-8 
import time 
import redis 
 
rc = redis.StrictRedis (host = '****', port = '6379', db = 3, password = '******') 
ps = rc. pubsub () 
ps.subscribe ('liao') 
#Subscribe message from liao for item in ps.listen (): #Listening status: take it 
    if there is a message published if item ['type'] == 'message': 
        print item ['channel'] 
        print item ['data']
 
Copy code

 

The data structure, which is the item, is similar to: {'pattern': None, 'type': 'message', 'channel': 'liao', 'data': '300033 1'}, so it can be passed Channel to determine which queue this message belongs to. (When running the program, first run the subscriber, and then run the publisher program)

In summary, there are two main points:

  • One is the connection method. There are three ways to connect to redis using python: ① Use the Redis class in the library (or StrictRedis class, in fact it is almost the same); ② Use the ConnectionPool connection pool (to maintain long connections); ③ Use the Sentinel class (if there are multiple redis clusters, The program will choose a suitable connection by itself).
  • The second is the subscription method. The pubsub method in the StrictRedis class is used here. After connecting, you can use the subscribe or psubscribe method to subscribe to redis messages. Among them, subscribe is to subscribe to a channel, and psubscribe can subscribe to multiple channels (when writing, the channel as a parameter should be a list). Then you can start listening.

The publish / subscribe model in redis is a message communication model. Today, let's talk about implementing a simple publish and subscribe function in python.

Implementation one:
redis_helper.py: encapsulate the publish and subscribe method

import redis
                
                
class RedisHelper(object): def __init__(self): self.__conn = redis.Redis(host="localhost") # 订阅频道 self.chan_sub = "fm104.5" def public(self, msg): """ 在指定频道上发布消息 :param msg: :return: """ # publish(): 在指定频道上发布消息,返回订阅者的数量 self.__conn.publish(self.chan_sub, msg) return True def subscribe(self): # 返回发布订阅对象,通过这个对象你能1)订阅频道 2)监听频道中的消息 pub = self.__conn.pubsub() # 订阅频道,与publish()中指定的频道一样。消息会发布到这个频道中 pub.subscribe(self.chan_sub) ret = pub.parse_response() # [b'subscribe', b'fm86', 1] print("ret:%s" % ret) return pub

redis_pub.py: publisher

from redis_helper import RedisHelper
                
                
obj = RedisHelper()
for i in range(5): obj.public("hello_%s" % i)

redis_sub.py: subscriber

from redis_helper import RedisHelper
                
                
obj = RedisHelper()
redis_sub = obj.subscribe() while True: msg = redis_sub.parse_response() print(msg)

Implementation method 2:
redis_helper.py: encapsulate the publish and subscribe method

import redis
                
                
class RedisHelper(object): def __init__(self): self.__conn = redis.Redis(host="localhost") # 频道名称 self.chan_sub = "orders" def public(self, msg): """ 在指定频道上发布消息 :param msg: :return: """ # publish(): 在指定频道上发布消息,返回订阅者的数量 self.__conn.publish(self.chan_sub, msg) return True def subscribe(self): # 返回发布订阅对象,通过这个对象你能1)订阅频道 2)监听频道中的消息 pub = self.__conn.pubsub() # 订阅某个频道,与publish()中指定的频道一样。消息会发布到这个频道中 pub.subscribe(self.chan_sub) return pub

redis_pub.py:

from redis_helper import RedisHelper


obj = RedisHelper()
for i in range(5): obj.public("hello_%s" % i)

redis_sub.py:

from redis_helper import RedisHelper

obj = RedisHelper()
redis_sub = obj.subscribe()
while True:
    # listen()函数封装了parse_response()函数 msg = redis_sub.listen() for i in msg: if i["type"] == "message": print(str(i["channel"], encoding="utf-8") + ":" + str(i["data"], encoding="utf-8")) elif i["type"] == "subscrube": print(str(i["chennel"], encoding="utf-8"))

The difference between the above two methods is that method one uses the parse_response () method of the publication subscription object to obtain subscription information, and method two uses the listen () method of the publication subscription object to obtain subscription information. The listen () method encapsulates the parse_response () method, adds blocking, and processes the results returned by parse_response () to make the results simpler.

 

Guess you like

Origin www.cnblogs.com/xiao-xue-di/p/12690900.html