Python multi-CPU parallel programming

Source: Internet
Author: User

Python multithreading can only be concurrency, because it intelligently uses a CPU kernel

Multi-CPU parallel computing supported by the PP package under Python


installation

PIP Install pp


Use

#-*-coding:utf-8-*-Import math, sys, time import pp def isprime (n): "" "returns whether n is a prime number" "If not Isinstance (n, int):  Raise TypeError ("argument passed to Is_prime are not ' 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 sumprimes (n): For I in Xrange (ii): SUM ([x for X in Xrange (2,n) if IsPrime (x)] "" "" "" "" "" "" "" "Return sum ([x for X in xrange (2,n) if IsPrime (x)]) inputs = (10 0000, 100100, 100200, 100300, 100400, 100500, 100600, 100700) # start_time = Time.time () # for input in inputs: # Prin T Sumprimes (input) # print ' Single-thread execution, total time consuming ', time.time ()-start_time, ' s ' # 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 Jobserv
  ER with Ncpus workers  Job_server = pp. Server (Ncpus, ppservers=ppservers) Else: # creates jobserver with automatically detected number of workers Job_ser ver = pp. Server (ppservers=ppservers) print "The number of working core threads available to PP", Job_server.get_ncpus (), "workers" start_time = Time.time () jobs = [(i Nput, Job_server.submit (sumprimes, (input,), (IsPrime,), ("Math",))) for input in inputs] #submit提交任务 for input, job in jobs : print "Sum of primes below", input, "is", Job () # Job () Get method Execution result print "Multithreaded execution time:", Time.time ()-Start_time, "s" job _server.print_stats () #输出执行信息

Execution Results

PP Available working core threads 4 workers
sum of primes below 100000 is 454396537
sum of primes below 100100-454996777
sum o F primes below 100200 is 455898156
sum of primes below 100300 be 456700218 Sum of primes below 100400 is
457603451
Sum of primes below 100500 is 458407033
Sum of primes below 100600 is 459412387
sum of primes below 100700 is 460217613
multithreading execution time consuming:  15.4971420765 s
  job Execution Statistics:
 job Count |% of all jobs | Job time sum | time per Job | Job Server
         8 |        100.00 |      60.9828 |     7.622844 | local time
elapsed since server creation 15.4972219467
0 active tasks, 4 cores



Submit function definition
defSubmit (Self, Func, args= (), depfuncs= (), modules= (), Callback=none, callbackargs= (), group=' Default ', Globals=none): "" "submits function to the execution \ func-function to be executed executed method arg S-tuple with arguments of the ' func ' method the arguments passed in, using tuples depfuncs-tuple with functions which might to be called from ' Fu NC ' passes in its own write method, tuple modules-tuple with module names to import incoming module callback-callback function which wil l am called with argument list equal to callbackargs+ (result,) as soon as calculation is D One callbackargs-additional arguments for callback function Group-job Group, are used when wait (group) is called to given group to finish globals-dictionary from which all modules, functio NS and classes is imported, for instance:globals=globals () "" "






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.