为什么 ZeroMQ 轮询器没有收到消息(python)? [英] Why is ZeroMQ poller not receiving messages (python)?

查看:83
本文介绍了为什么 ZeroMQ 轮询器没有收到消息(python)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 中使用带有两个套接字的 ZeroMQ Poller() 功能:

I'm trying to use the ZeroMQ Poller() functionality with two sockets in python:

import zmq

# Prepare our context and sockets
context = zmq.Context()

receiver = context.socket(zmq.DEALER)
receiver.connect("ipc:///tmp/interface-transducer")

subscriber = context.socket(zmq.SUB)
subscriber.bind("ipc:///tmp/fast-service")
subscriber.setsockopt(zmq.SUBSCRIBE, b"10001")

# Initialize poll set
poller = zmq.Poller()
poller.register(receiver, zmq.POLLIN)
poller.register(subscriber, zmq.POLLIN)

# Process messages from both sockets
while True:
    try:
        socks = dict(poller.poll())
    except KeyboardInterrupt:
        break

    if receiver in socks:
        message = receiver.recv()
        print("RECEIVER OK\n")

    if subscriber in socks:
        message = subscriber.recv()
        print("SUBSCRIBER OK\n")

然后作为ROUTER发送消息的服务器被描述为:

And then the server that sends messages as a ROUTER is described as:

def main():
    context = zmq.Context()
    router = context.socket(zmq.ROUTER)
    router.bind("ipc:///tmp/interface-transducer")
    while True:
        identity = b'electrode-service'
        b_identity = identity
        router.send_multipart([b_identity, b'[1,2]'])
        print("Sent")
        time.sleep(1)

if __name__ == "__main__":
    main()

但是当我运行这两个进程时,它没有按预期工作,轮询脚本不打印任何内容.这样的实现可能有什么问题?

But when I run these two processes, it does not work as expected, the poller-script does not print anything. What could be the problem of such implementation?

推荐答案

Q:这种实现可能有什么问题?"

  • 这样的实现容易出现死锁&由于只使用 .poll() 的阻塞形式而失败 &.recv() 方法

    • such implementation is prone to deadlock & fails due to using exclusively the blocking-forms of .poll() & .recv() methods

      在多个对等点连接到 AccessPoints、实现循环传入/传出流量映射的情况下,这种实现不够自卫

      such implementation is not self-defending enough in cases, where multiple peers get connected into AccessPoints, that implement round-robin incoming/outgoing traffic mappings

      这种实现是非常错误的,在这种情况下,.send_multipart() 是惊人的,只调用一个 .recv()警告,将需要多部分消息处理

      such implementation is awfully wrong in standing self-blinded in calling just a single .recv() in cases, where the .send_multipart() is strikingly warning, there will be multi-part message-handling needed

      ipc:// 传输类容易隐藏 O/S 相关的用户级代码限制(由操作系统放置在路径名的格式和长度以及有效用户级别上)那里的 R/W/X 权利 )

      ipc:// Transport Class is prone to hide O/S related user-level code restrictions ( placed by the operating system on the format and length of a pathname and effective user-rights to R/W/X there )

      ipc:// 传输类 .connect()-method 的使用是顺序依赖的,用于目标地址尚未由 O 创建的情况/S 服务(成功的 .bind() 需要先发生)

      ipc:// Transport Class .connect()-method's use is order-dependent for cases the target-address has not yet been created by O/S services ( a successful .bind() needs to happen first )

      最后但并非最不重要的一点是,任何下一次尝试将 .bind() 连接到同一个 ipc:// 传输类目标都将无声地破坏您想要的 ROUTER-访问消息/信令平面基础设施您的实施花费了零努力来自我保护和自我诊断可能默默出现在幕后"的错误

      last but not least, any next attempt to .bind() onto the same ipc:// Transport Class target will silently destroy your intended ROUTER-access to the messaging/signalling-plane infrastructure & your implementation has spent zero-efforts to self-protect and self-diagnose errors that might silently appear "behind the curtains"

      zeromq 不应该自动处理死锁吗?我尝试使用 zeromq 指南中给出的示例 mspoller 如果我不能同时使用 .poll() 和 recv(),我应该如何使用 ZMQ Poller 结构?– hao123

      Shouldn't zeromq deal automatically with deadlocks? I tried using the example given in the zeromq guide mspoller If I can't use .poll() and recv() simultaneously, how should I use ZMQ Poller structure? – hao123


      ZeroMQ zen-of-zero 专注于性能 + 低延迟,因此请考虑将所有应有的注意事项都掌握在自己手中(根据需要和在需要时,核心库永远不会执行比实现几乎线性可扩展性能目标所需的更多步骤).

      No,
      ZeroMQ zen-of-zero is performance + low-latency focused, so kindly consider all due care for blocking-prevention to be in your own hands (as needed & where needed, the core lib will never do a single more step than needed for the goal of achieving an almost linear scalable performance ).

      不,
      自由使用 .poll()- &.recv()-methods,但完成它以适应非阻塞方式 - .poll( 0 ) &添加主动检测 + 多部分消息处理(同样,最好以非阻塞方式,在适当的情况下使用 zmq.NOBLOCK 选项标志).自阻塞使代码失控.

      No,
      use freely both .poll()- & .recv()-methods, yet complete it so as to fit into a non-blocking fashion - .poll( 0 ) & add active detection + handling of multi-part messages ( again, best in a non-blocking fashion, using zmq.NOBLOCK option flag where appropriate ). Self-blocking gets code out of control.

      这篇关于为什么 ZeroMQ 轮询器没有收到消息(python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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