vle*_*tre 2 python jupyter-notebook
我正在 Jupyter 笔记本中运行一个函数,我想知道如果用户中断内核是否可以执行一些代码。
例如,如果你有这个功能:
import time
def time_sleep():
time.sleep(5)
print('hello')
Run Code Online (Sandbox Code Playgroud)
print('cell terminated')如果单元格被中断,我可以添加运行吗?
键盘中断 (ctrl+c) 表现为KeyboardInterrupt异常,所以
try:
time.sleep(5)
print('That was a nice nap.')
except KeyboardInterrupt:
print('What a rude awakening!')
Run Code Online (Sandbox Code Playgroud)
如果 Jupyter 向内核发送一个真正的中断信号(显然它确实如此!)。
| 归档时间: |
|
| 查看次数: |
342 次 |
| 最近记录: |