我写了一个小脚本来在 4 个线程之间分配工作负载并测试结果是否保持有序(相对于输入的顺序):
from multiprocessing import Pool
import numpy as np
import time
import random
rows = 16
columns = 1000000
vals = np.arange(rows * columns, dtype=np.int32).reshape(rows, columns)
def worker(arr):
time.sleep(random.random()) # let the process sleep a random
for idx in np.ndindex(arr.shape): # amount of time to ensure that
arr[idx] += 1 # the processes finish at different
# time steps
return arr
# create the threadpool
with Pool(4) as p:
# schedule one map/worker for each row in the original data
q = p.map(worker, [row for row in vals])
for idx, row in enumerate(q):
print("[{:0>2}]: {: >8} - {: >8}".format(idx, row[0], row[-1]))
对我来说,这总是导致:
[00]: 1 - 1000000
[01]: 1000001 - 2000000
[02]: 2000001 - 3000000
[03]: 3000001 - 4000000
[04]: 4000001 - 5000000
[05]: 5000001 - 6000000
[06]: 6000001 - 7000000
[07]: 7000001 - 8000000
[08]: 8000001 - 9000000
[09]: 9000001 - 10000000
[10]: 10000001 - 11000000
[11]: 11000001 - 12000000
[12]: 12000001 - 13000000
[13]: 13000001 - 14000000
[14]: 14000001 - 15000000
[15]: 15000001 - 16000000
问题:那么, Pool
在存储每个 map
函数的结果时真的保持原始输入的顺序吗 q
?
旁注:我问这个,因为我需要一种简单的方法来并行处理多个工人的工作。在某些情况下,顺序无关紧要。但是,在某些情况下,结果(如 q
)必须按原始顺序返回,因为我使用了一个依赖于有序数据的附加 reduce 函数。
性能:在我的机器上,这个操作比在单个进程上正常执行快 4 倍(正如预期的那样,因为我有 4 个内核)。此外,所有 4 个内核在运行时都处于 100% 使用率。
原文由 daniel451 发布,翻译遵循 CC BY-SA 4.0 许可协议
Pool.map
结果是有序的。如果您需要订单,那很好;如果你不这样做,Pool.imap_unordered
可能是一个有用的优化。请注意,虽然您从
Pool.map
收到结果的顺序是固定的,但它们的计算顺序是任意的。