【发布时间】:2021-05-28 06:51:15
【问题描述】:
我正在尝试使用多线程的 websocket (https://websockets.readthedocs.io/en/stable/)。我想要的是在我的程序继续运行时继续获取数据,但是,当我像下面的代码那样做时,服务器没有接收到来自客户端的任何连接。我之前在主线程上运行过它,它运行良好。
async def hello(websocket, path):
while True:
data = await websocket.recv()
print(data)
await websocket.send(data)
def between_callback():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
ws_server = websockets.serve(hello, '192.168.0.115', 8899)
loop.run_until_complete(ws_server)
loop.close()
if __name__ == "__main__":
_thread = threading.Thread(target=between_callback)
_thread.start()
# Do something in main thread
【问题讨论】:
-
你的主线程是做什么的?它加入线程了吗?
-
@KlausD。它正在使用从 websocket 接收到的数据来做其他事情。
标签: python multithreading websocket