</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