【发布时间】:2018-05-22 18:22:50
【问题描述】:
我是 Python3 asyncio 的新手。
我有一个函数可以不断地从 websocket 连接中检索消息。
我想知道是否应该以递归方式使用while True 循环或asyncio.ensure_future。
哪个是首选还是无关紧要?
示例:
async def foo(websocket):
while True:
msg = await websocket.recv()
print(msg)
await asyncio.sleep(0.0001)
或
async def foo(websocket):
msg = await websocket.recv()
print(msg)
await asyncio.sleep(0.0001)
asyncio.ensure_future(foo(websocket))
【问题讨论】: