Zeromq 多个订阅者

Zeromq multiple subscribers

我正在尝试将视频流从一个程序发送到任意数量的客户端(比如 0-10 个客户端)

我可以通过一个发布者和一个订阅者轻松地做到这一点,但是如果我启动另一个客户端应用程序,它不会开始接收任何数据,直到我关闭第一个应用程序。

我的代码如下所示:
服务器

import zmq
import imageio
import simplejpeg


context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.connect("ipc:///tmp/v4l")



gif = imageio.get_reader('video.mp4')

while True:
    for frame in gif:
        frame = simplejpeg.encode_jpeg(frame)
        publisher.send(frame)

客户端

import zmq
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.bind("ipc:///tmp/v4l")
subscriber.setsockopt(zmq.SUBSCRIBE, b'')
i = 0
while True:
    data = subscriber.recv()
    print(i)
    i = i+1

在文档 (https://learning-0mq-with-pyzmq.readthedocs.io/en/latest/pyzmq/patterns/pubsub.html) 中指出

Scenario #2 is more known, general pattern where multiple subscribers subscribes to messages/topics being published by a publisher. though i cannot figure out why this does not work for me.

使发布者bind() 和多个订阅者connect()。您只能 bind/listen 连接到同一端点一次,但可以连接多次。