asyncio模块中的Future和Task
task是可以理解为单个coroutine,经过ensure_future方法处理而形成,而众多task所组成的集合经过asyncio.gather处理而形成一个future。
再不精确的粗略的说,future就是存放着众多task或future的容器。
而task又是future的子类,所以不管是task还是future还是coreture都可以看成是一个广义的携程,future无非是一个内部包含众多携程的大携程而已,await后面,task,coroture,future都可以接。
ensure_future 可以将 coroutine 封装成 Task。
asyncio.ensure_future(coro_or_future, *, loop=None)
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
----------------------------------------分割线----------------------------------------
asyncio.gather 将一些 Future 和 coroutine 封装成一个 Future。
asyncio.wait方法则返回一个 coroutine。
run_until_complete 既可以接收 Future 对象,也可以是 coroutine 对象,如果是coroutine,则先把他转化为future
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.
asyncio模块中的Future和Task的更多相关文章
- Python asyncio 模块
Python 3.4 asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接获取一个EventLo ...
- python:Asyncio模块处理“事件循环”中的异步进程和并发执行任务
python模块Asynico提供了管理事件.携程.任务和线程的功能已经编写并发代码的同步原语. 组成模块: 事件循,Asyncio 每个进程都有一个事件循环. 协程,子例程概念的泛化,可以暂停任务, ...
- 读asyncio模块源码时的知识补漏
硬着头皮看了一周的asyncio模块代码,了解了大概的执行流程,引用太多,成尤其是对象间函数的引用. 光是这么一段简单的代码: # coding: utf8 import asyncio import ...
- Python:asyncio模块学习
python asyncio 网络模型有很多中,为了实现高并发也有很多方案,多线程,多进程.无论多线程和多进程,IO的调度更多取决于系统,而协程的方式,调度来自用户,用户可以在函数中yield一个状态 ...
- asyncio模块
asyncio模块 这是官网也非常推荐的一个实现高并发的一个模块,python也是在python 3.4中引入了协程的概念. asyncio 是干什么的? 异步网络操作 并发 协程 python3 ...
- Python之路(第四十七篇) 协程:greenlet模块\gevent模块\asyncio模块
一.协程介绍 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的. 协程相比于线程,最大的区别在于 ...
- asyncio模块实现单线程-多任务的异步协程
本篇介绍基于asyncio模块,实现单线程-多任务的异步协程 基本概念 协程函数 协程函数: 定义形式为 async def 的函数; aysnc 在Python3.5+版本新增了aysnc和awai ...
- Python之asyncio模块的使用
asyncio模块作用:构建协程并发应用的工具 python并发的三大内置模块,简单认识: .multiprocessing:多进程并发处理 .threading模块:多线程并发处理 .asyncio ...
- python——asyncio模块实现协程、异步编程
我们都知道,现在的服务器开发对于IO调度的优先级控制权已经不再依靠系统,都希望采用协程的方式实现高效的并发任务,如js.lua等在异步协程方面都做的很强大. Python在3.4版本也加入了协程的概念 ...
随机推荐
- 对于Arrays的deep相关的方法。
关于: deepEquals Arrays.equals(Object[] o1, Object[] o2):当是判断数组是引用类型数组的时候,从以下条件判断: 1.o1与o2指向同一个数组实例时,返 ...
- Luogu P2463 [SDOI2008]Sandy的卡片
题目链接 \(Click\) \(Here\) 真的好麻烦啊..事实证明,理解是理解,一定要认认真真把板子打牢,不然调锅的时候真的会很痛苦..(最好是八分钟能无脑把\(SA\)码对的程度\(QAQ\) ...
- Thymeleaf中的&&解析问题
1.问题: 最近使用了新的html模板thymeleaf..在模板里使用js语法时遇到问题,&&不能正确的被解析,使用if(a&&b){}可以通过模板解析,但是浏览器上 ...
- SNMP支持IPv6
SNMP Trap 网上好像很少提到ipv6的配置文件格式,这个配置文件的格式应该为: trap2sink udp6:[fec0::]:: 创建监听的函数入口:netsnmp_udp_transp ...
- Hadoop记录-Ganglia监控HDFS和HBase指标说明
HDFS相关 datanode相关 参数 解释 dfs.datanode.blockChecksumOp_avg_time 块校验平均时间 dfs.datanode.blockChecksumOp_n ...
- Shuttle 学习
见 http://blog.csdn.net/liu765023051/article/details/38521039
- log4j 基础教程【转】
参考引用自: http://javacrazyer.iteye.com/blog/1135493 我的git地址: https://git.oschina.net/KingBoBo/Log4JDemo ...
- Java实现OPC通信
1.PLC和OPC 使用的PLC:西门子的S7 300,具体型号如下图 使用的OPC server软件: 模拟仿真用的 MatrikonOPCSimulation(50M),百度网盘,密码: mcur ...
- HDU 1263(水果统计 **)
题意是对水果的产地和种类进行统计再按格式输出. 代码如下: #include <bits/stdc++.h> using namespace std; struct node { ],pl ...
- 【概括】C++11/14特性
c++11 c++14