A ZeroMQ multithreading application is designed keeping in mind a few rules:
- Each process has its own ZeroMQ context, that is the only object that should be shared among threads. Nothing else, ZeroMQ socket included, should be shared.
- The threads in a process are connected by inproc sockets.
- A thread could have its own ZeroMQ context, but in this case it can't be connected to other threads in the same process using an inproc socket.
In this post, I use the C++ interface to ØMQ 2.1, if you are using version 3.1, you could be interested in another post, where I have also implemented a graceful way to shutdown the worker threads.
A well designed ZeroMQ application could be easily modified to switch from a multiprocess to a multithread support. For instance, let's have a look at the ZeroMQ broker example we have just seen.
The client doesn't change at all. It is going to have a REQ socket like this:
zmq::socket_t socket(context, ZMQ_REQ); socket.connect("tcp://localhost:5559");And send/receive through it a message and its reply as generated by the server.
The changes are all in the server. Originally it was built as a bunch of processes connected to the broker, now we rewrite it as a single multithreaded process. The broker is not running anymore in its own process, but it is part of the server itself.
This is a possible C++ implementation for the server main routine:
boost::thread_group threads; // 1 try { zmq::context_t context(1); zmq::socket_t clients(context, ZMQ_ROUTER); clients.bind("tcp://*:5559"); zmq::socket_t workers(context, ZMQ_DEALER); zmq_bind(workers, "inproc://workers"); // 2 for(int i = 0; i < threadNbr; ++i) // 3 threads.create_thread(std::bind(&doWork, std::ref(context))); // 4 zmq::device(ZMQ_QUEUE, clients, workers); // 5 } catch(const zmq::error_t& ze) { std::cout << "Exception: " << ze.what() << std::endl; } threads.join_all(); // 61. To make the application more portable, we can use the Boost Thread library. Here we create a group of threads, that would contain all the service threads.
2. This is the major change in the code. The DEALER socket does not expects anymore a connection from other processes, but from other inproc sockets.
3. In the variable threadNbr we have precedently put the number of concurrent service we want to run. This value could be passed as input argument, or read from a configuration file.
4. We create a new thread, specifying the code it has to run, and a parameter that should be passed by reference to the actual function - doWork(). The parameter is the ZeroMQ context that, as we said, is the only object that we expect to be shared among different threads.
5. As before, we use ZeroMQ queue device to do the dirty job.
6. Currently this code is never executed, since the device is expected to run forever. But a more refined implementation should in any case take care of cleaning up.
The doWork() function is very close to the code that was executed by any single process in the previous version. The main differences are that here we don't create a new context, but use the one passed as parameter, enjoying the fact that it is thread-safe; and that the reply socket connects inproc to the broker:
void doWork(zmq::context_t& context) { try { zmq::socket_t socket(context, ZMQ_REP); // 1 socket.connect("inproc://workers"); // 2 while(true) { zmq::message_t request; socket.recv(&request); std::string data((char*)request.data(), (char*)request.data() + request.size()); std::cout << "Received: " << data << std::endl; boost::this_thread::sleep(boost::posix_time::seconds(1)); // 3 zmq::message_t reply(data.length()); memcpy(reply.data(), data.c_str(), data.length()); socket.send(reply); } } catch(const zmq::error_t& ze) { std::cout << "Exception: " << ze.what() << std::endl; } }1. The socket is created using the process context.
2. Given (1), we can connect the socket inproc.
3. Using Boost sleep we write code easier to port on different platforms.
You can find the C code on which this C++ example is based in the Z-Guide.
please provide how we can manage in php
ReplyDeleteSorry man (also for the late answer), I can't. I have no time for it, and my PHP knowledge level is very low.
DeleteThis is a great post! Came across ZeroMQ when trying to split a large MT application in several processes, but it looks like I'll be able to simplify it greatly while allowing to run modules as separate process if required. So thanks for writing this sample code :)
ReplyDeleteThank you, Antony. I agree with you, ZeroMQ is a very nice and useful tool!
DeleteGood content. You write beautiful things.
ReplyDeletehacklink
taksi
sportsbet
vbet
vbet
mrbahis
korsan taksi
sportsbet
hacklink
başakşehir
ReplyDeletebayrampaşa
beşiktaş
beykoz
beylikdüzü
C4VP
hatay
ReplyDeletekars
mardin
samsun
urfa
XV7WEP
Atatürk
ReplyDeleteKusura
Saray
Alipaşa
Meydankavağı
C8UQ6
Iğdır
ReplyDeleteAdana
Karabük
Diyarbakır
Antep
LZLG0
Ankara
ReplyDeleteVan
Hakkari
Edirne
Yozgat
58MZ7W
Eskişehir
ReplyDeleteAdana
Sivas
Kayseri
Samsun
7Dİ
whatsapp görüntülü show
ReplyDeleteücretli.show
Q7FUG
görüntülü.show
ReplyDeletewhatsapp ücretli show
B6İ00E
ankara parça eşya taşıma
ReplyDeletetakipçi satın al
antalya rent a car
antalya rent a car
ankara parça eşya taşıma
2DH
39C01
ReplyDeleteBursa Parça Eşya Taşıma
Kütahya Lojistik
Aydın Evden Eve Nakliyat
Kars Lojistik
Kocaeli Evden Eve Nakliyat
D2390
ReplyDeleteTokat Evden Eve Nakliyat
Antep Şehir İçi Nakliyat
Mexc Güvenilir mi
Mamak Boya Ustası
Çerkezköy Sineklik
Yozgat Parça Eşya Taşıma
Pancakeswap Güvenilir mi
Karabük Parça Eşya Taşıma
Çerkezköy Boya Ustası
D44B3
ReplyDeleteKripto Para Üretme Siteleri
Coin Nasıl Kazılır
Mexc Borsası Kimin
Bitcoin Nasıl Kazılır
Binance Borsası Güvenilir mi
Ön Satış Coin Nasıl Alınır
Yeni Çıkan Coin Nasıl Alınır
Mexc Borsası Kimin
Binance Neden Tercih Edilir
13990
ReplyDeletekilis görüntülü sohbet yabancı
istanbul sesli sohbet odası
şırnak rastgele görüntülü sohbet
samsun kadınlarla sohbet
bedava görüntülü sohbet sitesi
diyarbakır ücretsiz sohbet odaları
sinop telefonda rastgele sohbet
igdir muhabbet sohbet
tekirdağ görüntülü canlı sohbet
00641
ReplyDeleterastgele sohbet odaları
sesli sohbet sesli chat
nevşehir canlı görüntülü sohbet siteleri
Giresun Sesli Sohbet Siteleri
hakkari canli goruntulu sohbet siteleri
Erzincan Rastgele Sohbet Odaları
Antep Kadınlarla Görüntülü Sohbet
görüntülü sohbet
random görüntülü sohbet
7BC63
ReplyDeleteistanbul mobil sohbet chat
aydın ucretsiz sohbet
Tokat Parasız Sohbet
düzce rastgele görüntülü sohbet ücretsiz
hakkari bedava görüntülü sohbet sitesi
adana bedava sohbet siteleri
elazığ canlı görüntülü sohbet siteleri
amasya parasız sohbet
yabancı canlı sohbet