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实现有返回结果的多线程.其中前两种方式线程执行完后都没 ...
随机推荐
- PyQt5速成教程
博客地址 https://www.jianshu.com/nb/26159952
- c# 24种设计模式4建造者模式(Builder)
先来一个例子 建造者接口 public interface IBuilder { void CreateLogo(); void CreateBody(); void CreateWheel(); v ...
- C# 出现base-64 字符数组的无效长度的解决办法
最近的一个项目,在传递参数时,在Win2003上正常,在Win7下抛出“base-64 字符数组的无效长度”这样的错误 对比了一下经过Convert.ToBase64String()转换过的参数发现, ...
- mybatis入门--初识mybatis
初识mybatis 今天,一起来说说mybits这个框架吧.这是一个持久层的框架.之前叫做ibatis.所以,在它的代码中出现ibatis这个词的时候,不要感到惊讶.不是写错了,它确实就是这个样子的. ...
- BZOJ1977或洛谷4180 [BJWC2010]次小生成树
一道LCA+生成树 BZOJ原题链接 洛谷原题链接 细节挺多,我调了半天..累炸.. 回到正题,我们先求出随便一棵最小生成树(设边权和为\(s\)),然后扫描剩下所有边,设扫到的边的两端点为\(x,y ...
- python httplib2应用get post
import httplib2,time #装饰器方法,用于记录方法消耗时间 #推荐将print 改成log def timer(func): def _warpper(self,*argv) ...
- java调用第三方包的例子
第三方包路径 D:\jp\log4j\log4j-1.2.16.jar 代码D:\jp\log4j\Log4jDemo.java import org.apache.log4j.*; public c ...
- Delegate,Action,Func,匿名方法,匿名委托,事件 (转载)
Delegate,Action,Func,匿名方法,匿名委托,事件 (转载) 一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本 ...
- Windows 平台 (UWP)应用设计
Make Your Apps Cooperate with Cross-App Communication : https://rewards.msdn.microsoft.com/Challeng ...
- [C#]“正在终止线程”的问题
在C#中启用线程后,如果试图使用Abort方法来终止线程,那么必定会抛出“正在终止线程”的异常,一开始我也想过如何来避免这种异常出现,花了不少气力,但最后发现全是徒劳. 原因是一个正在运行的线程被终止 ...