[英]Starting an independent ApplicationSession and event loop from an existing one
我希望能夠從現有的事件循環中生成一個獨立的事件循環/反應器。 假設我在一個模塊standaloneapps
有一個應用程序:
#in standaloneapps.py
class StandaloneApp(ApplicationSession):
def runner(self, message):
print(message)
@inlineCallbacks
def start_app(self):
yield self.subscribe(self.runner, 'com.example.some_topic')
我希望能夠從另一個應用程序啟動該應用程序。 例:
from standaloneapps import StandaloneApp
class ApplicationStarter(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
yield self.subscribe(self.start_app, 'com.example.startapp')
def start_app(self, message):
print('subscribing to app')
new_runner = ApplicationRunner(url="ws://127.0.0.1:8080/ws",
realm="realm1")
runner.run(StandaloneApp)
我可以啟動ApplicationStarter
,但是一旦發布事件'com.example.startapp'
交叉開關就會崩潰,並拋出異常builtins.Exception: not joined
。
也許這似乎是一個過於復雜的設置,但是我正在嘗試讓一個應用程序訂閱一個“應用程序分派器”,該分派器會動態啟動可能事先未知的新應用程序。 我希望新的應用程序可以在不同的事件循環上運行,以便保持隔離狀態。
編輯:只需添加以下內容即可:Runner.run(StandaloneApp,start_reactor = False)
調用以啟動第二個應用程序。 這迫使它在同一循環中運行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.