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'
]
|