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移植之make uImage编译过程分析
编译出uboot可以运行的linux内核代码的命令是make uImage,下面详细介绍下生成linux-2.6.22.6/arch/arm/boot/uImage的过程: 1.vmlinux.Ima ...
- Jmeter常用脚本开发之Junit Request
说明:Junit Request就是把Junit测试框架的自动化用例在jmeter上执行 步骤: 1.创建Java工程,编写Junit自动化测试用例 2.然后把用例打成jar包,复制到Jmter的li ...
- string+和stringbuffer的速度比较
public class Main{ public static void main(String[] args){ /* 1 */ String string = "a" + & ...
- 01. pt-align
01. pt-align pt-align xxx.txt =========================================== pt-align对齐输出格式 name city a ...
- 异步Servlet和异步过虑器
异步处理功能可以节约容器线程.此功能的作用是释放正在等待完成的线程,是该线程能够被另一请求所使用. 要编写支持异步处理的 Servlet 或者过虑器,需要设置 asyncSupported 属性为 t ...
- How to convert a PDF file to JPEGs using PHP
Hey, Today I would like to show you how we can convert PDF to JPEG using imagick extension. Imagick ...
- read temperature
button1, button2, richtexbox1, serialport1, using System;using System.Collections.Generic;using Syst ...
- NETSHARP微信开发说明
一.微信开发介绍 1.微信分为个人号,订阅号.服务号,需要去理解三个号的区别,对于开发来说也需要了解不同的账号所提供的功能 2.微信号需要审批,审批之后有一些功能才能使用 3.微信提供的功能及使用情况 ...
- zookeeper相关
1.zookeeper应用:集群节点间的数据同步(资源管理),分布式锁(主要是利用客户端在一个会话中在zookeeper中创建一个znode节点,然后再去执行自己的业务代码,比如去更新数据库,其他客户 ...
- spring学习十九 常用注解
1. @Component 创建类对象,相当于配置<bean/>2. @Service 与@Component 功能相同. 2.1 写在 ServiceImpl 类上.3. @Reposi ...