Process Pool实现Python的并行执行
参考:Python3.6.2文档
Source code: Lib/concurrent/futures/thread.py and Lib/concurrent/futures/process.py
Executor对象
class concurrent.futures.Executor
方法:
submit(fn, *args, **kwargs) #函数fn会按fn(*args **kwargs)执行,返回值是一个Future Object
map(func, *iterables, timeout=None, chunksize=1) #executor.map() 函数调用时需要输入辅助函数和待处理的数据列表。
#这个函数帮我们完成所有麻烦的工作,把列表分成几个小列表,把小列表分配给每个子进程,运行子进程,以及汇总结果。
map(func, *iterables)
submit(fn, *args, **kwargs)
shutdown(wait=True)
示例:
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(pow, 323, 1235)
print(future.result())
ThreadPoolExecutor对象
Executor子类
class concurrent.futures.ThreadPoolExecutor(max_workers=None, thread_name_prefix=”)
import time
def wait_on_b():
time.sleep(5)
print(b.result()) # b will never complete because it is waiting on a.
return 5 def wait_on_a():
time.sleep(5)
print(a.result()) # a will never complete because it is waiting on b.
return 6 executor = ThreadPoolExecutor(max_workers=2)
a = executor.submit(wait_on_b)
b = executor.submit(wait_on_a)
ProcessPoolExecutor对象
Executor子类
class concurrent.futures.ProcessPoolExecutor(max_workers=None)
import concurrent.futures
import math PRIMES = [
112272535095293,
112582705942171,
112272535095293,
115280095190773,
115797848077099,
1099726899285419] def is_prime(n):
if n % 2 == 0:
return False sqrt_n = int(math.floor(math.sqrt(n)))
for i in range(3, sqrt_n + 1, 2):
if n % i == 0:
return False
return True def main():
with concurrent.futures.ProcessPoolExecutor() as executor:
for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
print('%d is prime: %s' % (number, prime)) if __name__ == '__main__':
main()
不并行的话,代码应该是
for number in PRIMES:
pirme = is_prime(number);
ProcessPoolExecutor.map(...)常用于for循环中
Process Pool实现Python的并行执行的更多相关文章
- python的multiprocessing模块进程创建、资源回收-Process,Pool
python的multiprocessing有两种创建进程的方式,每种创建方式和进程资源的回收都不太相同,下面分别针对Process,Pool及系统自带的fork三种进程分析. 1.方式一:fork( ...
- a simple erlang process pool analysis
a simple erlang process pool analysis 这是一个简单的erlang进程池分析,是learn you some erlang for Great Good 里面的一个 ...
- A daemon process class in python
In everbright task schedule project, we need some daemon process to do certain work, here is a examp ...
- Process Autocad by python
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...
- Python 浅析线程(threading模块)和进程(process)
线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 进程与线程 什么 ...
- python 全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)
昨日内容回顾 进程 multiprocess Process —— 进程 在python中创建一个进程的模块 start daemon 守护进程 join 等待子进程执行结束 锁 Lock acqui ...
- python学习笔记——multiprocessing 多进程组件 进程池Pool
1 进程池Pool基本概述 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量时间,如果操作的对象数目不大时,还可以直接适用Process类动态生成 ...
- python学习笔记——multiprocessing 多进程模块Process
系统自带的fork模块创建的多进程是基于Linux或Unix平台的,而window平台并不支持: python中的multiprocess为跨平台版本的多进程模块,支持子进程.通信和共享数据.执行不同 ...
- 60%的人不懂Python进程Process,你懂吗?
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:蒋狗 新手注意:如果你Python基础学的不够扎实,遇问题没人解答 ...
随机推荐
- 如何在win10(64位系统)上安装apache服务器
今天装了Apache服务器,下面是我总结的方法: 一,准备软件 1.64位的apache版本 传送门:http://www.apachelounge.com/download/ 2.VC11运行库 下 ...
- 常用增强学习实验环境 I (MuJoCo, OpenAI Gym, rllab, DeepMind Lab, TORCS, PySC2) (转载)
原文地址:http://blog.csdn.net/jinzhuojun/article/details/77144590 和其它的机器学习方向一样,强化学习(Reinforcement Learni ...
- ipAllocate_and_linkState_hacking
#!/bin/bash # Author: Joshua Chen # Date: Jun # Location: Shenzhen #. 解读这两个程序是因为程序中包含了大部分shell脚本的基本语 ...
- 教你正确进入DFU及恢复模式 无敌重刷模式
苹果分为两种恢复模式,一种叫做DFU模式,另一种是恢复模式.DFU的全称是Development FirmwareUpgrade,实际意思就是iPhone固件的强制升降级模式.而恢复模式则是屏幕上会显 ...
- BZOJ4836: [Lydsy1704月赛]二元运算【分治FFT】【卡常(没卡过)】
Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使 ...
- 微信小程序实现多选分享
微信小程序拉取好友列表后,默认只能选一个分享,现在在分享回调onShareAppMessage里加上这段代码,拉取好友列表时,右上角会出现多选按钮,这样就解决了微信小程序安卓下只能单选分享的问题. / ...
- JavaScript 之call , apply 和prototype 介绍
1. 前言 为什么将这三个概念放在一起说.原因是这些是会在实现js 继承会需要使用到的 2. call 和 apply call 和 apply 的作用基本类似, 都是去执行function并将这个f ...
- Sql语句导出数据库表结构及查询表视图储存过程名
--一句Sql把表结构全部查询出来 SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colo ...
- Excel 从字符串中提取日期值
因为工作需要,Excel 表中有一串字符,需要将字符里的日期提取出,并转成日期值. 需要转成如下格式: 可使用以下公式. =DATEVALUE(TEXT(MID(I2,1,4)+1&" ...
- [DL] 基于theano.tensor.dot的逻辑回归代码中的SGD部分的疑问探幽
在Hinton的教程中, 使用Python的theano库搭建的CNN是其中重要一环, 而其中的所谓的SGD - stochastic gradient descend算法又是如何实现的呢? 看下面源 ...