2011-08-09 35 views
2

是否存在将消息进程中服务传递给另一个服务的扭曲机制?我写了一个原型总线,看起来像扭曲的服务间消息传递/总线

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) 

要像

foo.py

from Application.data import bus 

def doSomething(fooArg): 
    print "Hello from Foo, you sent " , fooArg 

bus.register("foo.doSomething", doSomething) 

bar.py

from Application.data import bus 

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

这是一个非常简单的例子,如使用主要用例是共享内存数据存储。最初我尝试使用单例,但试图用单元测试覆盖它时遇到了太多问题。然后,我试着将数据存储的引用传递给了每个地方,但是感觉我正在将我的应用程序绑定到100%依赖于永不改变的数据存储。

我唯一关心的是data.bus思想,它基本上只是一个过度美化的全局变量。所以我的问题是,是否有某种服务总线或消息系统在内部扭曲,以允许在扭曲的应用程序内的不同资源之间传递任意消息,还是我的data.bus理念与解决方案一样好?

回答

1

它听起来像你想为python“黑板”或“元组空间”(我看不到扭曲改变了什么?)?如果是的话,这里有一个 - http://pypi.python.org/pypi/linda/0.5.1

+0

不幸的是,它看起来像约克大学可能存在网络问题。 PIP安装404's和指定的主页也丢失。 – David

+0

在此处的Google代码中找到替代来源http://code.google.com/p/pylinda/ – David

+0

我同意Twisted可能不应该在此更改任何内容,尤其是如果它用于进程内消息传递。 – Glyph