Python3以前 tornado的异步,是通过yield @gen.coroutine
@run_on_executor等来实现,但现在基本用asyncio替代的,使用async 和wait:
class WebSocketServerHandler(WebSocketHandler):
async def on_message(self, message):
await self.send_end(self.pair_token)
async def send_end(self, token):
json_str = dict()
json_str["command"] = "end_help"
json_str["token"] = token
await self.write_message(json.dumps(json_str))
pass
Tornado到AsyncIO:Python异步编程进化
本文探讨了Python中从Tornado到AsyncIO的异步编程变迁,展示了如何使用AsyncIO进行WebSocket消息处理,包括@run_on_executor到async/await的转变。
913

被折叠的 条评论
为什么被折叠?



