359025439 发表于 2018-11-6 08:16:49

【Python之旅】第七篇(三):使用Redis订阅服务

  在C/S架构中,可以充分地利用Redis订阅服务,实现服务器端和客户端的信息收发,下面说说在Python中如何使用Redistribute的订阅服务。
  这里要举的例子是,Server端进行服务的订阅,而Client端进行消息的广播发送。
  1.方法一:Server端使用Redis操作+Client端使用Python交互器
  (1)Server端
  进入Redis操作界面:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis-2.8.9/src$ redis-cli  
127.0.0.1:6379>
  开始监听频道chan_107:
127.0.0.1:6379> SUBSCRIBE chan_107  
Reading messages... (press Ctrl-C to quit)
  
1) "subscribe"
  
2) "chan_107"
  
3) (integer) 1
  (2)Client端
  创建redis_connector.py文件,用以连接Server端Redis:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='192.168.1.112',port=6379,db=0)
  在Python交互器中导入redis_connector,并发布消息:
>>> redis.r.publish('chan_107','hello my name is xpleaf')  
1L
  此时在Server端中就能看到Client端发布的消息了:
127.0.0.1:6379> SUBSCRIBE chan_107  
Reading messages... (press Ctrl-C to quit)
  
1) "subscribe"
  
2) "chan_107"
  
3) (integer) 1
  
1) "message"
  
2) "chan_107"
  
3) "hello my name is xpleaf"
  2.方法二:Server端使用Python程序+Client端使用Python交互器
  (1)Server端
  程序代码如下:
import redis_connector as redis  

  
channel = 'chan_107'    #频道应该要和发布者的一样,否则将无法订阅到发布者发布的消息
  

  
msg_queue = redis.r.pubsub() #bind listen instance
  
msg_queue.subscribe(channel)
  

  
while True:
  data = msg_queue.parse_response() #waiting for the publisher
  print data
  可以看到这里也导入了redis_connector模块用来连接本地的Redis数据库,跟Client端的类似,只是IP地址改为'localhost',如下:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='localhost',port=6379,db=0)
  msg_queue.parse_response()即是用来响应Client端发布的消息,执行该程序,开始监听消息:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
===>光标停在此处,开始监听Client端发布的消息
  (2)Client端
  创建redis_connector.py文件,用以连接Server端Redis:
#!/usr/bin/env python  
import redis
  
r = redis.Redis(host='192.168.1.112',port=6379,db=0)
  在Python交互器中导入redis_connector,并发布消息:
>>> redis.r.publish('chan_107','hello my name is yonghaoye')  
2L
  
>>> redis.r.publish('chan_107','second msg')
  
2L
  上面两步其实和方法一是一样的。
  此时在Server端中也可以订阅到Client端发布的消息了:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
['message', 'chan_107', 'hello my name is yonghaoye']
  
['message', 'chan_107', 'second msg']
  
===>光标停在此处,继续监听Client端发布的消息
  3.方法三:Server端使用Python程序+Client端使用Python程序
  直接给出Client端的程序代码:
import redis_connector as redis  

  
for i in range(10):
  
      redis.r.publish('chan_107','This is the NO.%s msg I send to you' % i)
  Server端开始监听:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  Client端发布消息:
# python publish.py  在Server端中很快就可以监听到Client端发布的消息:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py  
['message', 'chan_107', 'This is the NO.0 msg I send to you']
  
['message', 'chan_107', 'This is the NO.1 msg I send to you']
  
['message', 'chan_107', 'This is the NO.2 msg I send to you']
  
['message', 'chan_107', 'This is the NO.3 msg I send to you']
  
['message', 'chan_107', 'This is the NO.4 msg I send to you']
  
['message', 'chan_107', 'This is the NO.5 msg I send to you']
  
['message', 'chan_107', 'This is the NO.6 msg I send to you']
  
['message', 'chan_107', 'This is the NO.7 msg I send to you']
  
['message', 'chan_107', 'This is the NO.8 msg I send to you']
  
['message', 'chan_107', 'This is the NO.9 msg I send to you']


页: [1]
查看完整版本: 【Python之旅】第七篇(三):使用Redis订阅服务