ZeroMQ with Producer-consumer

Source: Internet
Author: User


</step00>


Make sure need!

Let's see the map below:



</step01> If Your Data centre send many many data to a socket ...


Let's see the map again.




</step02> So what know how to use the ZeroMQ deal with your data ...


We assume the the socket.recv just like a client ... (Actually,it ' s server/client)


So the code can is like the below ...


</pre><pre name= "code" class= "python" >#!/usr/bin/pythonimport zmq import timecontext = Zmq. Context () socket = Context.socket (ZMQ. REQ) Socket.connect ("tcp://localhost:5559") for request in range (1,11):            socket.send (b "hello:-->%d"% request)        message = SOCKET.RECV ()        print ("Received reply%s [%s]"% (request,message))        Time.sleep (1)


</step03> Here,you has the data source ... But where is the ZeroMQ.

Good! Let ' s create one, Because it's work like a broker, so we name it as "broker.py"


#!/usr/bin/pythonimport Zmqcontext = zmq. Context () frontend = Zmq. Context () frontend = Context.socket (zmq. ROUTER) backend = Context.socket (zmq. DEALER) Frontend.bind ("tcp://*:5559") backend.bind ("tcp://*:5560") Poller = Zmq. Poller () Poller.register (FRONTEND,ZMQ. Pollin) Poller.register (BACKEND,ZMQ. Pollin) while True:        socks = Dict (Poller.poll ())        if Socks.get (frontend) = = Zmq. Pollin:                message = Frontend.recv_multipart ()                backend.send_multipart (message)        if Socks.get (backend) = = Zmq. Pollin:                message = Backend.recv_multipart ()                frontend.send_multipart (message)


</step04> we have a broker, so he/she looking for someone work for him/her ....

Here,we'll hire some worker ... (Actually,we make them ...)

How-to-make it ... Let's see the code below


#!/usr/bin/pythonimport Zmqcontext = zmq. Context () frontend = Zmq. Context () frontend = Context.socket (zmq. ROUTER) backend = Context.socket (zmq. DEALER) Frontend.bind ("tcp://*:5559") backend.bind ("tcp://*:5560") Poller = Zmq. Poller () Poller.register (FRONTEND,ZMQ. Pollin) Poller.register (BACKEND,ZMQ. Pollin) while True:        socks = Dict (Poller.poll ())        if Socks.get (frontend) = = Zmq. Pollin:                message = Frontend.recv_multipart ()                backend.send_multipart (message)        if Socks.get (backend) = = Zmq. Pollin:                message = Backend.recv_multipart ()                frontend.send_multipart (message)


</step05>


That ' s all! Thanks:)


Ref:http://zguide.zeromq.org/page:all

ZeroMQ with Producer-consumer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.