【Python之旅】第七篇(三):使用Redis訂閱服務
1
2
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/redis- 2.8 . 9 /src$ redis-cli
127.0 . 0.1 : 6379 >
|
1
2
3
4
5
|
127.0 . 0.1 : 6379 > SUBSCRIBE chan_107
Reading messages... (press Ctrl-C to quit) 1 ) "subscribe"
2 ) "chan_107"
3 ) (integer) 1
|
1
2
3
|
#!/usr/bin/env python import redis
r = redis.Redis(host= `192.168.1.112` ,port= 6379 ,db= 0 )
|
1
2
|
>>> redis.r.publish( `chan_107` , `hello my name is xpleaf` )
1L |
1
2
3
4
5
6
7
8
|
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"
|
1
2
3
4
5
6
7
8
9
10
|
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
|
1
2
3
|
#!/usr/bin/env python import redis
r = redis.Redis(host= `localhost` ,port= 6379 ,db= 0 )
|
1
2
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py ===>游標停在此處,開始監聽Client端釋出的訊息 |
1
2
3
|
#!/usr/bin/env python import redis
r = redis.Redis(host= `192.168.1.112` ,port= 6379 ,db= 0 )
|
1
2
3
4
|
>>> redis.r.publish( `chan_107` , `hello my name is yonghaoye` )
2L >>> redis.r.publish( `chan_107` , `second msg` )
2L |
1
2
3
4
|
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端釋出的訊息 |
1
2
3
4
|
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)
|
1
|
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day7/monitor/m_server/core$ python redis_sub.py |
1
|
[root@moban ~]# python publish.py |
1
2
3
4
5
6
7
8
9
10
11
|
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` ]
|
相關文章
- Bango:美國訂閱服務使用者調查Go
- 訂閱制正酣?解析Google Play Pass、Apple Arcade 訂閱服務GoAPP
- 詳談:Redis事務和訊息訂閱Redis
- Redis釋出訂閱Redis
- 【Python】python使用redis做佇列服務PythonRedis佇列
- Python–Redis實戰:第三章:Redis命令:第六節:釋出與訂閱PythonRedis
- Redis的釋出訂閱Redis
- Redis系列(三)-Redis釋出訂閱及客戶端程式設計Redis客戶端程式設計
- 利用redis的hash結構搭建訊息服務(發訊息,訂閱訊息,消費訊息,退訂)Redis
- 使用Spring Data Redis 釋出訂閱訊息SpringRedis
- spring boot 使用redis進行釋出訂閱Spring BootRedis
- 使用Spring Data Redis 實現訂閱/釋出SpringRedis
- 索尼PlayStation Vue服務訂閱使用者將登陸Apple TVVueAPP
- Redis服務之常用配置(三)Redis
- Digitas:會員訂閱服務調查報告Git
- 在蘋果Mac如何檢視或取消訂閱服務?蘋果Mac
- Redis 的訂閱與釋出Redis
- Redis 訂閱 + swoole_websocket 推送RedisWeb
- Laravel Redis釋出與訂閱.LaravelRedis
- Redis之釋出與訂閱Redis
- redis訂閱者和進階Redis
- YouGov:56%的美國人訂閱了流媒體服務Go
- 基於訂閱的服務通訊架構體系架構
- 訂閱服務能否拯救瀕臨消亡的“2A”遊戲?遊戲
- Redis系列(八):釋出與訂閱Redis
- redis 釋出與訂閱原理分析Redis
- 瑞士軍刀redis - 釋出訂閱Redis
- 設計模式之釋出訂閱模式(2) Redis 釋出/訂閱模式設計模式Redis
- 上線半年,蘋果的遊戲訂閱服務表現如何了?蘋果遊戲
- 蘋果遊戲訂閱服務Apple Arcade上線測試蘋果遊戲APP
- 我為什麼不看好蘋果推出的遊戲訂閱服務?蘋果遊戲
- 訂閱號服務開發01-搭建開發環境開發環境
- 蘋果遊戲訂閱服務啟動內部員工測試蘋果遊戲
- 蘋果遊戲訂閱服務 Arcade 定價4.99美元/月蘋果遊戲
- WCCFTech:優秀作品與XGP遊戲訂閱服務相輔相成遊戲
- 萬字長文入門 Redis 命令、事務、鎖、訂閱、效能測試Redis
- 基於 Redis 的訂閱與釋出Redis
- Redis的訊息釋出和訂閱Redis