tornado異步編程

  tornado相較於flask入門難,精通更難,用兩種框架都寫一個最簡單的帶有time.sleep的入門dome就會發現區別,同時進來多個請求flask就不會阻塞(雖然也是單進程但flask會使用selet複用或者多線程方式避免給請求阻塞的感受),而torndao就會發生阻塞(單進程單線程,一個一個來處理),這樣的問題在入門tornado時是須要搞清楚的,通常就是經過 @tornado.web.asynchronous,@tornado.gen.coroutine來解決。php

# -*- coding:utf-8 -*-
#!/bin/env python import os import time import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import tornado.gen import tornado.httpclient import tornado.concurrent import tornado.ioloop from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor from tornado.options import define, options import sys reload(sys) sys.setdefaultencoding('utf8') """ https://blog.csdn.net/chenyulancn/article/details/45888949 參考並拓展 https://blog.csdn.net/hjhmpl123/article/details/53673108 curl http://localhost:8000/sleep curl http://localhost:8000/justnow 有2個url,一個是耗時的請求,一個是能夠或者說須要馬上返回的請求,但願訪問耗時的請求不會影響也不會被其餘人的請求 專業點講就是:如何在tornado中以異步的方式調用同步函數? """ define("port", default=8000, help="run on the given port", type=int) class My(object): def __init__(self): self.executor = ThreadPoolExecutor(2) #不能夠設爲1或0 @run_on_executor def f(self): print(os.path.join(os.path.dirname(__file__), 'python')) time.sleep(2) print(10) return 1,2,3 @run_on_executor def f1(self): time.sleep(1) print(15) return 4,5,6 #不要 yield 4,5,6 @run_on_executor def f2(self): time.sleep(1.5) print('hello, world1') f11=self.f1() f12=self.f() a,b,c= f11.result() d,e,f= f12.result() # a,b,c= yield self.f1() # 這樣是不能夠的 # d,e,f= yield self.f() print "a,b,c:",a,b,c print "d,e,f:",d,e,f print('hello, world2') return 'success',a+b+c+d+e+f #不要 yield 'success',a+b+c+d+e+f class SleepHandler(tornado.web.RequestHandler): @tornado.web.asynchronous @tornado.gen.coroutine def get(self): #yield tornado.gen.Task(tornado.ioloop.IOLoop.instance().add_timeout, time.time() + 5) m = My() result, m = yield tornado.gen.maybe_future(m.f2()) yield self.write({"result":result, "sum":m}) class JustNowHandler(tornado.web.RequestHandler): def get(self): self.write("i hope just now see you") if __name__ == "__main__": tornado.options.parse_command_line() app = tornado.web.Application(handlers=[ (r"/sleep", SleepHandler), (r"/justnow", JustNowHandler)]) http_server = tornado.httpserver.HTTPServer(app) http_server.listen(options.port) tornado.ioloop.IOLoop.instance().start()

轉載於猿2048:➣《tornado異步編程》python

相關文章
相關標籤/搜索