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实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
随机推荐
- 【Linux系列】Ubuntu ping通,xshell无法连接
现象描述:Ubuntu能Ping通主机,主机也能ping通虚拟机.而且,虚拟机也能上网.只是xshell不能连接. 解决方案: 一:使用管理员身份 设置防火墙. 先查看一下防火墙状态 sudo ufw ...
- CBV FBV rest framework
CBV与FBV restful协议 ---- 一切皆是资源,操作只是请求方式 ----book表增删改查 /books/ books /books/add/ addbook /books/(\d+)/ ...
- PAT 1087 有多少不同的值(20)(STL—set)
1087 有多少不同的值(20 分) 当自然数 n 依次取 1.2.3.--.N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数 ...
- A面&B面
难难难.道是玄,不遇知音不可谈.遇了知音聊两句,免教那枉费舌尖.难得今天心情不错,反思毕业这五年的种种,有浑噩.迷茫.彷徨.莽撞.执着.困顿.不惧,走到今天迈过了几道坎早已忘却,同时也还在询问自己值不 ...
- No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal
No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...
- win7 64位远程连接oracle11g64位
1.首先下载即时客户端 instantclient-basic-windows.x64-11.2.0.4.0,下载地址:http://www.oracle.com/technetwork/topics ...
- IntelliJ IDEA 配置svn
1.启用版本控制 2.在弹出的框中选择svn 3.下载并解压VisualSVN 下载地址:http://subversion.apache.org/packages.html#windows 4.i ...
- Mac OS X Git安装教程
http://code.google.com/p/git-osx-installer上也提供了一个Git的图形化客户端:OpenInGitGui,可以从这里获得,OpenInGitGui十分小巧,下载 ...
- 关于Web应用开发流程的总结
以下内容为个人工作总结,如果不当,谢谢指出错误. 假设最简单的情况,一个开发人员,开发所有的代码,一个测试人员.一个测试的服务器,一个生产的服务器. 开发人员需要为公司开发一个项目,开发人员首先分析产 ...
- import this
import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than ...