Python使websockets侦听器保持活动状态

时间:2020-06-12 17:21:55

标签: python websocket python-multithreading

我正在尝试编写一个Python脚本,该脚本将连接到网络套接字并收听广播,并(现在)将它们打印到终端。理想情况下,我希望它在线程中运行,以便我可以继续执行其他命令。

这是我到目前为止的代码,它可以运行,但是在收到第一条消息后停止。

import websockets
import threading 
import asyncio


async def hello():
    uri = "mywebsocket_url"
    async with websockets.connect(uri) as websocket:
        msg = await websocket.recv()
        print(msg)


if __name__ == '__main__':
    meter_loop = asyncio.new_event_loop()
    meter_thread = threading.Thread(
        name='Meter',
        target=asyncio.get_event_loop().run_until_complete(hello()))

我已经阅读了websockets文档和stackoverflow,这个主题上有很多线程,但是似乎没有一个线程可以满足我的需求。如果有人可以提供帮助,将不胜感激。

0 个答案:

没有答案
相关问题