我已经按照github页面上的说明在我的Manjaro Linux机器上从源代码构建了ZeroMQ。所有测试都通过了。在
基本上./autogen
./configure
./make
./make install
./make check
我用Python运行指南中的气象站示例,并使用tcp通过家庭WiFi从ly笔记本电脑向我的PC发送/接收消息。在
服务器:
^{pr2}$
客户:#
# 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。在
谢谢。在