为什么我的模拟代理实现在另一个 Socket 创建时崩溃?

发布于 2025-01-14 03:44:34 字数 1918 浏览 0 评论 0原文

我遇到了 PyZMQ 代理的问题,我不太明白。当我取消注释看似不相关的代码行时,代理似乎可以工作。这是一个 MWE:

minimal_proxy.py:

    from multiprocessing import Process
    import zmq
    
    
    def pubsubproxy(ctx: zmq.Context):
        receiver = ctx.socket(zmq.SUB)
        receiver.connect("tcp://localhost:51121")
        receiver.setsockopt_string(zmq.SUBSCRIBE, "")
    
        sender = ctx.socket(zmq.PUB)
        sender.bind("tcp://*:51131")
    
        # Pub/sub  Proxy
        zmq.proxy(receiver, sender)
    
    def main():
        ctx = zmq.Context.instance()
    
        # capture = ctx.socket(zmq.PUSH) # when uncommented, proxy fails
        p = Process(target=pubsubproxy, args=(ctx,))
        p.start()
        p.join()
    
    if __name__ == '__main__':
        main()

proxy_tester.py:

import zmq
from multiprocessing import Process
import time

def receive(ctx: zmq.Context):
    receiver = ctx.socket(zmq.SUB)
    receiver.connect("tcp://localhost:51131")
    receiver.setsockopt_string(zmq.SUBSCRIBE, "")

    while True:
        msg = receiver.recv() # wait for timestamp messages relayed via proxy
        print(msg) # print message to stdout when received

def send(ctx: zmq.Context):
    sender = ctx.socket(zmq.PUB)
    sender.bind("tcp://*:51121")

    while True:
        sender.send_string(f"timestamp: {time.time()}")
        time.sleep(1)


def main():
    ctx = zmq.Context.instance()

    p1 = Process(target=receive, args=(ctx,))
    p2 = Process(target=send, args=(ctx,))
    p1.start()
    p2.start()

    p1.join()
    p2.join()

if __name__ == '__main__':
    main()

如您所见,proxy_tester 在端口 51121 上打开一个发布者,在端口 51131 上打开一个接收者,并期望minimal_proxy 连接这两个端口。不幸的是,当我尝试创建一个新的套接字捕获(据我所知与代理完全无关)时,我停止从 proxy_tester 中的接收器接收时间戳消息。 zmq.VERSION 返回 40203。这是怎么回事?

I ran into an issue with PyZMQ's proxy that I don't really understand. It seems that the proxy works when I uncomment a seemingly unrelated line of code. Here's an MWE:

minimal_proxy.py:

    from multiprocessing import Process
    import zmq
    
    
    def pubsubproxy(ctx: zmq.Context):
        receiver = ctx.socket(zmq.SUB)
        receiver.connect("tcp://localhost:51121")
        receiver.setsockopt_string(zmq.SUBSCRIBE, "")
    
        sender = ctx.socket(zmq.PUB)
        sender.bind("tcp://*:51131")
    
        # Pub/sub  Proxy
        zmq.proxy(receiver, sender)
    
    def main():
        ctx = zmq.Context.instance()
    
        # capture = ctx.socket(zmq.PUSH) # when uncommented, proxy fails
        p = Process(target=pubsubproxy, args=(ctx,))
        p.start()
        p.join()
    
    if __name__ == '__main__':
        main()

proxy_tester.py:

import zmq
from multiprocessing import Process
import time

def receive(ctx: zmq.Context):
    receiver = ctx.socket(zmq.SUB)
    receiver.connect("tcp://localhost:51131")
    receiver.setsockopt_string(zmq.SUBSCRIBE, "")

    while True:
        msg = receiver.recv() # wait for timestamp messages relayed via proxy
        print(msg) # print message to stdout when received

def send(ctx: zmq.Context):
    sender = ctx.socket(zmq.PUB)
    sender.bind("tcp://*:51121")

    while True:
        sender.send_string(f"timestamp: {time.time()}")
        time.sleep(1)


def main():
    ctx = zmq.Context.instance()

    p1 = Process(target=receive, args=(ctx,))
    p2 = Process(target=send, args=(ctx,))
    p1.start()
    p2.start()

    p1.join()
    p2.join()

if __name__ == '__main__':
    main()

As you can see, proxy_tester opens up a publisher on port 51121 and a receiver on port 51131, and expects minimal_proxy to connect the two. Unfortunately when I try to create a new socket capture (which to my knowledge is completely unrelated to the proxy), I stop receiving timestamp messages from the receiver in proxy_tester. zmq.VERSION returns 40203. What's going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文