python - 为什么 ZeroMQ 轮询器没有收到消息(python)?

标签 python sockets zeromq polling pyzmq

我正在尝试使用 ZeroMQ Poller() 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 发送的服务器被描述为:
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()
但是当我运行这两个进程时,它没有按预期工作,轮询脚本不打印任何内容。这种实现可能有什么问题?

最佳答案

Q : "What could be the problem of such implementation?"


  • 由于只使用 .poll() 的阻塞形式,这种实现很容易出现死锁和失败。 & .recv()方法
  • 在多个对等点连接到 AccessPoints 的情况下,这种实现不够自我保护,实现循环传入/传出流量映射
  • 这样的实现是非常错误的,因为只调用一个 .recv()在这种情况下,.send_multipart()是惊人的警告,将需要多部分消息处理
  • ipc://传输类容易隐藏与 O/S 相关的用户级代码限制(由操作系统放置在路径名的格式和长度上,以及对 R/W/X 的有效用户权限)
  • ipc://运输类 .connect() -method 的使用取决于 O/S 服务尚未创建目标地址的情况(需要先成功 .bind())
  • 最后但并非最不重要的是,任何下一次尝试 .bind()到同一个 ipc://运输级目标将无声无息地摧毁你的意图 ROUTER - 访问消息/信令平面基础设施,您的实现花费零努力来自我保护和自我诊断可能默默出现在“幕后”的错误

  • 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 专注于性能 + 低延迟,因此请考虑将所有应有的防止阻塞的注意事项掌握在自己手中(根据需要和需要,核心库永远不会执行比需要更多的步骤以实现几乎线性可扩展性能的目标)。
    不,两者都可以自由使用 .poll() - & .recv() -methods,但完成它以适应非阻塞方式 - .poll( 0 ) & 添加主动检测 + 多部分消息的处理(同样,最好以非阻塞方式,在适当的情况下使用 zmq.NOBLOCK 选项标志)。自阻塞使代码失控。

    关于python - 为什么 ZeroMQ 轮询器没有收到消息(python)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64608767/

    相关文章:

    python - 找不到docker discord.py ffmpeg.exe

    python - 通过仅获取元组的第一个值来重新格式化元组列表时出现问题?

    c++ - 构造函数失败后初始化 boost::asio 套接字

    java - 处理java.net.SocketException : Socket closed when multithreading?

    python - 如何使用 Zeromq 的 inproc 和 ipc 传输?

    Python RegEx - 如何处理字符串中的可选部分

    python - 如何使用 jira-python 检索与 JIRA 票证关联的 'links'?

    c# - Socket.Select 返回错误 "An operation was attempted on something that is not a socket"

    java - 在哪里可以找到基于 TLS 的 zeromq 的 Java 示例?

    c++ - ZeroMQ:如何使用多个发布者和单个客户端,使用 C < C11