上一篇文章: Python:Tornado 第二章:實戰演練:開發Tornado網站:第五節:RequestHandler:輸出相應函數
下一篇文章: Python:Tornado 第二章:實戰演練:開發Tornado網站:第七節:安全Cookie機制
Tornado有兩種方式可改變同步的處理流程:web
該方法已通過期,再也不贅述,直接使用@tornado.gen.coroutine代替。segmentfault
Tornado協程結合了同步處理和異步處理的有點,使得代碼即清晰易懂,又可以適應海量客戶端的高併發請求。安全
代碼:併發
import tornado.web import tornado.httpclient from tornado.web import Application import tornado.ioloop class MainHandler(tornado.web.RequestHandler): @tornado.gen.coroutine def get(self): http=tornado.httpclient.AsyncHTTPClient() response=yield http.fetch("http://www.baidu.com") self.write(response.body) if __name__ == '__main__': app=Application([ ("/",MainHandler) ]) app.listen("8888") tornado.ioloop.IOLoop.current().start()
協程化的關鍵技術點以下:app