【发布时间】:2015-03-03 18:43:19
【问题描述】:
我对 WebSocketHandler 的方法 open 有问题。 我用 gen.coroutine 包装它,以便在内部使用异步调用(访问 redis)。但是还有一个问题,open 中的任何错误都不会被捕获。
例子:
@gen.coroutine
def open(self):
t = 8/0
self._connection_id = yield self._generate_connection_id()
self.write_message('...')
方法 open 在 WebSocketProtocol._run_callback 内部调用:
def _run_callback(self, callback, *args, **kwargs):
try:
callback(*args, **kwargs)
except Exception:
app_log.error("Uncaught exception in %s",
self.request.path, exc_info=True)
self._abort()
这个方法没有任何装饰器,所以方法打开返回future并且这个异常没有被处理。
那么如何在 open 中使用 async 方法,并处理异常?
【问题讨论】:
标签: asynchronous websocket tornado coroutine