Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zmq Context - Should I create another context in a new thread?

Tags:

c++

zeromq

I've got several server apps that use a shared ZMQ class I created. Occasionally when these servers process a request, they need to send a message to another ZMQ server. I'm fairly new to ZMQ so I wanted to make sure I understand this correctly.

The class that handles the server listener creates a zmq::context_t and zmq::socket_t, and binds the socket. This runs on a separate thread infinitely. When this server needs to send a message in another function ( completely torn-off from this ZMQ class ), would I need to generate a new context here and send the message, or should I somehow pass down the same context to this class ( on a different thread ), bind a new socket then go from there?

If the former, does it matter what number I use to initialize the new context, or is context( 1 ) fine? There's a part in the guide that says creating a second context is like having multiple instances of ZMQ, which I don't think really matters since its only being used to send a file then closing. But I'm probably wrong?

like image 592
user1324674 Avatar asked Jul 17 '17 23:07

user1324674


People also ask

What is context in rbzmq?

ZMQ::Context In: rbzmq.c Parent: Object ZeroMQ library context. Methods close new socket Public Class methods new(io_threads=1) Initializes a new 0MQ context. The io_threads argument specifies the size of the 0MQ thread pool to handle I/O operations.

What is the use of ZMQ_term() function?

Terminates the 0MQ context. If there are no longer any sockets open within context at the time zmq_term() is called then context shall be shut down and all associated resources shall be released immediately. Otherwise, the following applies: The close() function shall return immediately.

How many examples of ZeroMQ zcontext are there?

C# (CSharp) ZeroMQ ZContext - 30 examples found. These are the top rated real world C# (CSharp) examples of ZeroMQ.ZContext extracted from open source projects. You can rate examples to help us improve the quality of examples.

What happens when ZMQ_term() is called?

zmq.close() → nil Terminates the 0MQ context. If there are no longer any sockets open within context at the time zmq_term() is called then context shall be shut down and all associated resources shall be released immediately. Otherwise, the following applies:


1 Answers

In short: a single instance of context, with a single I/O thread, is probably what you need. In more detail:

Per the docs the context is thread safe. Note that the original 0MQ sockets generally are not ( with having some newer, drafted, ones designed so as they become such ).

From the FAQ:

What is the optimal number of I/O threads for best performance?

The basic heuristic is to allocate 1 I/O thread in the context for every gigabit per second of data that will be sent and received ( aggregated ). Further, the number of I/O threads should not exceed ( number_of_cpu_cores - 1 ).

Another thing to note is inproc:// transport-class sockets must be created in the same context.

Unless you're transferring multiple gigabits/sec, I recommend a single context for your entire process with a single I/O thread.

Also as a guideline, the 0MQ context is intended to be "long-lived". It would be unusual to create/destroy multiple contexts in a single run of an application. This is generally true of 0MQ sockets, too. If you are creating/destroying many sockets you may have taken the wrong approach.

like image 121
colini Avatar answered Oct 08 '22 05:10

colini