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.