Is this parallel calculation using the new concurrent.futures lib OK?
I submitted the following, which runs OK for me, but I only have a single core machine and I take the documentations comm开发者_运维知识库ent that it will be spread over more cores if I had them; automatically.
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()
It looks OK to me running in Python 3.2b1 (r32b1:87064, Dec 5 2010, 19:08:18), but I would welcome criticism from others. P.S. I did the above for this: http://rosettacode.org/wiki/Parallel_calculations
I bought a multi-core laptop for xmas. The example worked fine.
精彩评论