Parallel Python 分布式计算模块

程序名称:Parallel

授权协议: GPL

操作系统: 跨平台

开发语言: Python

Parallel 介绍

Parallel
Python是Python进行分布式计算的开源模块,能够将计算压力分布到多核CPU或集群的多台计算机上,能够非常方便的在内网中搭建一个自组织的分布式计算平台。先从多核计算开始,普通的Python应用程序只能够使用一个CPU进程,而通过Parallel
Python能够很方便的将计算扩展到多个CPU进程中

示例代码:

#!/usr/bin/python
# File: sum_primes.py
# Author: VItalii Vanovschi
# Desc: This program demonstrates parallel computations with pp module
# It calculates the sum of prime numbers below a given integer in parallel
# Parallel Python Software: http://www.parallelpython.com

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()

# Parallel Python Software: http://www.parallelpython.com

Parallel 官网

http://www.parallelpython.com/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


BlazeDS 是一个基于服务器的Java 远程控制(remoting)和Web消息传递(messaging)技术, 它能够使得后端的Java应用程序和运行在浏览器上的Adobe
OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件、Annotation、POJOs 进行设定。可以使用纯 Java
Volta 是一套开发工具,专为开发分布式、实时系统应用。它包括级联,控制流分析工具,
OpenDDS 是一个开源的 C++ 实现的 对象管理组织 OMG 的 数据分布式服务 (DDS) 。OpenDDS利用自适应通信环境(ACE)提供一个跨平台的环境。
JADE (Java Agent DEvelopment Framework) 是一个完全用Java语言实现的软件框架。它通过一个兼容 FIPA
FastMM ,在D2006和2007中已代替了原来的内存管理器。
WebRTC 是一项在浏览器内部进行实时视频和音频通信的技术,是谷歌于2010年以6820万美元收购VoIP软件开发商 Global IT
gwtwiki - The Java Wikipedia API (Bliki engine),是一个 Wikipedia/Mediawiki 语法解析器,可以把 wiki 的文本转换成 HTML。它支持 wiki 标签,例如 bold, italic, headers, nowiki,
Esper 是一个复杂事件处理组件(CEP - Complex Event Processing),它有 Java 版本和 .NET 版本(NEsper)。
LuaTinker 的作者是Kwon-il Lee韩国人写的,最新的版本是0.2.C,这个C++ wrapper For Lua能够方便和
各种语言环境下gitignore文件的一个集合。
简介 ContentExtractor 是一个开源的网页正文抽取工具,用JAVA实现,具有非常高的抽取精度。
SwiftRandom 是一组函数集合,可以从不同的分布生成伪随机变量。 使用示例: //Single pseudorandom normal variable
java-linq-examples 是 101 个 LINQ 示例的 Java 移植版本。Android 兼容 Java 1.7. 此外还有以下语言移植版本:
mal,Make a Lisp,顾名思义,用图灵完备的编程语言写 Lisp 交互器,目前已经有35种不同语言的实现:
一个 Go 语言实现的中国行政区划查询工具。 介绍 最新中国行政区划,数据来源:http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201608/t20160809_1386477.html
Teaf 简介 Tencent Easy ACE Framework,基于 ACE 的高性能轻量级服务框架,单进程多线程模型,支持 select/epoll 等多种网络
介绍 让 Guzzle 支持 Swoole 协程,这个项目目的就是这么简单明了! Guzzle-Swoole 是 Guzzle 的处理器(Handler),并没有对 Guzzle 本身代码进行修改,理论上可以兼容后续版本。
goproxy-shell goproxy服务端部署脚本 使用方法 wget --no-check-certificate https://github.com/sjz123321/goproxy-
1. pyMd2Doc介紹(pip版本) 利用python将markdown转换成带可收缩、可跳转到文本内容的目录文档。