我实现了一个类,它连接到websocket以接收异步消息。在
class websocketClient(...):
def on_message(self, msg): # Gets automatically called
notifyBrokerAbout(msg) # How to proceed from here?
class Broker():
self.websocket = websocketClient(...)
self.websocket.start()
def heartbeat(self):
return self.websocket
heartbeat函数启动websocket并返回它。websocket类本身有一个on_message()
函数,它充当每次收到新消息时触发的回调函数。在
我不想时不时地调用一个函数来GET
从websocket输出到代理的最新的msg
,相反,我想在这些消息到达on_message()
时立即将它们发送给代理。在
如何用Python最好地解决这个问题?我正在寻找一种方法,将来自websocketClient
son_message()
的新消息推送到heartbeat()
,以便在主应用程序中订阅该函数并等待更新。在
这可以通过使用reactor模式异步处理请求,然后进行同步调度来解决。在
通过搜索“reactor gevent”和“reactor twisted”,您会找到许多很好的例子。有几种变化。我将避免推荐特定的变体,因为总体选择将取决于示例代码中不明显的其他系统级问题。在
相关问题 更多 >
编程相关推荐