我熟悉事件驅動的編程,但是我遇到了這個問題,我終止了可能的解決方案。我讀了龍捲風的文檔,我試着用:使用Tornado web框架執行異步任務
- 期貨
- gen.coroutine
- 異步
- add_timeout
,但我沒能解決以下問題:
我有一個websocket服務器這只是聽新郵件 並根據消息類型調用特定的功能
類WebSocketHandler(tornado.websocket.WebSocketHandler):
... def on_message(self, message): if message['type'] is X: self.write(functionA(message['data'])) elif message['type'] is Y: self.write(functionB(message['data'])) ...
問題是當一個計算代價函數是執行,讓說功能A,它可能需要長達5分鐘終止
def functionA(message):
params = extract_params(message)
cmd = "computationally_expensive_tool"
out = check_output(cmd, shell=True, stderr=STDOUT, cwd=working_dir)
...
return json.dumps({
"error": False,
"message": "computationally_expensive_tool_execution_terminated",
"type": X
})
我的問題是我該如何執行該函數n以異步的方式處理,這樣我仍然可以處理其他消息和functionA的結果。
你能提供一個你的功能是什麼樣子的例子嗎? –
嗨@ notorious.no,謝謝你的關注。我添加了一些關於函數A和我想實現的目標的細節。所以functionA調用這個工具,當它結束時,我希望能夠通過發送消息來通知客戶。 – MrGoodKat