0

I would like to have two python scripts (it can be more in real use) that publishes on the same port to a single client. Here is the code for my scripts:

server1.py:

import time
import zmq

ctx = zmq.Context()

s1 = ctx.socket(zmq.PUB)
s1.connect("tcp://127.0.0.1:5566")

for i in range(10):
    s1.send_pyobj({'job':'publisher 1','yo':10})
    time.sleep(5)

server2.py:

import time
import zmq

ctx = zmq.Context()

s2 = ctx.socket(zmq.PUB)
s2.connect("tcp://127.0.0.1:5566")

for i in range(10):
    s2.send_pyobj({'job':'publisher 2','yo':10})
    time.sleep(5)

client.py:

import zmq

ctx = zmq.Context()
c = ctx.socket(zmq.SUB)
c.bind("tcp://127.0.0.1:5566")
c.setsockopt(zmq.SUBSCRIBE, '')

while True:
    msg = c.recv_pyobj()
    print("MSG: ", msg)

This naive implementation works but, being new to zmq,I was wondering if it was indeed the right implementation or if there was a better way to proceed.

Eurydice
  • 8,001
  • 4
  • 24
  • 37

1 Answers1

0

I think that your design is valid. However, as the comments to the answer in this similar question suggest, you might struggle to subscribe with multiple clients with this architecture.

Marcus
  • 401
  • 6
  • 13