Jupyter笔记本,如何同时运行多个单元?

COE*_*COE 6 python parallel-processing ipython jupyter-notebook

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

>>> calc(1,2,3)
Run Code Online (Sandbox Code Playgroud)

它生成一个使用变量模拟某些内容的 C 代码(x=1, y=2, z=3),编译 C 代码并执行编译后的输出文件。

我想在 jupyter 笔记本中同时运行多个calc(x,y,z)具有不同 s 的 s 。(x,y,z)您可能已经注意到,问题在于 jupyter Notebook 中的单元格是按顺序执行的。如果我运行三个calc函数,则需要比运行一个函数的时间长三倍的时间。

我尝试了两种方法,但效果不佳。

  1. 使用multiprocessing模块:通过使用模块,可以calc在“一个单元”中同时执行多个操作。但为了以后的分析,我想同时执行多个单元,其中每个单元仅calc使用不同的处理器(或 CPU 内核)。
  2. 使用ipyparallel细胞魔法(受此答案启发):导入后我尝试如下ipyparallel

    # Cell 1
    %%px --targets 0 # use processor 0
    calc(1,1,1)
    
    Run Code Online (Sandbox Code Playgroud)

    # Cell 2
    %%px --targets 1 # use processor 1
    calc(2,2,2)        
    
    Run Code Online (Sandbox Code Playgroud)

    # Cell 3
    %%px --targets 2 # use processor 2
    calc(3,3,3) 
    
    Run Code Online (Sandbox Code Playgroud)

但单元是按顺序执行的:单元 2 在单元 1 模拟完成后执行,与单元 3 类似。

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

小智 2

在您的解决方案中,单元按照您的预期由不同的引擎执行。该问题是由默认阻止行为引起的。您只需添加--noblock参数即可以非阻塞模式执行单元格。然后,单元返回AsyncResult对象,一旦执行完成,就可以通过调用 method 读取输出display_outputs()。请参阅文档了解详细信息目标和阻止

# Cell 1
%%px --targets 0 --noblock
calc(1,1,1)
Run Code Online (Sandbox Code Playgroud)

# Cell 2
%%px --targets 1 --noblock
calc(2,2,2)   
Run Code Online (Sandbox Code Playgroud)

# Cell 3
%%px --targets 2 --noblock
calc(3,3,3) 
Run Code Online (Sandbox Code Playgroud)

如果您需要访问输出,您可以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()
Run Code Online (Sandbox Code Playgroud)

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