【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` ]
|
相關文章
- 訂閱制正酣?解析Google Play Pass、Apple Arcade 訂閱服務GoAPP
- Bango:美國訂閱服務使用者調查Go
- 育碧或將推出訂閱服務
- 詳談:Redis事務和訊息訂閱Redis
- Dubbo系列之 (四)服務訂閱(1)
- Dubbo系列之 (六)服務訂閱(3)
- Dubbo系列之 (五)服務訂閱(2)
- Redis釋出訂閱Redis
- Python–Redis實戰:第三章:Redis命令:第六節:釋出與訂閱PythonRedis
- spring boot 使用redis進行釋出訂閱Spring BootRedis
- 使用Spring Data Redis 釋出訂閱訊息SpringRedis
- 育碧全新訂閱服務UPLAY+公佈
- Mindshare:3/4的英國流媒體訂閱使用者將繼續訂閱現有服務
- Digitas:會員訂閱服務調查報告Git
- Recurly:訂閱服務平均流失率為7.02%
- 利用redis的hash結構搭建訊息服務(發訊息,訂閱訊息,消費訊息,退訂)Redis
- Laravel Redis釋出與訂閱.LaravelRedis
- Redis 的訂閱與釋出Redis
- Redis 訂閱 + swoole_websocket 推送RedisWeb
- redis訂閱者和進階Redis
- Simon Kucher&Partners:全球35%玩家享有訂閱服務
- Redis服務之常用配置(三)Redis
- 瑞士軍刀redis - 釋出訂閱Redis
- redis 釋出與訂閱原理分析Redis
- Redis系列(八):釋出與訂閱Redis
- 設計模式之釋出訂閱模式(2) Redis 釋出/訂閱模式設計模式Redis
- 在蘋果Mac如何檢視或取消訂閱服務?蘋果Mac
- Redis的訊息釋出和訂閱Redis
- 基於 Redis 的訂閱與釋出Redis
- Redis實現訊息釋出訂閱Redis
- SpringBoot Redis 釋出訂閱模式 Pub/SubSpring BootRedis模式
- .NetCore使用Redis,StackExchange.Redis佇列,釋出與訂閱,分散式鎖的簡單使用NetCoreRedis佇列分散式
- 訂閱服務能否拯救瀕臨消亡的“2A”遊戲?遊戲
- 蘋果遊戲訂閱服務 Arcade 定價4.99美元/月蘋果遊戲
- Ofcom:47%的英國家庭訂閱了流媒體服務
- 蘋果遊戲訂閱服務Apple Arcade上線測試蘋果遊戲APP
- YouGov:56%的美國人訂閱了流媒體服務Go
- 訂閱號服務開發01-搭建開發環境開發環境
- 萬字長文入門 Redis 命令、事務、鎖、訂閱、效能測試Redis