stdout没有用Jupyter + Python +线程刷新

时间:2016-03-26 20:39:25

标签: python ipython python-multithreading jupyter jupyter-notebook

我有一个Jupyter笔记本,我有一个大概是这样的单元格:

from multiprocessing.pool import ThreadPool
import subprocess as sp

def slow_command(arg):
    res = sp.check_output(["slow_command", arg])
    print "Done:", arg
    return res

results = ThreadPool(4).map(slow_command, args)

但是当我运行单元格时,会有很长的延迟,然后立即打印所有输出:

 *60 second delay*
 Done: 1
 Done: 2
 Done: 3
 ... etc ...

而不是输出打印输出:

*5 second delay*
Done: 1
*5 second delay*
Done: 2
... etc ...

当我用常规ThreadPool(4).map(…)替换map(…)时,输出会按预期显示。

这里发生了什么?当我使用ThreadPool时,有什么方法可以强制显示输出?

我尝试添加sys.stdout.flush()但似乎没有任何区别。

0 个答案:

没有答案