簡體   English   中英

我的python龍卷風代碼不起作用...為什么

[英]My python tornado code doesn't work… why

我有兩個Handler類來比較@ tornado.web.asynchronous裝飾器

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()

好的,當我run http://localhost:8005/test1 ,幾秒鍾后我可以看到你好...

但是,當我run http://localhost:8005/test2 ,頁面正在加載和加載...我應該看到Hello to the Tornado world! Hello Hello to the Tornado world! Hello但我再也看不到Hello好話

我的代碼有什么問題?

使用tornado.web.asynchronous裝飾器時,需要顯式調用finish方法來完成http請求。

def _test_callback(self, response): 
    self.write(response.body)  
    self.finish()  # <------

您似乎在裝飾為異步的處理程序中缺少self.finish()

如果指定了此裝飾器,則方法返回時響應不會結束。 由請求處理程序調用self.finish()完成HTTP請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM