【问题标题】:How to run code after an IPython kernel restart如何在 IPython 内核重启后运行代码
【发布时间】:2018-11-15 14:15:19
【问题描述】:

我正在使用 python-64bit ver3.6.5 在 spyder 中运行此代码。有人可以帮助我如何在 ipython 重新启动后运行命令

import IPython
import time

def restart_ipyhton():
    app = IPython.Application.instance()
    app.kernel.do_shutdown(True)  

print('before restart')
restart_ipyhton()
time.sleep(10)
print('after restart')

Even though I specified a delay of 10 seconds, ipython kernel executed the statements "print('after restart')" before it died. Can someone help me how can I run commands after ipython restarts

【问题讨论】:

  • 请点击突出显示的段落以查看我在 spyder ipython 控制台中的输出图像
  • 你为什么要这样做?
  • 简单来说就是解决第三方API调用内存过大的问题。在使用 python 对第三方应用程序 API 发出超过一定数量的调用后,我遇到了内存问题。所以,我想在某些调用之后在 spyder 中杀死 ipython 并重新启动我的内核以完成其余的调用。
  • 你可以释放内存。或者,您可以使用 ipython 魔法清除所有变量。这将导致一个“新”内核。

标签: python ipython spyder


【解决方案1】:

(这里是 Spyder 维护者) 问题是内核重启是异步调用,即它们是在外部进程中执行的,并且无法从代码中判断调用何时完成以及新内核何时完成可用。那是因为代码运行在将要被杀死的内核中。

所以,据我所知,不可能做你想做的事(但我不是 Jupyter 架构方面的专家)。

【讨论】:

    猜你喜欢
    • 2014-12-06
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-12-02
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多