【发布时间】:2017-05-07 12:53:24
【问题描述】:
我编写了一个小脚本来在 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% 的使用率。
【问题讨论】:
标签: python multithreading python-3.x multiprocessing threadpool