Question

The ZMQ_PUSHsection in ZMQ socket documentation say that calling send() on PUSH socket, which has no downstream nodes should block until at least one node becomes available.

However, running the following code does not seem to block on send(). Also, the process does not exit until I run a matching PULL socket and receive the messages:

import zmq
import time

zmq_context = zmq.Context()
print '> Creating a PUSH socket'
sender = zmq_context.socket(zmq.PUSH)
print '> Connecting'
sender.connect('tcp://localhost:%s' % 5555)
print '> Sending'
sender.send('message 1')
print '> Sent'

Output:

Creating a PUSH socket
Connecting
Sending
Sent

Am I missing something, or is this a bug in PyZmq?

Version Info: Windows 7, Python 2.7, PyZMQ 14.0.1

EDIT
After some fiddling, it seems that if I replace sender.connect('tcp://localhost:5555) with sender.bind('tcp://127.0.0.1:5555), it works as expected. Not sure how its related, though.

Was it helpful?

Solution

The HWM condition is not hit in the example you gave. When you open a connection, a buffer is created even before the peer connection is established. HWM is only hit when this buffer is full. For example:

import zmq

zmq_context = zmq.Context()
print '> Creating a PUSH socket'
sender = zmq_context.socket(zmq.PUSH)
sender.hwm = 1
print '> Connecting'
sender.connect('tcp://localhost:%s' % 5555)
for i in range(3):
    print '> Sending', i
    sender.send('message %i' % i)
    print '> Sent', i

Where HWM is set to 1. In this case, the first message will be buffered, and the second send will block until the first message is actually transmitted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top