首页 > 其他 > 详细

pyzmq的4种模式(pair)笔记

时间:2015-11-29 02:05:31      阅读:486      评论:0      收藏:0      [点我收藏+]

? ? 今天学习下pyzmq,感觉怎么样呢,看了官网开头,记录之,基本上也可以算个翻译

? ? ?Exclusive pair pattern 特点

? ? ? ? ? ?1. 双向通讯

? ? ? ? ? ?2. 套接字无状态(封装的很好吧)

? ? ? ? ? ?3. 只能有一个对等连接(这能干嘛用,似乎没啥用)

? ? ? ? ? ?4. 服务端监听,客户端连接

bubuko.com,布布扣

然后:你根本不用考虑message接收的完整不完整,你照做就是了!(原文)

官方example在此:

pairserver.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
import zmq
import random
import sys
import time

port = "5556"
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind("tcp://*:%s" % port)
while True:
    socket.send("Server message to client3")
    msg = socket.recv()
    print msg
    time.sleep(1)

pairclient.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import zmq
import random
import sys
import time

port = "5556"
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.connect("tcp://localhost:%s" % port)
while True:
    msg = socket.recv()
    print msg
    socket.send("client message to server1")
    socket.send("client message to server2")
    time.sleep(1)

?

然后run起来,有点小问题,client连续send了2次,server倒是没问题,来一个响应一个,client的recv()不是应该收2个?"Server message to client3"吗

?改一下server:

import zmq
import time

port = ‘5556‘
context = zmq.Context()
socket = context.socket(zmq.PAIR)
socket.bind(‘tcp://*:%s‘%port)
i = 0
while True:
	i += 1
	print ‘begin:‘,i
	socket.send(‘msg from server:‘+str(i))
	msg = socket.recv()
	print ‘msg:%s‘%msg
	time.sleep(1)

?run起来:

client:?

begin
msg from server:1
begin
msg from server:2
begin
msg from server:3

?server:

begin: 1
msg:client msg to server -1
begin: 2
msg:client msg to server -2
begin: 3
msg:client msg to server -1
begin: 4
msg:client msg to server -2
begin: 5
msg:client msg to server -1
begin: 6

?好吧,它确实没丢任何message,只能说呵呵,这封装的,有点别扭。

pyzmq的4种模式(pair)笔记

原文:http://pjwqq.iteye.com/blog/2259952

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!