PyZMQ PUSH socket does not block on send()

∥☆過路亽.° 提交于 2020-01-13 06:51:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/21759094/pyzmq-push-socket-does-not-block-on-send

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!