インプロセスサービスを別のインプロセスサービスに渡すためのねじれたメカニズムはありますか?私は本当に簡単な例ですサービス間メッセージ/バスがツイストされた
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")
のように使用するために
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)
のように見えるプロトタイプバスを書きました主な使用事例は、共通のメモリデータストアを共有することです。最初はシングルトンを使ってみましたが、単体テストでカバーしようとすると問題が多すぎました。私はどこにでもデータストアへの参照を渡そうとしましたが、データストアに100%依存しないようにアプリケーションを結びつけていると感じました。
私がdata.busの考え方に気がついたのは、それが基本的には栄光に満ちたグローバル変数だということだけでした。だから私の質問は、ツイストアプリケーション内の別のリソース間で任意のメッセージを渡すことができるようにねじれの内部にサービスバスやメッセージングシステムのいくつかの並べ替えがあるか、それはソリューションのために得て良い私のdata.busアイデアですか?
残念ながら、ヨーク大学ではネットワーキングの問題があるようです。 pipインストール404のものであり、指定されたホームページも欠落している。 – David
ここにGoogleコードで代替ソースが見つかりました。http://code.google.com/p/pylinda/ – David
Twistedはおそらくここで何も変更すべきではないことに同意します。特に、インプロセスメッセージングの場合にはそうです。 – Glyph