# python分布式计算框架PP(Parallel Python)集群模式试用

【标题】
python分布式计算框架PP(Parallel Python)集群模式试用

【背景】

Parallel Python库（简称PP）

【结论】

【操作过程】

pip install pp 即可安装pp库

python ppserver.py  -p 35000 -i 192.168.1.104 -s "123456"

#!/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, datetime
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 = ("192.168.1.104:35000",)
#ppservers=("*",)

if len(sys.argv) > 1:
ncpus = int(sys.argv[1])
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers, secret="123456")
else:
# Creates jobserver with automatically detected number of workers
job_server = pp.Server(ppservers=ppservers, secret="123456")

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 = (500000, 500100, 500200, 500300, 500400, 500500, 500600, 500700, 500000, 500100, 500200, 500300, 500400, 500500, 500600, 500700)
#inputs = (1000000, 1000100, 1000200, 1000300, 1000400, 1000500, 1000600, 1000700)
jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]
for input, job in jobs:
print datetime.datetime.now()
print "Sum of primes below", input, "is", job()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()

c:\Python27\python.exe test_pp_official.py
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

Starting pp with 4 workers
Sum of primes below 100 is 1060
2016-08-28 19:07:26.579000
Sum of primes below 500000 is 9914236195
2016-08-28 19:07:33.032000
Sum of primes below 500100 is 9917236483
2016-08-28 19:07:33.035000
Sum of primes below 500200 is 9922237979
2016-08-28 19:07:33.296000
Sum of primes below 500300 is 9926740220
2016-08-28 19:07:33.552000
Sum of primes below 500400 is 9930743046
2016-08-28 19:07:33.821000
Sum of primes below 500500 is 9934746636
2016-08-28 19:07:34.061000
Sum of primes below 500600 is 9938250425
2016-08-28 19:07:37.199000
Sum of primes below 500700 is 9941254397
2016-08-28 19:07:37.202000
Sum of primes below 500000 is 9914236195
2016-08-28 19:07:41.640000
Sum of primes below 500100 is 9917236483
2016-08-28 19:07:41.742000
Sum of primes below 500200 is 9922237979
2016-08-28 19:07:41.746000
Sum of primes below 500300 is 9926740220
2016-08-28 19:07:41.749000
Sum of primes below 500400 is 9930743046
2016-08-28 19:07:41.752000
Sum of primes below 500500 is 9934746636
2016-08-28 19:07:41.756000
Sum of primes below 500600 is 9938250425
2016-08-28 19:07:43.846000
Sum of primes below 500700 is 9941254397
Time elapsed:  17.2770001888 s
Job execution statistics:
job count | % of all jobs | job time sum | time per job | job server
6 |         35.29 |      27.4460 |     4.574333 | 192.168.1.104:35000
11 |         64.71 |      60.2950 |     5.481364 | local
Time elapsed since server creation 17.2849998474
0 active tasks, 4 cores

• 本文已收录于以下专栏：

## python分布式计算dispy简单使用

dispy，是用asyncoro实现的分布式并行计算框架。框架也是非常精简，只有4个组件，在其源码文件夹下可以找到：dispy.py (client) provides two ways of cre...
• suzyu12345
• 2016年12月28日 16:28
• 1713

## Parallel Python——一个简易的分布式计算系统

Parallel Python实现了一种简易的分布式计算方法。
• alaclp
• 2014年07月06日 05:07
• 2634

## Python 分布式计算模块 Parallel

Parallel Python是一个Python模块，它提供了Python代码的在SMP（系统有多个处理器或内核）和集群（计算机通过网络连接）上并行执行的机制。能够将计算压力分布到多核CPU或集群的多...
• fennvde007
• 2014年09月04日 10:54
• 804

## Python学习之分布式进程初探(1)

• pp634077956
• 2016年03月13日 14:07
• 1568

## Python 并行分布式框架：Celery 超详细介绍

celery python 并行分布式 框架 超详细
• liuxiaochen123
• 2015年08月25日 18:34
• 16318

## python zerorpc分布式框架

#Zeromq 是基于zeromq、gevent和 #msgpack开发的分布式RPC框架zerorpc-python。 #服务端 import zerorpc class HelloRpc(ob...
• Chuweitao_1992130
• 2015年08月29日 14:38
• 1390

## Python 并行分布式框架 Celery

Celery 官网：http://www.celeryproject.org/ Celery 官方文档英文版：http://docs.celeryproject.org/en/latest/in...
• freeking101
• 2017年07月13日 17:30
• 990

## python分布式rpc框架zerorpc安装及使用教程

rpc使构建分布式系统简单许多，在云计算的实现中有很广泛的应用 rpc可以是异步的python实现rpc，可以使用标准库里的SimpleXMLRPCServer，另外zerorpc是第三方库支持rp...
• comprel
• 2017年05月25日 22:34
• 4352

## 实战搭建Gearman 分布式处理框架 + python客户端

• robby213
• 2011年05月22日 13:32
• 9726

## spark 集群运行python作业

spark集群运行python作业，pyspark
• xiaolewennofollow
• 2015年07月31日 16:22
• 6471

举报原因： 您举报文章：python分布式计算框架PP(Parallel Python)集群模式试用 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)