Giter Site home page Giter Site logo

fbzmq threading model about fbzmq HOT 4 CLOSED

facebookarchive avatar facebookarchive commented on August 22, 2024
fbzmq threading model

from fbzmq.

Comments (4)

saifhhasan avatar saifhhasan commented on August 22, 2024

Hello,

ZMQ internally has multiple IO threads (configurable when you create context). These threads are responsible for actually performing IO. Socket read/recv are just commands exchanged between ZMQ socket interface and actual IO threads (often there is 1 IO thread as it can serve upto several Gbps of throughput as per ZMQ's doc).

Give a shot to what you intend to do and ZMQ's architecture should be able to handle it with scale you intend.

Once message is received you can pass it to worker thread or do processing yourself. In former case you will need method to interact between threads.

NOTE: fbzmq is just a thin wrapper over the ZeroMQ library in C, with some additional constructs that makes programming in C++ easier and less error prone with ZMQ.

from fbzmq.

kreuzerkrieg avatar kreuzerkrieg commented on August 22, 2024

Thanks for response. My question is, when and how I return reply from the server after the message is processed in separate worker thread. Consider following code:

addSocket(fbzmq::RawZmqSocketPtr{*serverSocket}, ZMQ_POLLIN, [this](int) noexcept {
           auto msg = serverSocket.recvOne(serverConfiguration.readTimeout);
           auto resp = requestProcessor(msg);
           serverSocket.sendOne(resp.value());
       });

It is a simple construct which blocks the processing thread, message received --> processed --> reply sent. Now lets make the processing asynchronous

addSocket(fbzmq::RawZmqSocketPtr{*serverSocket}, ZMQ_POLLIN, [this](int) noexcept {
           auto msg = serverSocket.recvOne(serverConfiguration.readTimeout);
           threadPool.AddTask([self{shared_from_this()}](auto&& msg){self->requestProcessor(msg);});
           serverSocket.sendOne(resp.value()); // <-- now what?
       });

In this case the processing is done asynchronously, but how I send the reply? Passing the socket to the thread pool to signal the completion sounds like a bad idea since (AFAIR) zmq sockets are not thread-safe and cannot be used simultaneously from different threads.

from fbzmq.

saifhhasan avatar saifhhasan commented on August 22, 2024

You add socket to event loop ... There are two ways to send reply I can think of

  • When you receive an event on socket and send response to the same socket or other
  • Or timeout that occurs in loop (EventLoop supports scheduling timeouts), which will invoke callback function and it can send message to socket.

One thing to note here is that, if callback is invoked that means message is always ready to read so you can read with NON_BLOCKING flag to avoid getting blocked.

I think what you're asking here is general async programming question. You might like to refer to some guides available online to achieve that.

from fbzmq.

kreuzerkrieg avatar kreuzerkrieg commented on August 22, 2024

@saifhhasan, I dont think this question is in any way related to async programming. It is a question of zmq patterns to employ in such a case. For example, the answer may be Paranoid Pirate Pattern which is described in the zmq guide.

from fbzmq.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.