Posted on 2013-03-08 20:18:01 tornado
使用tornado的异步http调用时候,在继承RequestHandler的子类中,可以重载on_connection_close方法。
此方法在对端连接关闭,或者在socket上读写错误的时候被调用,可以让服务器做一些清理。
调用过程大概这样:
首先说明,ioloop没有保证读写出错的时候做什么,以及怎么做。 这些都是由iostream来操作的,因为它就是对ioloop的封装,增加了错误处理、各种情况下的回调、读写缓存等等。
def close(self, exc_info=False):
"""Close this stream.
If ``exc_info`` is true, set the ``error`` attribute to the current
exception from `sys.exc_info()` (or if ``exc_info`` is a tuple,
use that instead of `sys.exc_info`).
"""
if not self.closed():
if exc_info:
if not isinstance(exc_info, tuple):
exc_info = sys.exc_info()
if any(exc_info):
self.error = exc_info[1]
if self._read_until_close:
callback = self._read_callback
self._read_callback = None
self._read_until_close = False
self._run_callback(callback,
self._consume(self._read_buffer_size))
if self._state is not None:
self.io_loop.remove_handler(self.fileno())
self._state = None
self.close_fd()
self._closed = True
self._maybe_run_close_callback()
最后一句,就是self._maybe_run_close_callback(),说明如果定义了on_connection_close函数,就调用它。在这句代 码的前面做了一些例如关闭连接、清除回调、从ioloop中移除回调等等。
It’s in point of fact a great and helpful piece of info. I am happy that you shared this useful information with us. Please stay us up to date like this. Thanks for sharing.
My Website: Australian Open
You there, this is really good post here. Thanks for taking the time to post such valuable information. pba odds betting