【问题标题】:Stop execution of Colab notebook from within a cell based on condition根据条件从单元格内停止执行 Colab 笔记本
【发布时间】:2022-09-26 07:09:26
【问题描述】:

基本上我要做的是如果满足条件,则取消当前单元格和所有后续单元格的执行。如果 exit() 停止了笔记本的执行,这就是这个想法(但它只会停止运行它的单元格的执行)

yes_and_no_list = [\'yes\', \'y\', \'no\', \'n\']
response = \'\'
while not response.lower() in yes_and_no_list:
  response = input(\"Continue running notebook?\")

if response in yes_and_no_list[2:]:
    print(\"Notebook execution aborted.\")
    exit()

....所以我想知道是否有一段代码或笔记本魔术可以替换 \'exit()\' 并停止整个 Colab 笔记本的执行?

我已经尝试过对这个问题的回答: How to terminate current colab session from notebook cell

...但是如果我将 exit() 替换为 !kill $(ps aux | awk \'{print $2}\') 它似乎不会打印它上面的行,“Notebook execution aborted.\”,所以我对它的工作原理感到困惑。

    标签: python python-3.x linux jupyter-notebook google-colaboratory


    【解决方案1】:

    您可以使用 google.colab 中的运行时模块来结束会话:

    from google.colab import runtime
    runtime.unassign()
    

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 1970-01-01
      • 2020-11-08
      • 2017-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-06
      相关资源
      最近更新 更多