我想在用户中断对我的龙卷风HTTP服务器的请求时处理龙卷风(ver = 4.0),但是我不知道何时发生此事件。例如,我启动服务器,然后运行curl命令来请求它,然后我在请求它时中断卷曲。请帮助,谢谢!
这是我的手术
我阅读了文档,发现了on_finish
方法,但是当请求完成时将调用该方法。
# coding=u8
import time
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
print('Start %s.'%time.strftime('%F %H:%M:%S'))
time.sleep(5)
self.write('ok.')
print('Finish %s.'%time.strftime('%F %H:%M:%S'))
def on_finish(self):
"""Called after the end of a request.
Override this method to perform cleanup, logging, etc.
This method is a counterpart to `prepare`. ``on_finish`` may
not produce any output, as it is called after the response
has been sent to the client.
"""
print('Cancelled %s.'%time.strftime('%F %H:%M:%S'))
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
如何知道在请求处理过程中客户何时取消请求?
答案 0 :(得分:0)
on_connection_close()
方法来检测客户端何时关闭连接。time.sleep
,因为它是一项阻止功能。当您使用阻止功能时,Tornado的某些异步功能将无法使用。而是使用gen.sleep
之类的非阻塞替代方法。 示例:
from tornado import gen
class MainHandler(tornado.web.RequestHandler):
@gen.coroutine
def get(self):
yield gen.sleep(5) # asynchronous sleep
# ... do something else
def on_connection_close(self):
print('Cancelled %s.'%time.strftime('%F %H:%M:%S'))