python threading Future源码解析
1. Future内部还是用了condition这个锁
2. Cancel
# future在执行时,会一直更新这个状态
def cancel(self):
"""Cancel the future if possible. Returns True if the future was cancelled, False otherwise. A future
cannot be cancelled if it is running or has already completed.
"""
with self._condition:
if self._state in [RUNNING, FINISHED]:
return False if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
return True self._state = CANCELLED
self._condition.notify_all() self._invoke_callbacks()
return True
2. result
def result(self, timeout=None):
"""Return the result of the call that the future represents. Args:
timeout: The number of seconds to wait for the result if the future
isn't done. If None, then there is no limit on the wait time. Returns:
The result of the call that the future represents. Raises:
CancelledError: If the future was cancelled.
TimeoutError: If the future didn't finish executing before the given
timeout.
Exception: If the call raised then that exception will be raised.
"""
with self._condition:
if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
raise CancelledError()
elif self._state == FINISHED:
return self.__get_result() self._condition.wait(timeout) if self._state in [CANCELLED, CANCELLED_AND_NOTIFIED]:
raise CancelledError()
elif self._state == FINISHED:
return self.__get_result()
else:
raise TimeoutError()
3. set_result
def set_result(self, result):
"""Sets the return value of work associated with the future. Should only be used by Executor implementations and unit tests.
"""
with self._condition:
self._result = result
self._state = FINISHED
for waiter in self._waiters:
waiter.add_result(self) #通知主线程
self._condition.notify_all() # 通知其他使用了condition这个锁的,比如result
self._invoke_callbacks()
python threading Future源码解析的更多相关文章
- python Threading模块源码解析
查看源码: 这是一个线程控制的类,这个类可以被子类化(继承)在一定的条件限制下,这里有两种方式去明确活动:第一通过传入一个callable 对象也就是调用对象,一种是通过重写这个Thread类的run ...
- python threading ThreadPoolExecutor源码解析
future: 未来对象,或task的返回容器 1. 当submit后: def submit(self, fn, *args, **kwargs): with self._shutdown_lock ...
- python的基础类源码解析——collection类
1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 ################################### ...
- Python 枚举类源码解析
1. EnumMeta 元类编程,生成类的类,可以动态生成类. 用法: type(name, bases, dict) name -> 类名: str bases -> 基类: tuple ...
- 『Python』源码解析_从ctype模块理解对象
1.对象的引用计数 从c代码分析可知,python所有对象的内存有着同样的起始结构:引用计数+类型信息,实际上这些信息在python本体重也是可以透过包来一窥一二的, from ctypes impo ...
- 神经网络中 BP 算法的原理与 Python 实现源码解析
最近这段时间系统性的学习了 BP 算法后写下了这篇学习笔记,因为能力有限,若有明显错误,还请指正. 什么是梯度下降和链式求导法则 假设我们有一个函数 J(w),如下图所示. 梯度下降示意图 现在,我们 ...
- Netty 源码解析(三): Netty 的 Future 和 Promise
今天是猿灯塔“365篇原创计划”第三篇. 接下来的时间灯塔君持续更新Netty系列一共九篇 Netty 源码解析(一): 开始 Netty 源码解析(二): Netty 的 Channel 当前:Ne ...
- python线程threading.Timer源码解读
threading.Timer的作用 官方给的定义是: """Call a function after a specified number of seconds: t ...
- [源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案
[源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案 目录 [源码解析] 深度学习分布式训练框架 horovod (11) --- on s ...
随机推荐
- 一篇短文带您了解一下EasyCaching
前言 从2017年11月11号在Github创建EasyCaching这个仓库,到现在也已经将近一年半的时间了,基本都是在下班之后和假期在完善这个项目. 由于EasyCaching目前只有英文的文档托 ...
- java高并发系列 - 第3天:有关并行的两个重要定律
有关为什么要使用并行程序的问题前面已经进行了简单的探讨.总的来说,最重要的应该是处于两个目的. 第一,为了获得更好的性能: 第二,由于业务模型的需要,确实需要多个执行实体. 在这里,我将更加关注第一种 ...
- Nginx+keepalived(高可用主备模式)
Nginx+keepalived(高可用主备模式) 环境:centos6.7 准备:两台服务器(虚拟机).两台应用(Tomcat).Nginx.keepalived server1:192.168.2 ...
- ADB常用命令(adb常用命令)
基本用法 命令语法 adb 命令的基本语法如下: adb [-d|-e|-s <serialNumber>] <command> 如果只有一个设备/模拟器连接时,可以省略掉 [ ...
- mssql 单引号插入到sqlserver的方法分享
转自:http://www.maomao365.com/?p=6740 摘要: 下文讲述sqlserver操作中遇到单引号的处理方法sqlserver 数据库中可以存储任何字符,因为在数据库中字符都是 ...
- Linux system v 共享内存
system v 共享内存 #include <sys/types.h> #include <sys/shm.h> int shmget(key_t key, size_t s ...
- WindowsServer2003中IIS支持php的配置
1.安装MySQL(没有特殊说明的就按照默认安装)选择 Custom 自定义安装点击"Change"更改 MySQL 安装目录(自定义)其他按照默认的下一步就可以 安装完成后会自动 ...
- Git入门基础教程和SourceTree应用
目录 一.Git的安装 1.1 图形化界面 1.2 命令行界面 二.本地仓库的创建与提交 2.1 图形化界面 2.1.1 首先在电脑上有一个空白目录 2.1.2 打开SourceTree 2.1.3 ...
- Jmeter+ant+Jenkins构建接口自动化测试时构建失败 提示:Fatal Error! 字符引用 "&#原因
Jmeter+ant+Jenkins构建接口自动化测试时构建失败 提示:Fatal Error! 字符引用 "&#原因:接口响应数据中有&#
- 05webpack-webpack-dev-server时时跟新-第2种方式
<!--14 第一种方式 自动打开浏览器 端口号 指定托管的跟目录 启动热刷新 这种是在webpack.json中去配置的 直接在package中 写 将“script”:{ "dev ...