参考: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*iterablestimeout=Nonechunksize=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=Nonethread_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的并行执行的更多相关文章

  1. python的multiprocessing模块进程创建、资源回收-Process,Pool

    python的multiprocessing有两种创建进程的方式,每种创建方式和进程资源的回收都不太相同,下面分别针对Process,Pool及系统自带的fork三种进程分析. 1.方式一:fork( ...

  2. a simple erlang process pool analysis

    a simple erlang process pool analysis 这是一个简单的erlang进程池分析,是learn you some erlang for Great Good 里面的一个 ...

  3. A daemon process class in python

    In everbright task schedule project, we need some daemon process to do certain work, here is a examp ...

  4. Process Autocad by python

    一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...

  5. Python 浅析线程(threading模块)和进程(process)

    线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 进程与线程 什么 ...

  6. python 全栈开发,Day40(进程间通信(队列和管道),进程间的数据共享Manager,进程池Pool)

    昨日内容回顾 进程 multiprocess Process —— 进程 在python中创建一个进程的模块 start daemon 守护进程 join 等待子进程执行结束 锁 Lock acqui ...

  7. python学习笔记——multiprocessing 多进程组件 进程池Pool

    1 进程池Pool基本概述 在使用Python进行系统管理时,特别是同时操作多个文件目录或者远程控制多台主机,并行操作可以节约大量时间,如果操作的对象数目不大时,还可以直接适用Process类动态生成 ...

  8. python学习笔记——multiprocessing 多进程模块Process

    系统自带的fork模块创建的多进程是基于Linux或Unix平台的,而window平台并不支持: python中的multiprocess为跨平台版本的多进程模块,支持子进程.通信和共享数据.执行不同 ...

  9. 60%的人不懂Python进程Process,你懂吗?

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:蒋狗    新手注意:如果你Python基础学的不够扎实,遇问题没人解答 ...

随机推荐

  1. USER 版本与ENG 版本差异

    [Description] Android USER 版本与ENG 版本的差异   [Keyword] USER ENG user eng 用户版本 工程版本 差异   [Solution] Goog ...

  2. javascript 处理鼠标右键事件

    使用右键事件 在需要右键的地方加上  onmousedown="if(event.button == 2) alert('点击右键了!');即可   不经意地被一位同事问起在javascri ...

  3. PLsql登录数据库提示密码即将过期-

    小哥询问,PL*SQL用户登录后弹出警告:咋整? ORA-28002:the password will expire within 7 days密码在7天内将到期 do you wish to ch ...

  4. 轻松玩转AI 与PDF文件的转化(完美解决字体问题)

    经过漫长而坚苦卓绝的研究查阅了网上无数资料下载了众多相关软件进行试验终于,找到搞定PDF文件的方便并且有效的办法PDF文件!你这个魔鬼!退去吧!!!! 难点一: 如何修改客户常常会提供不知道从哪里搞来 ...

  5. CF1093:E. Intersection of Permutations(树状数组套主席树)

    题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[L2,R2]区间出现了多少个相同的数字. ...

  6. 20155201 2016-2017-2 《Java程序设计》第七周学习总结

    20155201 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 Lambda Lamdba表达式 例如 Comparator<String& ...

  7. CTF-练习平台-Social之 密码?

    一.密码? 看到题目提示是“张三”“生日”,再联系到我们设置密码时一般是名字的拼音首字母加生日,所以猜测是:zs19970315尝试后正确.

  8. JQ和JS获取span标签的内容(有的情况下JQ达不到预期的目的就用JS)

    https://www.cnblogs.com/anniey/p/6439021.html <span id="content">‘我是span标签的内容’</s ...

  9. org.apache.commons.lang3.Validate

    Validates.notBlank(user.getName(), "必须提供name");

  10. Vue中的“混合”——mixins使用方法

    混合是一种灵活的分布式复用 Vue 组件的方式.混合对象可以包含任意组件选项.以组件使用混合对象时,所有混合对象的选项将被混入该组件本身的选项.当组件和混合对象含有同名选项时,这些选项将以恰当的方式混 ...