Can anyone tell what are the types of zmq.sockets?
In what situation one can use these sockets?
The main difference I need is zmq.DEALER
and zmq.ROUTER
in python zeroMQ?
Which type of socket can use these sockets?
ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker.
ZeroMQ (also known as ØMQ, 0MQ, or zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast.
A REP socket is used by a service to receive requests from and send replies to a client. This socket type allows only an alternating sequence of receive and subsequent send calls. Each request received is fair-queued from among all clients, and each reply sent is routed to the client that issued the last request.
Contexts help manage any sockets that are created as well as the number of threads ZeroMQ uses behind the scenes. Create one when you initialize a process and destroy it as the process is terminated. Contexts can be shared between threads and, in fact, are the only ZeroMQ objects that can safely do this.
DEALER and ROUTER are sockets, which can allow easy scaling of REQ / REP pairs.
In direct communication, REQ and REP are talking in blocking manner.
A router is able to accepts requests, adds an envelope with information about that requestee, and makes this new message available for further processing by interconnecting code). When the response comes back (in an envelop), it can pass the response back to the requestee.
Dealer cares about workers. Note, that to make the whole solution usable, workers have to connect to the dealer, not the other way around.
DEALER also allows non-blocking connection with REP.
Some connecting code passes a request in an envelope to the DEALER. The dealer manages the distribution of such requests to workers (without the envelope) and later responds back to the interconnecting code (again in an envelope).
An interconnecting code is to shuffle messages between ROUTER and DEALER sockets.
The simplest version is here: http://zguide.zeromq.org/py:rrbroker
# Simple request-reply broker # # Author: Lev Givon <lev(at)columbia(dot)edu> import zmq # Prepare our context and sockets context = zmq.Context() frontend = context.socket(zmq.ROUTER) backend = context.socket(zmq.DEALER) frontend.bind("tcp://*:5559") backend.bind("tcp://*:5560") # Initialize poll set poller = zmq.Poller() poller.register(frontend, zmq.POLLIN) poller.register(backend, zmq.POLLIN) # Switch messages between sockets 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With