2011-08-09 36 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