咱們都知道,如今的服務器開發對於IO調度的優先級控制權已經再也不依靠系統,都但願採用協程的方式實現高效的併發任務,如js、lua等在異步協程方面都作的很強大。python
Python在3.4版本也加入了協程的概念,並在3.5肯定了基本完善的語法和實現方式。同時3.6也對其進行了如解除了await和yield在同一個函數體限制等相關的優化。程序員
event_loop 事件循環:程序開啓一個無限的循環,程序員會把一些函數註冊到事件循環上。當知足事件發生的時候,調用相應的協程函數。
coroutine 協程:協程對象,指一個使用async關鍵字定義的函數,它的調用不會當即執行函數,而是會返回一個協程對象。協程對象須要註冊到事件循環,由事件循環調用。
task 任務:一個協程對象就是一個原生能夠掛起的函數,任務則是對協程進一步封裝,其中包含任務的各類狀態。
future: 表明未來執行或沒有執行的任務的結果。它和task上沒有本質的區別
async/await 關鍵字:python3.5 用於定義協程的關鍵字,async定義一個協程,await用於掛起阻塞的異步調用接口。服務器
【一】建立協程網絡
首先定義一個協程,在def前加入async聲明,就能夠定義一個協程函數。多線程
一個協程函數不能直接調用運行,只能把協程加入到事件循環loop中。asyncio.get_event_loop方法能夠建立一個事件循環,而後使用run_until_complete將協程註冊到事件循環,並啓動事件循環。併發
例如:app
- import asyncio
- async def fun():
- print('hello word')
- loop = asyncio.get_event_loop()
- loop.run_until_complete(fun())
【二】任務對象task異步
協程對象不能直接運行,在註冊事件循環的時候,實際上是run_until_complete方法將協程包裝成爲了一個任務(task)對象。所謂task對象是Future類的子類。保存了協程運行後的狀態,用於將來獲取協程的結果。async
例如:函數
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- loop = asyncio.get_event_loop()
- task = loop.create_task(fun())
- print(task)
- loop.run_until_complete(task)
- print(task)
建立task後,task在加入事件循環以前是pending狀態,由於do_some_work中沒有耗時的阻塞操做,task很快就執行完畢了。後面打印的finished狀態。
asyncio.ensure_future 和 loop.create_task均可以建立一個task,run_until_complete的參數是一個futrue對象。當傳入一個協程,其內部會自動封裝成task,task是Future的子類。isinstance(task, asyncio.Future)將會輸出True。
【三】綁定回調
在task執行完畢的時候能夠獲取執行的結果,回調的最後一個參數是future對象,經過該對象能夠獲取協程返回值。若是回調須要多個參數,能夠經過偏函數導入。
例如:
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- def callback(future):
- print('Callback: ', future.result())
- loop = asyncio.get_event_loop()
- task = loop.create_task(fun())
- #print(task)
- task.add_done_callback(callback)
- loop.run_until_complete(task)
- #print(task)
也可使用ensure_future獲取返回值
例如:
- import asyncio
- async def fun():
- print('hello word')
- return 'miao'
- #def callback(future):
- #print('Callback: ', future.result())
- loop = asyncio.get_event_loop()
- #task = loop.create_task(fun())
- #task.add_done_callback(callback)
- task = asyncio.ensure_future(fun())
- loop.run_until_complete(task)
- print('the fun() return is: {}'.format(task.result()))
【四】await阻塞
使用async能夠定義協程對象,使用await能夠針對耗時的操做進行掛起,就像生成器裏的yield同樣,函數讓出控制權。協程遇到await,事件循環將會掛起該協程,執行別的協程,直到其餘的協程也掛起或者執行完畢,再進行下一個協程的執行。
耗時的操做通常是一些IO操做,例如網絡請求,文件讀取等。咱們使用asyncio.sleep函數來模擬IO操做。協程的目的也是讓這些IO操做異步化。
例如:
- #coding:utf-8
- import asyncio
- import threading
- import time
- async def hello():
- print("hello 1")
- r = await asyncio.sleep(1)
- print("hello 2")
- def main():
- loop = asyncio.get_event_loop()
- print("begin")
- loop.run_until_complete(hello())
- loop.close()
- print("end")
- if __name__ == "__main__":
- main()
【五】3.6更新
①能夠在同一個協程函數中同時使用await和yield
例如:
- import asyncio
- async def ticker(delay, to):
- for i in range(to):
- yield i
- await asyncio.sleep(delay)
- async def run():
- async for i in ticker(1, 10):
- print(i)
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(run())
- finally:
- loop.close()
順帶一提,yield 咱們能夠暫且認爲是一種中斷機制(詳情能夠參考官方文檔,這種解釋只是便於說明await)
例如:
- def a():
- print("first")
- yield
- print("second")
- yield
- print("end")
- yield
- if __name__ == "__main__":
- g1=a()
- print("next1")
- g1.__next__()
- print("next2")
- g1.__next__()
- print("next3")
- g1.__next__()
②容許在協程函數中異步推導式
例如:
- async def ticker(delay, to):
- for i in range(to):
- yield i
- await asyncio.sleep(delay)
- async def run():
- result = [i async for i in ticker(1, 10) if i%2]
- print(result)
- import asyncio
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(run())
- finally:
- loop.close()
【六】協程併發
定義tasks時能夠設置多個ensure,也能夠像多線程那樣用append方法實現
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- for i in range(4, 6):
- tasks.append(asyncio.ensure_future(do_some_work(i)))
當遇到阻塞時可使用await讓其餘協程繼續工做
例如:
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(3)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- for i in range(4, 6):
- tasks.append(asyncio.ensure_future(do_some_work(i)))
- loop = asyncio.get_event_loop()
- start = now()
- loop.run_until_complete(asyncio.wait(tasks))
- for task in tasks:
- print('Task ret: ', task.result())
- print('TIME: ', now() - start)
經過運行時間能夠看出aysncio實現了併發。asyncio.wait(tasks) 也可使用 asyncio.gather(*tasks) ,前者接受一個task列表,後者接收一堆task。
【七】協程嵌套
使用async能夠定義協程,協程用於耗時的io操做,咱們也能夠封裝更多的io操做過程,這樣就實現了嵌套的協程,即一個協程中await了另一個協程,如此鏈接起來。
例如:
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- async def main():
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- dones, pendings = await asyncio.wait(tasks)
- for task in dones:
- print('Task ret: ', task.result())
- start = now()
- loop = asyncio.get_event_loop()
- loop.run_until_complete(main())
- print('TIME: ', now() - start)
若是使用的是 asyncio.gather建立協程對象,那麼await的返回值就是協程運行的結果。
- #dones, pendings = await asyncio.wait(tasks)
- #for task in dones:
- #print('Task ret: ', task.result())
- results = await asyncio.gather(*tasks)
- for result in results:
- print('Task ret: ', result)
不在main協程函數裏處理結果,直接返回await的內容,那麼最外層的run_until_complete將會返回main協程的結果。
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- async def main():
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- return await asyncio.gather(*tasks)
- start = now()
- loop = asyncio.get_event_loop()
- results = loop.run_until_complete(main())
- for result in results:
- print('Task ret: ', result)
- print('TIME: ', now() - start)
或者返回使用asyncio.wait方式掛起協程。
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- async def main():
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- return await asyncio.wait(tasks)
- start = now()
- loop = asyncio.get_event_loop()
- done, pending = loop.run_until_complete(main())
- for task in done:
- print('Task ret: ', task.result())
- print('TIME: ', now() - start)
也可使用asyncio的as_completed方法
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- async def main():
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- for task in asyncio.as_completed(tasks):
- result = await task
- print('Task ret: {}'.format(result))
- start = now()
- loop = asyncio.get_event_loop()
- done = loop.run_until_complete(main())
- print('TIME: ', now() - start)
因而可知,協程的調用和組合十分的靈活,咱們能夠發揮想象盡情的浪
【八】協程中止
future對象有幾個狀態:
Pending
Running
Done
Cancelled
建立future的時候,task爲pending,事件循環調用執行的時候固然就是running,調用完畢天然就是done,若是須要中止事件循環,就須要先把task取消。可使用asyncio.Task獲取事件循環的task
例如:
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- start = now()
- loop = asyncio.get_event_loop()
- try:
- loop.run_until_complete(asyncio.wait(tasks))
- except KeyboardInterrupt as e:
- print(asyncio.Task.all_tasks())
- for task in asyncio.Task.all_tasks():
- print(task.cancel())
- loop.stop()
- loop.run_forever()
- finally:
- loop.close()
- print('TIME: ', now() - start)
啓動事件循環以後,立刻ctrl+c,會觸發run_until_complete的執行異常 KeyBorardInterrupt。而後經過循環asyncio.Task取消future
True表示cannel成功,loop stop以後還須要再次開啓事件循環,最後在close,否則會報錯。
循環task,逐個cancel是一種方案,但是正如上面咱們把task的列表封裝在main函數中,main函數外進行事件循環的調用。這個時候,main至關於最外出的一個task,那麼處理包裝的main函數便可。
- import asyncio
- import time
- now = lambda: time.time()
- async def do_some_work(x):
- print('Waiting: ', x)
- await asyncio.sleep(x)
- return 'Done after {}s'.format(x)
- async def main():
- coroutine1 = do_some_work(1)
- coroutine2 = do_some_work(2)
- coroutine3 = do_some_work(4)
- tasks = [
- asyncio.ensure_future(coroutine1),
- asyncio.ensure_future(coroutine2),
- asyncio.ensure_future(coroutine3)
- ]
- done, pending = await asyncio.wait(tasks)
- for task in done:
- print('Task ret: ', task.result())
- start = now()
- loop = asyncio.get_event_loop()
- task = asyncio.ensure_future(main())
- try:
- loop.run_until_complete(task)
- except KeyboardInterrupt as e:
- print(asyncio.Task.all_tasks())
- print(asyncio.gather(*asyncio.Task.all_tasks()).cancel())
- loop.stop()
- loop.run_forever()
- finally:
- loop.close()
【九】不一樣線程的事件循環
不少時候,咱們的事件循環用於註冊協程,而有的協程須要動態的添加到事件循環中。一個簡單的方式就是使用多線程。當前線程建立一個事件循環,而後在新建一個線程,在新線程中啓動事件循環。當前線程不會被block。
- import asyncio
- import time
- now = lambda: time.time()
- from threading import Thread
- def start_loop(loop):
- asyncio.set_event_loop(loop)
- loop.run_forever()
- def more_work(x):
- print('More work {}'.format(x))
- time.sleep(x)
- print('Finished more work {}'.format(x))
- start = now()
- new_loop = asyncio.new_event_loop()
- t = Thread(target=start_loop, args=(new_loop,))
- t.start()
- print('TIME: {}'.format(time.time() - start))
- new_loop.call_soon_threadsafe(more_work, 6)
- new_loop.call_soon_threadsafe(more_work, 3)
啓動上述代碼以後,當前線程不會被block,新線程中會按照順序執行call_soon_threadsafe方法註冊的more_work方法,後者由於time.sleep操做是同步阻塞的,所以運行完畢more_work須要大體6 + 3
【十】新線程協程
新線程協程的話,能夠在主線程中建立一個new_loop,而後在另外的子線程中開啓一個無限事件循環。主線程經過run_coroutine_threadsafe新註冊協程對象。這樣就能在子線程中進行事件循環的併發操做,同時主線程又不會被block。一共執行的時間大概在6s左右。
- import asyncio
- import time
- now = lambda: time.time()
- from threading import Thread
- def start_loop(loop):
- asyncio.set_event_loop(loop)
- loop.run_forever()
- async def do_some_work(x):
- print('Waiting {}'.format(x))
- await asyncio.sleep(x)
- print('Done after {}s'.format(x))
- def more_work(x):
- print('More work {}'.format(x))
- time.sleep(x)
- print('Finished more work {}'.format(x))
- start = now()
- new_loop = asyncio.new_event_loop()
- t = Thread(target=start_loop, args=(new_loop,))
- t.start()
- print('TIME: {}'.format(time.time() - start))
- asyncio.run_coroutine_threadsafe(do_some_work(6), new_loop)
- asyncio.run_coroutine_threadsafe(do_some_work(4), new_loop)