【发布时间】:2015-12-20 22:04:40
【问题描述】:
我最近发现multiprocessing.Pool 中的apply_async 方法对args=() 中传递的参数大小有限制。真的是这样还是我做错了什么?
我附上了一个示例代码,我只是在被调用函数中打印出矩阵的形状。如您所见,在大小为21000 时,该函数不再被调用(并且没有引发异常或警告:\)
import numpy as np
from multiprocessing import Pool
def _mp_print_size(mat):
try:
print 'Mat size [%d %d]' % (mat.shape[0], mat.shape[1])
except Warning as w:
print w
except Exception as e:
print e
def diff_sizes(size):
print 'Running with size ', size
mat = np.ones([size, size])
pool = Pool(2)
for i in range(2):
pool.apply_async(func=_mp_print_size, args=(mat, ))
pool.close()
pool.join()
if __name__ == '__main__':
sizes = np.arange(1000, 1000000, 10000)
for s in sizes:
diff_sizes(s)
打印出来的是:
Running with size 1000
Mat size [1000 1000]
Mat size [1000 1000]
Running with size 11000
Mat size [11000 11000]
Mat size [11000 11000]
Running with size 21000
Running with size 31000
Running with size 41000
Running with size 51000
Running with size 61000
如果有人问这个问题,我很抱歉 - 与以前的实例相关联,我将不胜感激,因为我自己找不到。
【问题讨论】:
-
一个 2100x2100 的矩阵相当大,为 441,000,00。它被腌制后传递到池中,最终以千兆字节为单位。当我尝试自己腌制数组时出现 MemoryError。这让我想知道多处理是否会默默地忽略该错误。对于像这样的大型对象,您可能希望使用共享内存。
-
你在linux上运行吗?如果是这样,
mat已经存在于子进程空间中,因此您不必发送它。 -
tdelaney - 我知道对吧?! Python中的整个多线程/多进程非常缺乏,通过尝试错误发现了大多数东西:\我在linux上运行它,你能详细说明一下吗?我不能只是不将它传递给函数并期望它工作,它不会编译。
标签: python python-multiprocessing