提问人:Evert Heylen 提问时间:2/22/2016 最后编辑:Evert Heylen 更新时间:4/14/2018 访问量:5068
如何在 Tornado 中使用 Python 3.5 风格的异步和等待进行 websockets?
How to use Python 3.5 style async and await in Tornado for websockets?
问:
请看这个简短的片段:
import tornado
import tornado.websocket
import tornado.ioloop
import tornado.gen
import tornado.web
class NewWsHandler(tornado.websocket.WebSocketHandler):
async def on_message(self, message):
await self.write_message("echo " + message)
class OldWsHandler(tornado.websocket.WebSocketHandler):
@tornado.gen.coroutine
def on_message(self, message):
yield self.write_message("echo " + message)
app = tornado.web.Application([(r'/', OldWsHandler)])
app.listen(8080)
tornado.ioloop.IOLoop.current().start()
OldWsHandler
在 Tornado 中使用 3.5 之前的异步函数方式,效果很好。但是,正如文档所述,最好使用 PEP 0492 以提高可读性和速度。
文档说:
只需使用装饰器代替函数定义,并代替 .
async def foo()
@gen.coroutine
await
yield
所以我写了.但是,在发送 websocket 消息时,它会引发警告:NewWsHandler
/usr/lib/python3.5/site-packages/tornado/websocket.py:417: RuntimeWarning: coroutine 'on_message' was never awaited callback(*args, **kwargs)
我真的不知道如何(正确)修复它。我尝试用 来装饰它,但这假设使用 HTTP 动词方法。因此,在我覆盖(不允许 websockets 这样做)之后,它似乎有点工作:tornado.web.asynchronous
finish()
class NewWsHandler(tornado.websocket.WebSocketHandler):
def finish(self):
pass
@tornado.web.asynchronous
async def on_message(self, message):
await self.write_message("echo " + message)
但这看起来仍然很黑客,并且似乎与文档相矛盾。正确的方法是什么?
注意:我使用的是 Python 3.5.1 和 Tornado 4.3。
答:
协程的调用方式与常规函数不同;因此,在子类化和重写方法时,不能将基类中的常规方法更改为子类中的协程(除非基类明确表示这是可以的)。 可能不是协程(从 Tornado 4.3 开始;这将在未来发生变化)。WebSocketHandler.on_message
相反,如果需要对消息执行异步操作,请将异步部分放在单独的函数中,并使用 调用它。(或者,如果这是您正在执行的唯一异步操作,请同步调用它)IOLoop.current().spawn_callback
write_message
更新
这在 Tornado 4.5 中发生了变化,现在可以用作协程。请参见 http://www.tornadoweb.org/en/stable/releases/v4.5.0.html#tornado-websocket。WebSocketHandler.on_message
评论
result = await query(...)
on_message
on_open()
评论