【发布时间】:2019-10-09 15:01:57
【问题描述】:
我在下面有一个 Python 函数示例,它只接受一个变量并在返回之前对其执行简单的数学运算。
如果我并行化这个函数,以更好地反映我想在现实生活中执行的操作,并运行并行化函数 10 次,我注意到在我的 IDE 上,尽管使用了 del results 行,内存还是增加了。
import multiprocessing as mp
import numpy as np
from tqdm import tqdm
def function(x):
return x*2
test_array = np.arange(0,1e4,1)
for i in range(10):
pool = mp.Pool(processes=4)
results = list(tqdm(pool.imap(function,test_array),total=len(test_array)))
results = [x for x in results if str(x) != 'nan']
del results
我有几个问题希望知道答案:
- 有没有办法防止这种内存增加?
- 这是由于并行化过程导致的内存加载吗?
【问题讨论】:
-
能否尝试打印出delete语句前后的内存使用情况?这个答案显示了如何做到这一点:stackoverflow.com/questions/938733/…
-
没问题。对于上面的确切示例:之前:163520512 之后:164524032(以字节为单位)
标签: python memory parallel-processing multiprocessing ram