在Jupyter中在后台运行进程 [英] Running a process in the background in Jupyter

查看:193
本文介绍了在Jupyter中在后台运行进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jupyter笔记本中,如果我运行一个需要很长时间的单元格,那么在计算第一个单元格之前,我将无法运行任何其他单元格.

In a Jupyter notebook, if I run a cell that takes a long time, I cannot run any other cell until the first cell is computed.

有没有办法告诉Jupyter在后台进程中运行一个单元,以便我可以继续运行其他单元?

Is there a way to tell Jupyter to run a cell in a background process, so that I can continue to run other cells?

推荐答案

我使用Multiprocessing进行了尝试,发现它不起作用,我不知道这是Windows还是最近的Jupyter/Python的限制.

I tried this using Multiprocessing and found it does not work, I've no idea if this is a limitation of Windows or recent Jupyter/Python.

因此,我根据这项工作使用线程创建了一个解决方案:
https://gist.github.com/maartenbreddels/3378e8257bf0ee18cfcbdacce6e6a77e

So instead I created a solution using Threading, based on this work:
https://gist.github.com/maartenbreddels/3378e8257bf0ee18cfcbdacce6e6a77e All credit to author of this Gist

此方法通过将回调提交到同一事件循环来挂接IPython内部用于执行工作的0MQ事件循环.

This method hooks the 0MQ event loop that IPython uses internally to perform work by submitting callbacks to the same event loop.

我在这里准备了一个示例Jupyter Notebook Gist:
https://gist.github.com/the-moog/94b09b49232731bd2a3cedd24501e23b

I've prepared an example Jupyter Notebook Gist is here:
https://gist.github.com/the-moog/94b09b49232731bd2a3cedd24501e23b

此示例异步更新了一对进度条,您应该能够用执行其他任务的代码替换回调.

This example updates a pair of progress bars asynchronously, you should be able to replace the callback with code that performs other tasks.

(我不喜欢帖子中的链接.如果有人知道如何将笔记本视图直接呈现为stackoverflow,请告诉我)

(I'm not a fan of links in posts. If anybody knows how to render notebook views directly into stackoverflow, let me know)

这篇关于在Jupyter中在后台运行进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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