Jupyter Notebook如何同时运行多个单元格?

6

我定义了一个运行bash脚本的Python函数。假设这个函数是:calc(x,y,z)。如果我在Python中使用一些变量运行这个函数,

>>> calc(1,2,3)

它生成一个C代码,使用变量(x=1, y=2, z=3)来模拟某些东西,编译C代码并执行已编译的输出文件。
我想在Jupyter笔记本中同时运行多个具有不同(x,y,z)的calc(x,y,z)。正如您可能已经注意到的那样,问题在于Jupyter笔记本中的单元格是按顺序执行的。如果我运行三个calc函数,则需要三倍于一个函数运行时间的时间。
我尝试了两种方法,但它们效果不佳。
  1. Use multiprocessing module: By using the module, it is possible to execute multiple calcs simultaneously in "one cell". But for later analysis, I would like to execute multiple cells simultaneously which include only one calc each using different processors (or cpu cores).
  2. Use ipyparallel cell magic (inspired by this answer): I tried as following after import ipyparallel

    # Cell 1
    %%px --targets 0 # use processor 0
    calc(1,1,1)
    

    .

    # Cell 2
    %%px --targets 1 # use processor 1
    calc(2,2,2)        
    

    .

    # Cell 3
    %%px --targets 2 # use processor 2
    calc(3,3,3) 
    

但是这些单元格是按顺序执行的:在完成单元格1的模拟后,才会执行单元格2,单元格3也是类似。

如何使用不同的核心运行多个Jupyter单元格?

1个回答

2
在您的解决方案中,单元格按预期由不同引擎执行。问题是由默认的阻塞行为引起的。您可以简单地添加--noblock参数以非阻塞模式执行单元格。然后,该单元格将返回AsyncResult对象,通过调用方法display_outputs()可以在执行完成后读取输出。有关详细信息,请参阅文档targets-and-blocking
# Cell 1
%%px --targets 0 --noblock
calc(1,1,1)

.

# Cell 2
%%px --targets 1 --noblock
calc(2,2,2)   

.

# Cell 3
%%px --targets 2 --noblock
calc(3,3,3) 

如果您需要访问输出,您可以像我上面解释的那样调用 display_outputs() 。
# output of the first the cell 1
___.display_outputs()

# output of the first the cell 2
__.display_outputs()

# output of the first the cell 1
_.display_outputs()

我使用下划线符号访问由单元格1-3返回的AsyncResult对象。还有其他访问这些对象的方法,例如通过使用Out[x],其中x是在执行单元格后在笔记本中可见的单元格执行编号。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接