Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ: have to sleep before send

I'm write a zeromq demo with Forwarder device (with pyzmq)

Here are the codes(reference 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 the terminal

then run python pub.py, the subscriber can't get the message. However, if I sleep a little time(for example 0.01s) before send, it works.

So my problem is, why have I sleep before send? thanks.

like image 311
wong2 Avatar asked Oct 18 '13 06:10

wong2


People also ask

Is ZeroMQ fast?

It's fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems.

Is ZeroMQ asynchronous?

ZeroMQ is an asynchronous network messaging library known for its high performance.

Is ZeroMQ a message broker?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is an asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker; the zero in the name is for zero broker.

What is ZeroMQ used for?

ZeroMQ is a library used to implement messaging and communication systems between applications and processes - fast and asynchronously.


1 Answers

It's known as Slow Joiner syndrome. Read the guide, there are ways to avoid it using pub/sub syncing.

like image 54
raffian Avatar answered Sep 21 '22 01:09

raffian