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源码解析的更多相关文章

  1. python Threading模块源码解析

    查看源码: 这是一个线程控制的类,这个类可以被子类化(继承)在一定的条件限制下,这里有两种方式去明确活动:第一通过传入一个callable 对象也就是调用对象,一种是通过重写这个Thread类的run ...

  2. python threading ThreadPoolExecutor源码解析

    future: 未来对象,或task的返回容器 1. 当submit后: def submit(self, fn, *args, **kwargs): with self._shutdown_lock ...

  3. python的基础类源码解析——collection类

    1.计数器(counter) Counter是对字典类型的补充,用于追踪值的出现次数. ps:具备字典的所有功能 + 自己的功能 ################################### ...

  4. Python 枚举类源码解析

    1. EnumMeta 元类编程,生成类的类,可以动态生成类. 用法: type(name, bases, dict) name -> 类名: str bases -> 基类: tuple ...

  5. 『Python』源码解析_从ctype模块理解对象

    1.对象的引用计数 从c代码分析可知,python所有对象的内存有着同样的起始结构:引用计数+类型信息,实际上这些信息在python本体重也是可以透过包来一窥一二的, from ctypes impo ...

  6. 神经网络中 BP 算法的原理与 Python 实现源码解析

    最近这段时间系统性的学习了 BP 算法后写下了这篇学习笔记,因为能力有限,若有明显错误,还请指正. 什么是梯度下降和链式求导法则 假设我们有一个函数 J(w),如下图所示. 梯度下降示意图 现在,我们 ...

  7. Netty 源码解析(三): Netty 的 Future 和 Promise

    今天是猿灯塔“365篇原创计划”第三篇. 接下来的时间灯塔君持续更新Netty系列一共九篇 Netty 源码解析(一): 开始 Netty 源码解析(二): Netty 的 Channel 当前:Ne ...

  8. python线程threading.Timer源码解读

    threading.Timer的作用 官方给的定义是: """Call a function after a specified number of seconds: t ...

  9. [源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案

    [源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案 目录 [源码解析] 深度学习分布式训练框架 horovod (11) --- on s ...

随机推荐

  1. 100道MySQL常见面试题总结,看完直接收藏

    前言 本文主要受众为开发人员,所以不涉及到MySQL的服务部署等操作,且内容较多,大家准备好耐心和瓜子矿泉水. 前一阵系统的学习了一下MySQL,也有一些实际操作经验,偶然看到一篇和MySQL相关的面 ...

  2. linux下使用mv将递归的文件从多个目录移动到一个目录中

    find /data/download/temp \( -iname '*.mp4' -o -iname '*.avi' \) -type f -exec mv -nv -t '/data/downl ...

  3. Spring Security 教程 大牛的教程

    https://www.iteye.com/blog/elim-2247073 Spring Security 教程 Spring Security(20)——整合Cas Spring Securit ...

  4. Microsoft Office自制安装指南 —Nusen_Liu

    Microsoft Word 2010 正版下载安装步骤 版权来自:Nusen_Liu 1.   解压文件(推荐解压到当前文件夹,大神也可以自定义的)下载地址在第16步 (*^__^*) 2.   解 ...

  5. xcode 左边导航栏中,类文件后面的标记“A”,"M","?"……等符号的含义???

        "M" = Locally modified     "U" = Updated in repository   "A" = Loc ...

  6. 使用AVFoundation完成照片拍摄存储相册, 开启关闭闪光灯, 切换摄像头

    在开启这个旅程之前, 请记住, AVFoundation是一个复杂的工具. 在很多情况下, 我我们使用苹果默认的API(比如:UIImagePickerController)就足够了. 在您阅读之前, ...

  7. docker系列(三):docker容器

    1 引言 在前面博文中,我们介绍了镜像.如果说镜像犹如面向对象中的类,本节要说的容器就是由类实例化出来的对象了,有了类才可以创建容器. 先从拉取一个官方提供的ubuntu最新镜像: $ docker ...

  8. Wireshark抓包笔录--之指定IP地址筛选捕获结果

    Wireshark安装 1.去官网下载相应的安装包 地址如下:https://www.wireshark.org/ 2.安装步骤,一路next,需要同意的地方点同意 3.安装完成后打开软件,如下: W ...

  9. 12. final修饰符

    一.final修饰符概述 1. final可以修饰类.变量和方法 2. final修饰的类.变量和方法不可改变 3. 不允许为final变量重新赋值,子类不允许覆盖父类的final方法,final类不 ...

  10. Python入门基础学习(函数)

    Python基础学习笔记(三) 函数的概念: 所谓函数,就是把具有独立功能的代码块组织为一个小模块,在需要的时候调用 函数的使用包含两个步骤: 1.定义函数 --封装独立的功能 2.调用函数 --享受 ...