我按照github页面上的说明在我的Manjaro Linux机器上从源代码构建了ZeroMQ . 所有测试都通过 .

基本上,

./autogen
./configure
./make
./make install
./make check

我在Python中使用指南运行气象站示例,并使用tcp通过家庭WiFi从笔记本电脑发送/接收消息到我的PC .

服务器:

#
#   Weather update server
#   Binds PUB socket to tcp://*:5556
#   Publishes random weather updates
#

import zmq
from random import randrange

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

while True:
    zipcode = 10001
    temperature = randrange(-80, 135)
    relhumidity = randrange(10, 60)

    socket.send_string("%i %i %i" % (zipcode, temperature, relhumidity))

客户:

#
#   Weather update client
#   Connects SUB socket to tcp://localhost:5556
#   Collects weather updates and finds avg temp in zipcode
#

import sys
import zmq

#  Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print("Collecting updates from weather server…")
socket.connect("tcp://192.168.1.2:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"

# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
    zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
total_temp = 0
for update_nbr in range(500):
    string = socket.recv_string()
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

print("Average temperature for zipcode '%s' was %dF" % (
      zip_filter, total_temp / update_nbr)
)

有时,订阅者将无法检测到发布者发送的消息,订阅者将等待下一条消息进入 . 发布者将继续发送消息,但一旦订阅者无法接收消息,将不再接收消息消息 . 我发现重新启动发布者会导致订阅者再次开始接收消息 .

我可以理解正在丢弃的消息,但任何人都可以向我解释为什么一旦发生这种情况,订阅者就无法接收更多消息?我做错了吗?

我相信这可能是一个构建问题,因为当我在基于apt-get安装的docker中运行相同的示例代码时,我永远不会丢失任何消息,它似乎永远不会失败 . 我需要重建,因为我最终想要使用NORM .

谢谢 .