ZeroMQ: Need to sleep before shipping

I am writing a demo version of zeromq with a Forwarder device (with pyzmq)

Here are the codes (link to https://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/devices/forwarder.html ):

forwarder.py

import zmq context = zmq.Context() frontend = context.socket(zmq.SUB) frontend.bind('tcp://*:5559') frontend.setsockopt(zmq.SUBSCRIBE, '') backend = context.socket(zmq.PUB) backend.bind('tcp://*:5560') zmq.device(zmq.FORWARDER, frontend, backend) 

sub.py

 import zmq context = zmq.Context() socket = context.socket(zmq.SUB) socket.connect('tcp://localhost:5560') socket.setsockopt(zmq.SUBSCRIBE, '') while True: print socket.recv() 

pub.py

 import zmq, time context = zmq.Context() socket = context.socket(zmq.PUB) socket.connect('tcp://localhost:5559') # time.sleep(0.01) socket.send('9 hahah') 

I run python forwarder.py , python sub.py in terminal

then run python pub.py , the subscriber will not be able to receive the message. However, if I sleep a little time (for example, 0.01 s) before sending, it works.

So my problem is why should I sleep before sending? thanks.

+6
source share
1 answer

He is known as Slow Joiner . Read the guide , there are ways to avoid this using pub / sub syncing .

+12
source

Source: https://habr.com/ru/post/956215/


All Articles