concurrent.future
concurrent.future module provides a high-level interface for asynchronously executing callables.
Basic Format:
from concurrent.future import ThreadPoolExecuor,ProcessPoolExecutor executor = ThreadPoolExecutor(max_workers=2) #max_workers 为池中允许的线程数 executor = ProcessPoolExecutor(max_workers=2) obj1 = executor.submit(function, arg1, arg2) #提交子线程
obj2 = executor.map(func,iterator) #异步运行的map executor.shutdown(wait=True) # 类似于pool.close() pool.join() 等待所有子进程/线程运行完结果 当wait = False的时候结果会立刻返回,但pool依然会等到所有结果都得到后才会释放 obj1.result()
obj2.result() #拿到结果 # ThreadPoolExecuor,ProcessPoolExecutor都可以采用with statement
with ThreadPoolExecutor(max_workers=1) as executor:
obj1 = executor.submit(function, arg1, arg2) #提交子线程
obj2 = executor.map(func,iterator) #异步运行的map
Others:
obj.exception
(timeout=second)
在second内如果没有结果就报错。 如果timeout=None 就相当于obj.join()无限制等待obj结束
- obj.add_done_callback(func)
将obj返回给func函数,由于返回的obj,所以func函数内需要obj.result()
- obj.cancel()
如果被执行了,就返回false,如果在执行可以被cancel 就返回true
- obj.cancelled
如果被cancel成功了就返回true
- done()
没有在执行就返回true(被cancel或者运行完毕)
exmaples:
def wait_on_future():
f = executor.submit(pow, 5, 2)
# This will never complete because there is only one worker thread and
# it is executing this function.
print(f.result()) executor = ThreadPoolExecutor(max_workers=1)
executor.submit(wait_on_future)
reference: http://pythonhosted.org/futures/
concurrent.future的更多相关文章
- Java中设置方法执行的超时时间java.util.concurrent.Future
java.util.concurrent.Future Future代表一个异步计算的结果.它提供了方法来检查是否计算已经完成,还是正在计算而处于等待状态,并且也提供了获取计算结果 方法.当计算完成后 ...
- java.util.concurrent.Future Basics
Hereby I am starting a series of articles about future concept in programming languages (also known ...
- Python的并发并行[4] -> 并发[1] -> concurrent.future 模块
concurrent.future 模块 1 thread模块 / thread Module 1.1 常量 / Constants Pass 1.2 函数 / Function Pass 1.3 类 ...
- 高效编程之 concurrent.future
背景 我们知道 Python 中有多线程threading 和多进程multiprocessing 实现并发, 但是这两个东西开销很大,一是开启线程/进程的开销,二是主程序和子程序之间的通信需要 序列 ...
- 线程笔记:Future模式
线程技术可以让我们的程序同时做多件事情,线程的工作模式有很多,常见的一种模式就是处理网站的并发,今天我来说说线程另一种很常见的模式,这个模式和前端里的ajax类似:浏览器一个主线程执行javascri ...
- java多线程系类:JUC线程池:06之Callable和Future(转)
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- Future和Promise
Future用于获取异步操作的结果,而Promise则比较抽象,无法直接猜测出其功能. Future Future最早来源于JDK的java.util.concurrent.Future,它用于代表异 ...
- Java多线程系列--“JUC线程池”06之 Callable和Future
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- 【原创】JAVA并发编程——Callable和Future源码初探
JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
随机推荐
- oracle 单表导出导入
exp username/password@服务名 file=d:\daochu.dmp tables=(tableneme,...)
- 性能(js)
1.避免全局查找: <script type="text/javascript"> function updateUI(){ var imgs=document.get ...
- MyBatis Generator介绍
MyBatis Generator介绍 MyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和 ...
- 07. pt-fifo-split
iostat -dxm 1 42 1>iostat.log 2>&1 ---------------------------------------- #!/bin/bash of ...
- 利用gulp搭建less编译环境
什么是less? 一种 动态 样式 语言. LESS 将 CSS 赋予了动态语言的特性,如 变量, 继承, 运算, 函数. LESS 既可以在 客户端 上运行 (支持IE 6+, Webkit, ...
- xml配置sql语句
- ApplicationContext(九)初始化非延迟的 bean
ApplicationContext(九)初始化非延迟的 bean 此至,ApplicationContext 已经完成了全部的准备工作,开始初始化剩余的 bean 了(第 11 步). public ...
- js生成简单二维码
js文件下载地址:https://download.csdn.net/download/weixin_38296752/10554485 一.引入qrcode.js文件 <script type ...
- openssl AES加密
此代码不涉及ECB和CBC等关联加密 #include <stdio.h> #include <string.h> #include <stdlib.h> #inc ...
- POI导出大量数据的简单解决方案
说明:我的电脑 2.0CPU 2G内存 能够十秒钟导出 20W 条数据 ,12.8M的excel内容压缩后2.68M 我们知道在POI导出Excel时,数据量大了,很容易导致内存溢出.由于Excel ...