python做并行计算_python做并行计算可以吗
python可以做并行计算,下面是相关介绍:
一、概览
Parallel Python是一个python模块,提供在SMP(具有多个处理器或多核的系统)和集群(通过网络连接的计算机)上并行执行python代码的机制。它轻巧,易于安装和与其他python软件集成。Parallel Python是一个用纯Python编写的开源和跨平台模块。二、特性
在SMP和集群上并行执行python代码
易于理解和实现基于Job的并行化技术(易于并行转换串行应用程序)
自动检测最佳配置(默认情况下工作进程数设置为有效处理器数)
动态处理器分配(工作进程数可以在运行时更改)
具有相同功能的后续作业的低开销(实现透明高速缓存以减少开销)
动态负载平衡(作业在运行时在处理器之间分布)
容错(如果其中一个节点发生故障,任务在其他节点上重新调度)
计算资源的自动发现
计算资源的动态分配(自动发现和容错的结果)
网络连接的基于SHA的认证
跨平台可移植性和互操作性(Windows,Linux,Unix,Mac OS X)
跨架构可移植性和互操作性(x86,x86-64等)
开源
三、动机
现在,用python编写的软件应用在很多应用程序中,包括业务逻辑,数据分析和科学计算。这与市场上的SMP计算机(多处理器或多核)和集群(计算机通过网络连接)的广泛可用性一起创建了并行执行python代码的需求。
为SMP计算机编写并行应用程序的最简单和常见的方法是使用线程。虽然,如果应用程序是计算绑定使用线程或线程python模块将不允许并行运行python字节码。原因是python解释器使用GIL(全局解释器锁)进行内部记账。这个锁允许一次只执行一个python字节码指令,即使在SMP计算机上。
PP模块克服了这个限制,并提供了一种写并行python应用程序的简单方法。内部ppsmp使用进程和IPC(进程间通信)来组织并行计算。后者的所有细节和复杂性完全被照顾,应用程序只提交作业并检索其结果(写并行应用程序的最简单的方法)。
为了使事情更好,用PP编写的软件并行工作,即使在通过本地网络或Internet连接的许多计算机上。跨平台可移植性和动态负载平衡允许PP即使在异构和多平台集群上也能有效地并行计算。
四、安装
任何平台:下载模块存档并将其解压缩到本地目录。 运行安装脚本:python setup.py install
Windows:下载并执行Windows安装程序二进制文件。
五、例子import math, sys, time
import pp
def isprime(n):
"""Returns True if n is prime and False otherwise"""
if not isinstance(n, int):
raise TypeError("argument passed to is_prime is not of 'int' type")
if n < 2:
return False
if n == 2:
return True
max = int(math.ceil(math.sqrt(n)))
i = 2
while i <= max:
if n % i == 0:
return False
i += 1
return True
def sum_primes(n):
"""Calculates sum of all primes below given integer n"""
return sum([x for x in xrange(2,n) if isprime(x)])
print """Usage: python sum_primes.py [ncpus]
[ncpus] - the number of workers to run in parallel,
if omitted it will be set to the number of processors in the system
"""
# tuple of all parallel python servers to connect with
ppservers = ()
#ppservers = ("10.0.0.1",)
if len(sys.argv) > 1:
ncpus = int(sys.argv[1])
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers)
else:
# Creates jobserver with automatically detected number of workers
job_server = pp.Server(ppservers=ppservers)
print "Starting pp with", job_server.get_ncpus(), "workers"
# Submit a job of calulating sum_primes(100) for execution.
# sum_primes - the function
# (100,) - tuple with arguments for sum_primes
# (isprime,) - tuple with functions on which function sum_primes depends
# ("math",) - tuple with module names which must be imported before sum_primes execution
# Execution starts as soon as one of the workers will become available
job1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))
# Retrieves the result calculated by job1
# The value of job1() is the same as sum_primes(100)
# If the job has not been finished yet, execution will wait here until result is available
result = job1()
print "Sum of primes below 100 is", result
start_time = time.time()
# The following submits 8 jobs and then retrieves the results
inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)
jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
for input, job in jobs:
print "Sum of primes below", input, "is", job()
print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()
python做并行计算_python做并行计算可以吗相关推荐
- python多核并行计算_Python 多核并行计算
以前写点小程序其实根本不在乎并行,单核跑跑也没什么问题,而且我的电脑也只有双核四个超线程(下面就统称核好了),觉得去折腾并行没啥意义(除非在做IO密集型任务).然后自从用上了32核128GB内存,看到 ...
- python文本去重_Python做文本按行去重的实现方法
文本: 每行在promotion后面包含一些数字,如果这些数字是相同的,则认为是相同的行,对于相同的行,只保留一行. 思路: 根据字典和字符串切割. 建立一个空字典. 读入文本,并对每行切割前半部分, ...
- python整人程序_python—做一个恶搞程序
python-做一个恶搞程序 very easy print("You can't win this game") #你不可能赢这个游戏 n = int(input("B ...
- python分类预测_python做logistic分类预测尝试
首先需要明确一点是logistics回归和线性回归的区别: 1线性回归的因变量Y是连续性变量,而logistics回归是分类变量. 2线性回归最后可以通过函数y=a1x1+a2x2...去描述,而lo ...
- python制作图_Python做图的方法
最近一直没有更新文章,在学习Machine learning.然后业余在kaggle那里瞎转,对Python常用的作图摸了个遍,本文将对这些作图方法做个简单介绍 一般我们作图主要是为了看数据分布.数据 ...
- python制作恶搞_python—做一个恶搞程序
python-做一个恶搞程序 very easy print( You can t win this game ) #你不可能赢这个游戏 n = int(input( Between 1 and 10 ...
- python天气预报界面_Python 做 UI 超 easy!(3.1)——天气预报小工具
前面我们做了一个简单的视窗应用,已经对 PySimpleGUI 有了大概的 "感觉",做出来的东西别管多简单吧,起码有点 "产品" 的意思了.上一节实现的 de ...
- python adf检验_python做adf检验
adf检验是用来检验序列是否平稳的方式 一般来说是时间序列中的一种检验方法 python中可使用现成的工具statsmodels来实现adf检验 import numpy as np import s ...
- python snownlp了解_python做snowNLP文本情感分类代码示例
一.snowNLP简介和作用 snownlp为python版的文本分析工具.是受到了TextBlob的启发而写的,可以方便的处理中文文本内容. 支持的中文自然语言操作包括: 中文分词 词性标注 情感分 ...
最新文章
- 关于事件相关电位P300应用于视频游戏的研究
- 「BZOJ 3994」「SDOI 2015」约数个数和「莫比乌斯反演」
- 关于[UITableView setEditing:YES animated:YES] 没有animation
- capslock键英语怎么读_刚开始读英文原版书该怎么做?商务英语专业有前途吗?...
- 11-图像梯度-Sobel算子
- 和合符放枕头下的作用_深度解析:记忆棉枕头好,还是乳胶枕头好?
- ATL 线程池的使用
- 带着canvas去流浪系列之五 绘制K线图
- 项目管理之架构、团队、人
- 简单的java程序代码带注释,Java:基于注释的代码注入的简单技术?
- 【Oracle】解锁用户
- 2个表 遍历 组合_7.2 图的存储结构(2)
- ABAP:SmartForms 之二--设计
- 简仿Path的商店表情弹出模块
- OverFeat论文理解
- 小小知识点(十九)护眼色豆沙绿的设置
- 2022备赛蓝桥杯给大家的建议与提醒和资料,值得认真看一下,多拿几十分
- Linux下获取Root权限的方法
- 快手正与NBA洽谈战略合作,涉及内容版权和商业化
- 自制Alfred/Wox插件推荐