python - python asyncio通过回调函数获取数据
问题描述
我有一个第三方代码向我发送数据(不一定在当前循环中),我使用我提供的回调方法获得这些数据,我希望我的 websocket 初始化第三个 patry 实例一次并发送我从中获得的所有内容(ws.send )。
如果我使用执行器,它可以工作,但它会阻止我获得多个连接。
不起作用的示例代码:
async def callback(ws, event_type, message):
await ws.send(message)
async def producer(ws):
func = functools.partial(callback, ws, event_type)
ThirdPartyCode.enable(func)
while True:
await asyncio.sleep(0.2)
def main():
start_server = websockets.serve(producer, 'localhost', PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
解决方案
推荐阅读
- node.js - Firestore 查询 endBefore 和 endAt 未按预期工作
- android - avdmanager 的设备列表与 Android Studio 的不同
- python - Python Tkinter:如何在没有崩溃的情况下在按钮中制作无限循环功能?
- database - 无法使用提供的连接字符串连接到 MongoDB Compass
- python - Python:对嵌套列表进行排序
- c# - 如何对 ClosedXML 中的列值求和
- python - 如何在运行时返回一行代码?
- javascript - 如何在 D3.js 中指定树的最后一个元素的标签名称?
- c# - 如何在Window应用程序datagridview中对一列求和
- mysql - ID 存在于表中,但未在 select 语句中获取