我的python tornado代码没有';不行。。。为什么?
我有两个处理程序类来比较@tornado.web.asynchronous decorator我的python tornado代码没有';不行。。。为什么?,python,tornado,Python,Tornado,我有两个处理程序类来比较@tornado.web.asynchronous decorator class Test1Handler(tornado.web.RequestHandler): def get(self): for i in range(1, 100000): print "kill time" self.write("hello") class Test2Handler(tornado.web.
class Test1Handler(tornado.web.RequestHandler):
def get(self):
for i in range(1, 100000):
print "kill time"
self.write("hello")
class Test2Handler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
http = tornado.httpclient.AsyncHTTPClient()
http.fetch("http://localhost:8005/test1", callback=self._test_callback)
self.write("Hello to the Tornado world! ")
def _test_callback(self, response):
print response.body
self.write(response.body)
以下是配置代码
app = tornado.web.Application([
(r'/test1', Test1Handler),
(r'/test2', Test2Handler)], debug=True)
app.listen(8005)
tornado.ioloop.IOLoop.instance().start()
好的,当我运行http://localhost:8005/test1
几秒钟后我可以看到hello
但是,当我运行http://localhost:8005/test2
,页面正在加载和加载。。。我应该看看向龙卷风世界问好!你好
但我永远看不到最后一个单词
我的代码怎么了?在使用decorator时,需要显式调用
finish
方法来完成http请求
def _test_callback(self, response):
self.write(response.body)
self.finish() # <------
def_测试_回调(self,response):
self.write(response.body)
self.finish()
如果给定了此装饰符,则在方法返回时响应未完成。由请求处理程序调用self.finish()来完成HTTP请求