Jupyter 如何在背景上 运行 单元格?

Jupyter how to run cells on background?

通常,如果网络连接丢失或网络浏览器关闭,而单元格 运行ning 我必须从头开始重新 运行 个单元格。

为了解决这个问题,我将 .ipynb 转换为 .py 并使用了 'nohup python .py'

有什么方法可以防止 运行ning 单元像 nohup 一样停止?

提前致谢。

对于我的情况:

jupyter --version
4.4.0
jupyter notebook --version
5.7.4

我 运行 服务器中的一个 jupyter notebook,并使用我的笔记本电脑连接到它。即使连接断开或浏览器关闭,运行ning 单元格也不会停止。终端中的文件下载过程运行ning 也不会受到影响。

可以了解一下你的情况吗?

比如你使用哪个版本,你运行你的jupyter notebook在哪里,是在本地还是在服务器上?

您可以在不同的线程中启动您的进程。

def network_call():
    # Do something here

threading.Thread(target=network_call).start()

move_ahead()

根据https://github.com/jupyter/notebook/issues/2446

Anything already running in the notebook will keep running, and the kernel it started for that will stay running - so it won't lose your variables. However, any output produced while the notebook isn't open in a browser tab is lost; there isn't an easy way to change this until we have the notebook server able to track the document state, which has been on the plan for ages.

当您关闭选项卡时,并不意味着进程已结束,它会保持 运行ning 后台无输出。

我不确定现在有没有什么办法可以实现,但是我发现如果你用的是jupyterhub,你可以继续运行 ipython a 终端选项卡,当“关闭”选项卡或切换到另一个设备(关闭上一个网页)时,所有输出都保留。也许 screentmux 更好?

代码仍然 运行s,只是标准 output/error 丢失了。例如,如果我有一个单元格包含:

from time import sleep
sleep(5)
42

然后我保存笔记本,运行这个单元格,关闭浏览器标签,等5秒,然后重新打开标签,我可以运行:

Out

(这是 运行ning 单元格结果的变量)我明白了:

{1: 42}

即代码 运行 完成