python可以做并行计算,下面是相关介绍:
一、概览
Parallel Python是一个python模块,提供在SMP(具有多个处理器或多核的系统)和集群(通过网络连接的计算机)上并行执行python代码的机制。它轻巧,易于安装和与其他python软件集成。Parallel Python是一个用纯Python编写的开源和跨平台模块。二、特性
在SMP和集群上并行执行python代码
易于理解和实现基于Job的并行化技术(易于并行转换串行应用程序)
自动检测最佳配置(默认情况下工作进程数设置为有效处理器数)
动态处理器分配(工作进程数可以在运行时更改)
具有相同功能的后续作业的低开销(实现透明高速缓存以减少开销)
动态负载平衡(作业在运行时在处理器之间分布)
容错(如果其中一个节点发生故障,任务在其他节点上重新调度)
计算资源的自动发现
计算资源的动态分配(自动发现和容错的结果)
网络连接的基于SHA的认证
跨平台可移植性和互操作性(Windows,Linux,Unix,Mac OS X)
跨架构可移植性和互操作性(x86,x86-64等)
开源
相关推荐:《python视频教程》
三、动机
现在,用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做并行计算可以吗的详细内容。更多信息请关注PHP中文网其他相关文章!

Python列表切片的基本语法是list[start:stop:step]。1.start是包含的第一个元素索引,2.stop是排除的第一个元素索引,3.step决定元素之间的步长。切片不仅用于提取数据,还可以修改和反转列表。

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/删除,2)储存的二聚体和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,请考虑performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

choosearraysoverlistsinpythonforbetterperformanceandmemoryfliceSpecificScenarios.1)largenumericaldatasets:arraysreducememoryusage.2)绩效 - 临界杂货:arraysoffersoffersOffersOffersOffersPoostSfoostSforsssfortasssfortaskslikeappensearch orearch.3)testessenforcety:arraysenforce:arraysenforc

在Python中,可以使用for循环、enumerate和列表推导式遍历列表;在Java中,可以使用传统for循环和增强for循环遍历数组。1.Python列表遍历方法包括:for循环、enumerate和列表推导式。2.Java数组遍历方法包括:传统for循环和增强for循环。

本文讨论了Python版本3.10中介绍的新“匹配”语句,该语句与其他语言相同。它增强了代码的可读性,并为传统的if-elif-el提供了性能优势

Python中的功能注释将元数据添加到函数中,以进行类型检查,文档和IDE支持。它们增强了代码的可读性,维护,并且在API开发,数据科学和图书馆创建中至关重要。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

Dreamweaver CS6
视觉化网页开发工具

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

WebStorm Mac版
好用的JavaScript开发工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境