Python:为什么 Pool.map() 在尝试使用其映射函数的输入参数时会挂起?



我有以下函数(为便于阅读而缩短),我使用 Python 的 (3.5)multiprocessing模块并行化:

def evaluate_prediction(enumeration_tuple):
i = enumeration_tuple[0]
logits_pred = enumeration_tuple[1]
print("This prints succesfully")
print("This never gets printed: ")
print(enumeration_tuple[0])
filename = sample_names_test[i]
onehots_pred = logits_to_onehots(logits_pred)
np.save("/media/nfs/7_raid/ebos/models/fcn/" + channels + "/test/ndarrays/" + filename, onehots_pred)

但是,每当我尝试读取其输入参数时,此函数都会挂起。执行可以越过logits_pred = enumeration_tuple[1]行,正如打印简单字符串的 print 语句所证明的那样,但每当我print(logits_pred)时它就会停止。所以显然,每当我实际需要传递的值时,该过程就会停止。我没有收到异常或错误消息。当使用 Python 的内置map()函数或 for 循环时,该函数会成功完成。我应该有足够的内存 en 计算能力可用。所有进程都写入不同的文件。enumerate(predictions)生成正确的索引值对,如预期的那样。我使用Pool.map()调用此函数:

pool = multiprocessing.Pool()
file_results = pool.map(evaluate_prediction, enumerate(predictions))

为什么挂?我怎样才能得到异常,以便我知道出了什么问题?

更新:将映射函数外包给另一个模块,从那里导入它,并将__init__.py添加到我的目录中后,我设法打印元组中的第一项,而不是第二项。

我以前遇到过类似的问题,对我有用的解决方案是将要并行化的函数放在单独的模块中,然后将其导入。

from eval_prediction import evaluate_prediction
pool = multiprocessing.Pool()
file_results = pool.map(evaluate_prediction, enumerate(predictions))

我假设您将函数定义保存在文件名eval_prediction.py同一目录中。确保你也有__init__.py。

最新更新