The pair/pair and req/REP modes are discussed above. Now let's look at the pub/sub and push/pull modes.
Pub/Sub: publish/subscribe mode. It is similar to subscribing to news. It adopts asynchronous Io and many-to-many mode. If there is no subscription, the messages sent by the server are directly discarded.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/37/89/wKioL1OtEcmDfgbUAACa4-9bWCI546.jpg "Title =" qq20140627133128.png "alt =" wKioL1OtEcmDfgbUAACa4-9bWCI546.jpg "/>
Pub_server.py
import zmqimport randomimport sysimport timeport = "5556"if len(sys.argv) > 1: port = sys.argv[1] int(port)context = zmq.Context()socket = context.socket(zmq.PUB)socket.bind("tcp://*:%s" % port)while True: topic = random.randrange(9999,10005) messagedata = random.randrange(1,215) - 80 print "%d %d" % (topic, messagedata) socket.send("%d %d" % (topic, messagedata)) time.sleep(1)
Sub_client.py
import sysimport timeimport zmqport = "5556"# Socket to talk to servercontext = zmq.Context()socket = context.socket(zmq.SUB)print "Collecting updates from weather server..."socket.connect("tcp://localhost:%s" % port)#socket.set(zmq.UNSUBSCRIBE, messagedata)topicfilter = "10001"socket.set(zmq.SUBSCRIBE, topicfilter)#Process 5 updatestotal_value = 0#for update_nbr in range (5):while True: string = socket.recv() topic, messagedata = string.split()# total_value += int(messagedata) print topic, messagedata time.sleep(1)
Zmq. subcribe is used to specify a message to be subscribed to. The subscribed message contains 10001 messages.
Push/pull: The task distribution mode. It is mainly used for Distributed Computing. It distributes many tasks to the worker, and then the worker sends the calculation results to the result collector.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/37/8A/wKioL1OtGOHjf4EhAACmQcZJt9k590.jpg "Title =" qq20140627133143.png "alt =" wkiol1otgohjf4ehaacmqczjt9k590.jpg "/>
Producer. py
import timeimport zmqimport randomcontext = zmq.Context()sender = context.socket(zmq.PUSH)sender.bind(‘tcp://*:5557‘)# sync start of batch# be sure all worker connect successsink = context.socket(zmq.PUSH)sink.connect(‘tcp://0.0.0.0:5558‘)print ‘Press Enter when the workers are ready:‘_ = raw_input()print ‘Sending tasks to workers...‘sink.send(b‘0‘)for task_nbr in xrange(1000000): workload = random.randint(1,10) sender.send_string(u‘%i‘ % workload)for i in range(10): sender.send_string(u‘0‘)time.sleep(1)
Consumer. py
import sysimport timeimport zmqcontext = zmq.Context()# Socket to recevie messages onreceiver = context.socket(zmq.PULL)receiver.connect(‘tcp://localhost:5557‘)# socket to send messagessender = context.socket(zmq.PUSH)sender.connect(‘tcp://localhost:5558‘)while True: a_str = receiver.recv_string() num = int(a_str) if num % 2 == 0 or a_str == u‘0‘: sender.send_string(a_str)
Result. py
import sysimport timeimport zmqcontext = zmq.Context()# Socket to receive messages onreceiver = context.socket(zmq.PULL)receiver.bind("tcp://*:5558")# Wait for start of batchs = receiver.recv()sum = 0flag = 0# Start our clock nowtstart = time.time()while True: a_str = receiver.recv_string() num = int(a_str) sum += num if a_str == ‘0‘: flag += 1 if flag == 10: breaktend = time.time()tdiff = tend - tstarttotal_msec = tdiff * 1000print "Total elapsed time: %d msec" % total_msec
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/37/9E/wKioL1OtPO6w0QYIAAES_IQ-b9I110.jpg "Title =" qq20140627173623.png "alt =" wKioL1OtPO6w0QYIAAES_IQ-b9I110.jpg "/>
The results are not accurate. They are the test results of starting one, two, three, and four consumer. py processes, which indicates that the computing time has been shortened.
Queue, forwarder, and streamer are proxies for req/REP, pub/sub, and push/pull respectively. They are used to represent machines in different network segments.
The proxy usage is not described here. See the following URL.
Reference address:
Http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/pyzmq.html
Https://github.com/anjuke/zguide-cn
Http://zguide.zeromq.org/
This article from the "fly World" blog, please be sure to keep this source http://liuping0906.blog.51cto.com/2516248/1431721