import multiprocessing import math def factorize_naive(n): """ A naive factorization method. Take integer 'n', return list of factors. """ if n < 2: return [] factors = [] p = 2
while True: if n == 1: return factors
r = n % p if r == 0: factors.append(p) n = n / p elif p * p >= n: factors.append(n) return factors elif p > 2: # Advance in steps of 2 over odd numbers p += 2 else: # If p == 2, get to 3 p += 1 assert False, "unreachable"
def worker(nums, out_q): """ The worker function, invoked in a process. 'nums' is a list of numbers to factor. The results are placed in a dictionary that's pushed to a queue. """ outdict = {} for n in nums: outdict[n] = factorize_naive(n) out_q.put(outdict)
def mp_factorizer(nums, nprocs):
# Each process will get 'chunksize' nums and a queue to put his out # dict into # out_q = Queue() out_q = multiprocessing.Manager().Queue() chunksize = int(math.ceil(len(nums) / float(nprocs))) procs = []
for i in range(nprocs): p = multiprocessing.Process( target=worker, args=(nums[chunksize * i:chunksize * (i + 1)], out_q)) procs.append(p) p.start()
# Collect all results into a single result dict. We know how many dicts # with results to expect. resultdict = {} for i in range(nprocs): resultdict.update(out_q.get())
# Wait for all worker processes to finish for p in procs: p.join()