【发布时间】:2021-10-20 09:37:12
【问题描述】:
我有一个基本上做两件事的程序:
打开一个 websocket 并继续监听消息并在一个永远循环中启动视频流。
我试图使用多进程来管理这两件事,但一个阻止另一个运行。
应用是
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(start_client())
async def start_client():
async with WSClient() as client:
pass
class WSClient:
async def __aenter__(self):
async with websockets.connect(url,max_size= None) as websocket:
self.websocket = websocket
await self.on_open() ## it goes
p = Process(target = init(self)) # This is the streaming method
p.start()
async for message in websocket:
await on_message(message, websocket) ## listen for websocket messages
return self
init方法是
def init(ws):
logging.info('Firmware Version: ' + getVersion())
startStreaming(ws)
return
基本上 startStreaming 里面有一个无限循环。
在此配置中,流启动但 websocket 的 on_message 未被调用,因为 Process 函数冻结了应用程序的其余部分。
如何同时运行这两种方法?
谢谢
【问题讨论】:
标签: python python-multiprocessing