您可以使用itertools.product创建一个包含嵌套for循环中所有(lowerThreshHold, upperThreshold)对的2元素元组的迭代器,然后将该迭代器拆分为八个块,将一个传递给池中的每个进程。然后你只需对从每个工人那里得到的结果进行排序,以选出最好的总体结果。我还建议使用multiprocessing.Pool而不是{},来简化事情。在import multiprocessing
import itertools
def get_chunks(iterable, chunks=1):
# This is from http://stackoverflow.com/a/2136090/2073595
lst = list(iterable)
return [lst[i::chunks] for i in xrange(chunks)]
def someOtherFunction(lower, upper):
return (lower + upper) / 2
def worker(pairs):
best_accuracy = best_lower = best_upper = 0
for lower_threshold, upper_threshold in pairs:
accuracy = someOtherFunction(lower_threshold, upper_threshold)
if accuracy > best_accuracy:
best_accuracy = accuracy
best_lower = lower_threshold
best_upper = upper_threshold
return best_accuracy, best_lower, best_upper
if __name__ == '__main__':
jobs = []
pairs = itertools.product(xrange(0, 100), xrange(100, 200))
chunked_pairs = get_chunks(pairs, chunks=multiprocessing.cpu_count())
pool = multiprocessing.Pool()
results = pool.map(worker, chunked_pairs)
pool.close()
pool.join()
print results
# Now combine the results
sorted_results = reversed(sorted(results, key=lambda x: x[0]))
print next(sorted_results) # Winner
输出:
^{pr2}$