What type of socket does ZeroMQ use for interprocess communication?

When I had two threads, I used the PAIR socket type. But now I use two processes that can be either on the same machine or on different machines. I do not need requests and answers, I do not need to send to multiple nodes, etc. I need the same thing that I had with PAIR (asynchronous, bidirectional), but with processes and with the network. What types of sockets should I use?

+6
source share
2 answers

Unfortunately, your world has become a little complicated. There is no direct analogue of the PAIR/PAIR socket in more widely distributed systems.

However, if you adhere to approximately the same shape of the topology (two nodes connecting exclusively with each other and without any other nodes), you can pretty much achieve what you want using ROUTER/DEALER or even DEALER/DEALER ( as you are in the comments). These sockets are similar to REQ/REP , but they do not provide a strict template for communication with the request and response, they are completely unlimited, so you get the same thing. The only problem arises if you intend to add more nodes, after which you should start managing things a little differently, in particular, the DEALER socket does not allow you to choose which node you are sending to, it is strictly rounded by the robin.

But by doing this, you will get what you are looking for (asynchronous, bidirectional).

The ROUTER socket type may require a bit of additional complexity, since you need to track the β€œidentifier” of another node in order to be able to send it back (you can get it almost for free, especially in your case with only one peer using it directly from the sent message). Since this is an exclusive pair, you can ignore the rounding uncertainty introduced by the DEALER socket and just go straight to DEALER/DEALER , which gives you an unlimited message template and does not require identifier management.

+2
source

@ Marco let me notice

there is a fundamental separation between the ZMQ.SOCKET (formal-communication template) "type" and any transport, select .bind() / .connect() over

Once your architecture was happy (as you wrote) to work with a PAIR / PAIR session

you can simply change the transport that will be used without any additional SLOC

works

 Python 2.7.3 ... >>> import zmq >>> zmq.zmq_version() '2.1.11' >>> aZmqCONTEXT = zmq.Context() # --<BoCTX>-- [SideA] Node >>> aZmqSOCKET = aZmqCONTEXT.socket( zmq.PAIR ) # here one decides about a type >>> aZmqSOCKET.bind( "tcp://192.168.0.62:2027" ) # here is the transport // used to be ( "ipc://...") >>> aZmqSOCKET.recv() # here the PAIR waits for 1st MSG 'aMSG from the opposite PAIR/PAIR zmq-session Node arrived via TCP-transport ... QED' >>> aZmqSOCKET.setsockopt( zmq.LINGER, 0 ) # pre-termination tidy-up >>> aZmqSOCKET.close() >>> aZmqCONTEXT.term() # --<EoCTX>-- safe to clean-exit >>> 
0
source

Source: https://habr.com/ru/post/970572/


All Articles