python paralell_浅尝辄止 Parallel 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/importmath, sys, timeimportppdefisprime(n):"""Returns True if n is prime and False otherwise"""ifnotisinstance(n, int):raiseTypeError("argument passed to is_prime is not of 'int' type")ifn<2:returnFalseifn==2:returnTrue

max=int(math.ceil(math.sqrt(n)))

i=2whilei<=max:ifn%i==0:returnFalse

i+=1returnTruedefsum_primes(n):"""Calculates sum of all primes below given integer n"""returnsum([xforxinxrange(2,n)ifisprime(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 withppservers=()#ppservers = ("10.0.0.1",)iflen(sys.argv)>1:

ncpus=int(sys.argv[1])#Creates jobserver with ncpus workersjob_server=pp.Server(ncpus, ppservers=ppservers)else:#Creates jobserver with automatically detected number of workersjob_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 availablejob1=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 availableresult=job1()print"Sum of primes below 100 is", result

start_time=time.time()#The following submits 8 jobs and then retrieves the resultsinputs=(100000,100100,100200,100300,100400,100500,100600,100700)

jobs=[(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",)))forinputininputs]forinput, jobinjobs:print"Sum of primes below", input,"is", job()print"Time elapsed:", time.time()-start_time,"s"job_server.print_stats()

Paperback: 107 pages Publisher: Packt Publishing - ebooks Account (June 25, 2014) Language: English Develop efficient parallel systems using the robust Python environment Overview Demonstrates the concepts of Python parallel programming Boosts your Python computing capabilities Contains easy-to-understand explanations and plenty of examples In Detail Starting with the basics of parallel programming, you will proceed to learn about how to build parallel algorithms and their implementation. You will then gain the expertise to evaluate problem domains, identify if a particular problem can be parallelized, and how to use the Threading and Multiprocessor modules in Python. The Python Parallel (PP) module, which is another mechanism for parallel programming, is covered in depth to help you optimize the usage of PP. You will also delve into using Celery to perform distributed tasks efficiently and easily. Furthermore, you will learn about asynchronous I/O using the asyncio module. Finally, by the end of this book you will acquire an in-depth understanding about what the Python language has to offer in terms of built-in and external modules for an effective implementation of Parallel Programming. This is a definitive guide that will teach you everything you need to know to develop and maintain high-performance parallel computing systems using the feature-rich Python. What you will learn from this book Explore techniques to parallelize problems Integrate the Parallel Python module to implement Python code Execute parallel solutions on simple problems Achieve communication between processes using Pipe and Queue Use Celery Distributed Task Queue Implement asynchronous I/O using the Python asyncio module Create thread-safe structures Approach A fast, easy-to-follow and clear tutorial to help you develop Parallel computing systems using Python. Along with explaining the fundamentals, the book will also introduce you to slightly advanced concepts and will help you in implementing these techniques in the real world. Who this book is written for If you are an experienced Python programmer and are willing to utilize the available computing resources by parallelizing applications in a simple way, then this book is for you. You are required to have a basic knowledge of Python development to get the most of this book.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值