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?
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.
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.
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.
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:
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.
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