task是能夠理解爲單個coroutine,通過ensure_future方法處理而造成,而衆多task所組成的集合通過asyncio.gather處理而造成一個future。app
再不精確的粗略的說,future就是存放着衆多task或future的容器。async
而task又是future的子類,因此無論是task仍是future仍是coreture均可以當作是一個廣義的攜程,future無非是一個內部包含衆多攜程的大攜程而已,await後面,task,coroture,future均可以接。oop
ensure_future 能夠將 coroutine 封裝成 Task。spa
asyncio.ensure_future(coro_or_future, *, loop=None)code
Schedule the execution of a coroutine object: wrap it in a future. Return a Task object.If the argument is a Future, it is returned directly.對象
import asyncio async def hello(name): await asyncio.sleep(2) print('Hello, ', name) coroutine = hello("World") a = asyncio.ensure_future(coroutine)# print (a.__class__)#Task b=asyncio.Future()#標準future print (b.__class__)#Future print (issubclass(a.__class__,b.__class__))#true,Task類是Future類的子類 #首先a是一個Task,又由於Task類是Futrue類的子類,因此,咱們也能夠說,a是一個Future #下面驗證If the argument is a Future, it is returned directly. c=asyncio.ensure_future(b)# print (c is b)#true d=asyncio.ensure_future(a)# print (d is a)#True
----------------------------------------分割線----------------------------------------blog
asyncio.gather 將一些 Future 和 coroutine 封裝成一個 Future。ci
asyncio.wait方法則返回一個 coroutine。it
run_until_complete 既能夠接收 Future 對象,也能夠是 coroutine 對象,若是是coroutine,則先把他轉化爲futureio
BaseEventLoop.run_until_complete(future)
Run until the Future is done.
If the argument is a coroutine object, it is wrapped by ensure_future().
Return the Future's result, or raise its exception.