PyZMQ PUSH 套接字不会在 send() 上阻塞 [英] PyZMQ PUSH socket does not block on send()

查看:27
本文介绍了PyZMQ PUSH 套接字不会在 send() 上阻塞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ZMQ 套接字文档中的 ZMQ_PUSH 部分 假设在没有下游节点的 PUSH 套接字上调用 send() 应该阻塞,直到至少有一个节点可用.

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.

但是,运行以下代码似乎不会阻止 send().此外,在我运行匹配的 PULL 套接字并接收消息之前,该进程不会退出:

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'

输出:

Creating a PUSH socket
Connecting
Sending
Sent

我是否遗漏了什么,或者这是 PyZmq 中的一个错误?

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

版本信息:Windows 7、Python 2.7、PyZMQ 14.0.1

编辑
经过一番摆弄之后,似乎如果我将 sender.connect('tcp://localhost:5555) 替换为 sender.bind('tcp://127.0.0.1:5555),它按预期工作.不过不确定它是如何相关的.

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.

推荐答案

在您给出的示例中未命中 HWM 条件.当您打开一个连接时,甚至在建立对等连接之前就会创建一个缓冲区.仅当此缓冲区已满时才会命中 HWM.例如:

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

其中 HWM 设置为 1.在这种情况下,第一个消息将被缓冲,第二个发送将阻塞,直到第一个消息真正传输完毕.

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.

这篇关于PyZMQ PUSH 套接字不会在 send() 上阻塞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆