ZeroMQ 文档指出,这是 PUSH/PULL 设置的问题,并建议采用以下模式:当您期望订阅者数量固定时,添加 REP/REQ 设置以提供节点协调。但是,如果您无法提前知道订阅者的数量,您应该考虑更改您的协议以更好地适应这些条件。
C 中的同步发布者(来自 ZGuide)
//
// Synchronized publisher
//
#include "zhelpers.h"
// We wait for 10 subscribers
#define SUBSCRIBERS_EXPECTED 10
int main (void)
{
s_version_assert (2, 1);
void *context = zmq_init (1);
// Socket to talk to clients
void *publisher = zmq_socket (context, ZMQ_PUB);
zmq_bind (publisher, "tcp://*:5561");
// Socket to receive signals
void *syncservice = zmq_socket (context, ZMQ_REP);
zmq_bind (syncservice, "tcp://*:5562");
// Get synchronization from subscribers
int subscribers = 0;
while (subscribers < SUBSCRIBERS_EXPECTED) {
// - wait for synchronization request
char *string = s_recv (syncservice);
free (string);
// - send synchronization reply
s_send (syncservice, "");
subscribers++;
}
// Now broadcast exactly 1M updates followed by END
int update_nbr;
for (update_nbr = 0; update_nbr < 1000000; update_nbr++)
s_send (publisher, "Rhubarb");
s_send (publisher, "END");
zmq_close (publisher);
zmq_close (syncservice);
zmq_term (context);
return 0;
}