同步:指兩個或兩個以上隨時間變化的量在變化過程當中保持必定的相對關係 現象:有一個共同的時鐘,按來的順序一個一個處理web
異步:雙方不須要共同的時鐘,也就是接收方不知道發送方何時發送,因此在發送的信息中就要有提示接收方開始接收的信息,如開始位,同時在結束時有中止位 現象:沒有共同的時鐘,不考慮順序來了就處理 瀏覽器
四種異步:websocket
import tornado.ioloop import tornado.web from data.table_1 import User from tornado.web import authenticated from pycket.session import SessionMixin import tornado.websocket from datetime import datetime import time import tornado.options import tornado.httpserver from tornado.options import define, options define('port',default=8000, help='run port', type=int) define('version', default=0.1, help='version', type=str) class BaseHandler(tornado.web.RequestHandler, SessionMixin): def get_current_user(self): # current_user = self.get_secure_cookie('ID') current_user = self.session.get('ID') if current_user: return current_user return None class AbcHandler(BaseHandler): def get(self): self.write('abc') import tornado.httpclient class SyncHandler(BaseHandler): def get(self): client = tornado.httpclient.HTTPClient() # 同步HTTPClient response = client.fetch('http://127.0.0.1:8000/sync') # 8000已經啓動,去訪問sync(至關於調用接口) print(response) self.write('----SyncHandler---') # 可能發生阻塞用異步 class CallbackHandler(BaseHandler): """ 1.經過回調函數實現異步 """ @tornado.web.asynchronous # 將請求變成長鏈接 def get(self): client = tornado.httpclient.AsyncHTTPClient() # 異步AsyncHTTPClient # 阻塞完畢後調用 callback response = client.fetch('http://127.0.0.1:8000/sync', callback=self.on_response) print(response) self.write('OK'+'<br>') def on_response(self, response): print(response) self.write('----CallbackSyncHandler---') self.finish() # 回調結束,請求結束,響應到瀏覽器(不然瀏覽器一直等待狀態) import tornado.gen class GenHandler(BaseHandler): """ 2.經過協程實現異步 yield """ @tornado.web.asynchronous @tornado.gen.coroutine def get(self): client = tornado.httpclient.AsyncHTTPClient() # 異步 # 節省內存(暫停) response = yield tornado.gen.Task(client.fetch,'http://127.0.0.1:8000/sync') print(response) self.write('---gen----') class FuncHandler(BaseHandler): """ 3.經過協程實現異步 yield 調用函數 @tornado.gen.coroutine裝飾函數(函數須要用到yield)""" @tornado.web.asynchronous @tornado.gen.coroutine def get(self): response = yield self.fun() print(response) self.write('---gen----') @tornado.gen.coroutine def fun(self): client = tornado.httpclient.AsyncHTTPClient() # 異步 response = yield tornado.gen.Task(client.fetch, 'http://127.0.0.1:8000/sync') raise tornado.gen.Return(response) from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor # (它是由thread模塊封裝的(建立線程的模塊)) import requests class ExeHandler(BaseHandler): """ 4.經過協程實現異步 yield 調用函數 @run_on_executor裝飾函數(函數不用yield) 須要下載requests 和futures""" executor = ThreadPoolExecutor() # 當發生阻塞時,可以建立一個新的線程來執行阻塞的任務(多線程) @tornado.web.asynchronous @tornado.gen.coroutine def get(self): response = yield self.fun() print(response) self.write('---exe----') @run_on_executor def fun(self): response = requests.get( 'http://127.0.0.1:8000/sync') return response application = tornado.web.Application( handlers=[ (r"/sync", SyncHandler), (r"/abc", AbcHandler), (r"/callback", CallbackHandler), (r"/gen", GenHandler), (r"/func", FuncHandler), (r"/exe", ExeHandler), ], cookie_secret='haha', debug=True ) if __name__ == '__main__': tornado.options.parse_command_line() # 獲取命令行的參數 --port=1040 就能使用這個參數 print(options.port) print(options.version) http_server = tornado.httpserver.HTTPServer(application) application.listen(options.port) tornado.ioloop.IOLoop.instance().start()