代码之家  ›  专栏  ›  技术社区  ›  user90150

zeromq持久性模式

  •  11
  • user90150  · 技术社区  · 15 年前

    谁必须管理ZeroMQ中的持久性?

    当我们在Python语言中使用ZeroMQ客户机时,有哪些插件/模块可用于管理持久性?

    3 回复  |  直到 15 年前
        1
  •  9
  •   P̲̳x͓L̳    9 年前

    据我所知,Zeromq没有任何持久性。它超出了它的范围,需要由最终用户处理。就像序列化消息一样。

        2
  •  3
  •   Name    14 年前

    在应用程序端,您可以相应地持久化,例如,我在node.js中构建了一个持久层,它通过websockets与后端php调用通信。

    持久性方面将消息保存了一段时间(http://en.wikipedia.org/wiki/time_to_live),这是为了给客户一个连接的机会。我使用内存中的数据结构,但我想用redis来获得磁盘上的持久性。

        3
  •  1
  •   marko.ristin    8 年前

    我们需要在处理来自订户的消息之前将其持久化。消息在单独的线程中接收并存储在磁盘上,而持久化消息队列在主线程中进行操作。

    该模块位于: https://pypi.org/project/persizmq

    import pathlib
    
    import zmq
    
    import persizmq
    
    context = zmq.Context()
    subscriber = context.socket(zmq.SUB)
    subscriber.setsockopt_string(zmq.SUBSCRIBE, "")
    subscriber.connect("ipc:///some-queue.zeromq")
    
    persistent_dir = pathlib.Path("/some/dir")
    storage = persizmq.PersistentStorage(persistent_dir=persistent_dir)
    
    def on_exception(exception: Exception)->None:
        print("an exception in the listening thread: {}".format(exception))
    
    with persizmq.ThreadedSubscriber(
        callback=storage.add_message, subscriber=subscriber, 
        on_exception=on_exception):
    
        msg = storage.front()  # non-blocking
        if msg is not None:
            print("Received a persistent message: {}".format(msg))
            storage.pop_front()