python 多cpu並行編程

來源:互聯網
上載者:User

python 多線程只能算並發,因為它智能使用一個cpu核心

python 下 pp包支援多cpu並行計算


安裝  

pip install pp


使用

#-*- coding: UTF-8 -*-import math, sys, timeimport ppdef IsPrime(n):    """返回n是否是素數"""    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 Truedef SumPrimes(n):    for i in xrange(15):        sum([x for x in xrange(2,n) if IsPrime(x)])    """計算從2-n之間的所有素數之和"""    return sum([x for x in xrange(2,n) if IsPrime(x)])inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)# start_time = time.time()# for input in inputs:#     print SumPrimes(input)# print '單線程執行,總耗時', time.time() - start_time, 's'# # tuple of all parallel python servers to connect withppservers = ()#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 "pp 可以用的工作核心線程數", job_server.get_ncpus(), "workers"start_time = time.time()jobs = [(input, 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()擷取方法執行結果print "多線程下執行耗時: ", time.time() - start_time, "s"job_server.print_stats()#輸出執行資訊

執行結果

pp 可以用的工作核心線程數 4 workersSum of primes below 100000 is 454396537Sum of primes below 100100 is 454996777Sum of primes below 100200 is 455898156Sum of primes below 100300 is 456700218Sum of primes below 100400 is 457603451Sum of primes below 100500 is 458407033Sum of primes below 100600 is 459412387Sum of primes below 100700 is 460217613多線程下執行耗時:  15.4971420765 sJob execution statistics: job count | % of all jobs | job time sum | time per job | job server         8 |        100.00 |      60.9828 |     7.622844 | localTime elapsed since server creation 15.49722194670 active tasks, 4 cores


submit 函數定義
def submit(self, func, args=(), depfuncs=(), modules=(),        callback=None, callbackargs=(), group='default', globals=None):    """Submits function to the execution queue        func - function to be executed  執行的方法        args - tuple with arguments of the 'func' 方法傳入的參數,使用元組        depfuncs - tuple with functions which might be called from 'func' 傳入自己寫的方法 ,元組        modules - tuple with module names to import  傳入 模組        callback - callback function which will be called with argument                list equal to callbackargs+(result,)                as soon as calculation is done        callbackargs - additional arguments for callback function        group - job group, is used when wait(group) is called to wait for        jobs in a given group to finish        globals - dictionary from which all modules, functions and classes        will be imported, for instance: globals=globals()    """






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.