通过Jupyter内核执行代码时等待内核就绪(Jupyter Notebook扩展) [英] Waiting for kernel to be ready when executing code via Jupyter kernel (Jupyter Notebook extension)

查看:285
本文介绍了通过Jupyter内核执行代码时等待内核就绪(Jupyter Notebook扩展)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个Jupyter Notebook扩展程序,在其中,一旦内核在初始化期间准备就绪,我就需要执行一些代码.我正在按以下方式执行代码,但我注意到有时在加载扩展程序之前内核已准备就绪,因此自定义事件中的代码永远不会执行.

I'm building a Jupyter Notebook extension where I need to execute some code as soon as Kernel becomes ready during initialization. I'm executing my code as below, but I noticed that sometimes Kernel becomes ready before I load my extension, so the code inside my custom event never gets executed.

Jupyter.notebook.events.one('kernel_ready.Kernel', () => {
     // Couldn't execute this line, because kernel was already
     // ready when this event handler was attached
     Jupyter.notebook.kernel.execute(someCode)
});

我可以只相信笔记本执行代码而不必担心内核准备就绪吗?我猜有某种类型的队列,所以代码最终将被执行.

Can I just trust notebook to execute the code without worrying about kernel to become ready? I'm guessing there is some type of queue, so the code would eventually get executed.

Jupyter.notebook.kernel.execute(someCode)

推荐答案

在nbextension预订kernel_ready事件之前,内核可能已完全准备就绪.在这种情况下,我们可以直接使用内核:

The kernel may be fully ready before the nbextension subscribes to kernel_ready event. In that case we can directly use kernel:

if (Jupyter.notebook.kernel) {
    // ... 
} else {
  Jupyter.notebook.events.one('kernel_ready.Kernel', (e) => {
    // ...
  });
}

这篇关于通过Jupyter内核执行代码时等待内核就绪(Jupyter Notebook扩展)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆