在joblib中打印函数的输出

时间:2019-07-02 04:39:50

标签: python parallel-processing joblib

我正在尝试使用joblib来并行化运行在一个函数上的循环。我希望显示功能的中间print命令,而不仅仅是功能的return值。

from joblib import Parallel, delayed

def dummy(i):
    print("the value passed is",i)

Parallel(n_jobs=2)(delayed(dummy)(i) for i in range(0,5,1))

我得到以下输出:

[None,
 None,
 None,
 None,
 None]

我想得到以下输出(或类似的东西):

[the value passed is 0,
the value passed is 1,
the value passed is 2,
the value passed is 3,
the value passed is 4]

编辑: 我只是注意到它确实在启动Jupyter笔记本的终端窗口中打印了我所需的输出。关于如何在笔记本中实际print的任何想法。 预先感谢。

2 个答案:

答案 0 :(得分:1)

将您的i参数放入功能虚拟Parallel(n_jobs=2)(delayed(dummy(i)) for i in range(0,5,1))

答案 1 :(得分:0)

from joblib import Parallel, delayed

def dummy(i):
    return "the value passed is " + str(i)

Parallel(n_jobs=2)(delayed(dummy)(i) for i in range(0,5,1))

输出::

[the value passed is 0,
the value passed is 1,
the value passed is 2,
the value passed is 3,
the value passed is 4]