ZeroMQ with producer

</step00>

Make sure what you need !

Let’s see the map below:

</step01> If your data centre send many many data to you by socket …

Let’s see the map again ..

</step02> So you may 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 be 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 have 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 will 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::all

,总在盼望未来,愿你的人生美开

ZeroMQ with producer

相关文章:

你感兴趣的文章:

标签云: