1 year ago

#288725

test-img

Zoravur Singh

Why my mock-up proxy implementation crashes on another Socket creation?

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?

python

zeromq

pyzmq

0 Answers

Your Answer

Accepted video resources