Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is zmq.ROUTER and zmq.DEALER in python zeromq?

Tags:

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?

like image 951
user27 Avatar asked May 10 '14 12:05

user27


People also ask

What is Zmq Python?

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.

What is Zmq socket?

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.

What is Rep socket?

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.

What is a Zmq context?

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.


1 Answers

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.

ROUTER - accept requests - from REQ side

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 - talks to workers - on REP side

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).

Interconnecting code

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) 
like image 148
Jan Vlcinsky Avatar answered Oct 29 '22 22:10

Jan Vlcinsky