zeromq持久性模式

2024-05-11 07:49:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

我想知道使用ZeroMQ的模式。


Tags: 模块插件语言客户机模式持久性zeromq
3条回答

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

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

据我所知,Zeromq没有任何持久性。它超出了它的范围,需要由最终用户处理。就像序列化消息一样。 在C#中,我使用db4o来添加持久性。通常,我会将对象保持在原始状态,然后将其序列化并发送到ZMQ套接字。顺便说一下,这是给酒吧/小餐馆的。

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

模块位于: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()

相关问题 更多 >