How to stop the running cell if interupt kernel does not work in Jupyter Notebook

后端 未结 6 974
我在风中等你
我在风中等你 2021-02-05 00:53

I\'m using Jupyter Notebook for a while. Often when I tried to stop a cell execution, interrupting kernel did not work. In this case, what else can I do, other than just closing

6条回答
  •  忘掉有多难
    2021-02-05 01:19

    If the iPython kernel did not die, you might be able to inject Python code into it that saves important data using pyrasite. You need to install and run pyrasite as root, i.e. with sudo python -m pip install pyrasite or python3 as needed. Then you need to figure out the process id (PID) of the iPython kernel (e.g. via htop or ps aux | grep ipython), say 3873. Then, write a script that saves the state for example to a pickle in a file inject.py, say, it is a Pandas dataframe df in the global scope:

    df.to_pickle("rescued_df.pkl")
    

    Finally, and inject it into the process as follows:

    sudo pyrasite 3873 inject.py
    

    You may need to enable dtrace first like so:

    echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
    

提交回复
热议问题