在未抛出异常的情况下的接口调用顺序为:
set_default_headers
initialize
prepare
http func
finish
在抛出异常的情况下的:
set_default_headers
initialize
prepare
http func
set_default_headers
write_error
finish
测试代码如下,为抛出异常情况下只需要将get方法里面的send_error注释掉
import tornado
import tornado.web
import tornado.ioloop
class indexHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
print('http func')
self.send_error(200)#在此处抛出一个error
def set_default_headers(self):
print('set_default_headers')
def initialize(self):
print('initialize')
def prepare(self):
print('prepare')
def write_error(self, status_code, **kwargs):
print('write_error')
def on_finish(self):
print('finish')
if __name__ == '__main__':
app=tornado.web.Application([
('/index',indexHandler)
]
)
app.listen(8888)
tornado.ioloop.IOLoop.instance().start()