Inter-service messaging/bus for twisted

2024-05-20 00:38:32 发布

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

是否有一种将消息传递给另一个进程内服务的扭曲机制?我写了一个巴士原型

    from collections import defaultdict
    channels = defaultdict(list)

    def registerSingle(name, callback):
        """
            Similar to register but ensures only one callback is register to a channel
            @todo change Exception to something more appropriate

            :name str A reasonably coherent name for a callback channel
            :callback callable Either a bound method or just a function
        """
        global channels
        if len(channels[name]) > 0:
            raise Exception("Tried to register %s but already has %s registered" % ( name, channels) )
        channels[name].append(callback)

    def register(name, callback):
        """
            Binds a callback to a named channel

            :name str A reasonably coherent name for a callback channel
            :callback callable Either a bound method or just a function
        """
        global channels
        channels[name].append(callback)


    def call(name, *args, **kwargs):
        """
            Applies the provided arguments to any and all callbacks for a specified channel

            :name str A reasonably coherent name for a callback channel
        """
        for callback in channels[name]:
            callback(*args, **kwargs)

被用作

在食品在

^{pr2}$

在棒.py在

 from Application.data import bus

 bus.call("foo.doSomething", "A simple string")

这是一个非常简单的例子,因为主要用例是共享一个通用的内存数据存储。最初我尝试使用单例实例,但在试图用单元测试覆盖它时遇到了太多问题。然后,我尝试在任何地方传递对数据存储的引用,但是我觉得我的应用程序100%依赖于数据存储而从不改变。在

我唯一关心的是数据总线这个想法是,它基本上只是一个过度美化的全局变量。所以我的问题是,twisted内部是否有某种服务总线或消息传递系统,允许在twisted应用程序内的不同资源之间传递任意消息,还是我的数据总线有什么好办法吗?在


Tags: to数据namefromimportregisterfordef