2010-12-21 106 views
0

我提交了以下內容,對我來說運行正常,但我只有一臺核心計算機,並且我認爲documentations評論說它將散佈在更多內核上(如果我擁有它們);自動。這是使用新的concurrent.futures lib的並行計算行嗎?

from concurrent import futures 
import math 

NUMBERS = [ 
    112272537195293, 
    112582718962171, 
    112272537095293, 
    115280098190773, 
    115797840077099, 
    1099726829285419] 

def lowest_factor(n): 
    if n % 2 == 0: 
     return 2 
    sqrt_n = int(math.floor(math.sqrt(n))) 
    for i in range(3, sqrt_n + 1, 2): 
     if n % i == 0: 
      return i 
    return n 

def main(): 
    print('For these numbers:\n ' + '\n '.join(str(p) for p in NUMBERS)) 
    with futures.ProcessPoolExecutor() as executor: 
     low_factor, number = min((l, f) for l, f in zip(executor.map(lowest_factor, NUMBERS), NUMBERS)) 
     print(' The mnimal prime factor is %d of %i' % (low_factor, number)) 

if __name__ == '__main__': 
    main() 

它看起來OK我在Python 3.2b1運行(r32b1:87064,2010年12月5日,19時08分18秒),但我更樂意別人的批評。 P.S.我做了以上這個:http://rosettacode.org/wiki/Parallel_calculations

回答

0

我買了一個多核筆記本電腦的聖誕節。這個例子運行良好。