Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ: asynchronous replies

Tags:

c

zeromq

I'm building an infrastructure (in C) where several seperate services communicate trough ZeroMQ. Now, in order to make this all running fast and smoothly, I want ZeroMQ to run asynchronously.
I know that ZeroMQ's IO-thread already is separated, but I want to perform multiple tasks at the same time.

At the moment, my pattern looks like this:

Client -> REQ     |  /\
Router -> ROUTER  |   |
---   proxy   --- |   |
Dealer -> DEALER  |   |
Workers-> REP     \/--|

However, this requires me to design a blocking, synchronous worker. The only way to make this asynchronous is to increase the number of workers (threads), but this doesn't seem very scalable to me.

Furthermore, the tasks that the workers perform are asynchronous themselves (using their own event loop). So basically, I'm looking to achieve something like this:

void *socket = zmq_socket(context, ZMQ_REP);
zsocket_connect(socket, "inproc://backend");

while (1) {
    zmq_msg_t request;
    zmq_msg_init(&request);
    zmq_msg_recv(&request, socket, 0);
    zmq_msg_close(&request);

    do_something(callback, socket);
}

do_something would then perform some operations, and when it's done, the callback function gets called.

void callback(void *data, void *socket) {
    zmq_msg_t reply;
    zmq_msg_init_size(&reply, 5);
    memcpy(zmq_msg_data(&reply), "World", 5);
    zmq_msg_send(&reply, socket, 0);
    zmq_msg_close(&reply);
}

Now, is there any way to achieve this (i.e. to combine two event loops)?

I've already looked into DEALER (client) -> ROUTER -> DEALER -> DEALER (worker), but that doesn't seem to work either, because it still blocks concurrent tasks at the worker end.

like image 807
cutsoy Avatar asked Mar 09 '13 22:03

cutsoy


People also ask

Is ZeroMQ asynchronous?

ZeroMQ is an asynchronous network messaging library known for its high performance. It's intended use is for distributed systems as well as concurrent systems. In summary, ZMQ allows you to send messages (binary data, serialized data, simple strings, etc.)

Is ZeroMQ TCP or UDP?

MQTT requires TCP/IP, whereas ZeroMQ can use a number of underlying transports, including UDP and shared memory.

Is ZeroMQ a message broker?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is an 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; the zero in the name is for zero broker.

What is a ZeroMQ 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

(client) DEALER<->ROUTER |<->| ROUTER <-> DEALER (worker) In this connection pattern nothing will block, hence you can interleave receives and sends however you want.

like image 99
iiwaasnet Avatar answered Sep 28 '22 07:09

iiwaasnet