Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 如何处理jupyter笔记本电脑的GCP抢先关机_Google Cloud Platform_Jupyter Notebook_Shutdown - Fatal编程技术网

Google cloud platform 如何处理jupyter笔记本电脑的GCP抢先关机

Google cloud platform 如何处理jupyter笔记本电脑的GCP抢先关机,google-cloud-platform,jupyter-notebook,shutdown,Google Cloud Platform,Jupyter Notebook,Shutdown,在谷歌云平台的抢占式虚拟机实例中,可以随时调用强制关闭。它们允许运行关闭脚本以避免文件丢失。但是如何使用脚本在我的jupyter笔记本中引起特定的中断?jupyter笔记本使用其后端的实例VM,您可以通过ssh协议访问它们,就像从计算引擎访问其他实例一样。这意味着在计算机引擎实例中工作的任何脚本都必须与jupyter笔记本一起工作 在你的描述中,我知道你指的是这个。此脚本在关闭实例时保存检查点,因此不会触发shutdown命令本身 有两种方法可以关闭实例,一种是从实例内部(脚本)关闭实例,另一种

在谷歌云平台的抢占式虚拟机实例中,可以随时调用强制关闭。它们允许运行关闭脚本以避免文件丢失。但是如何使用脚本在我的jupyter笔记本中引起特定的中断?

jupyter笔记本使用其后端的实例VM,您可以通过ssh协议访问它们,就像从计算引擎访问其他实例一样。这意味着在计算机引擎实例中工作的任何脚本都必须与jupyter笔记本一起工作

在你的描述中,我知道你指的是这个。此脚本在关闭实例时保存检查点,因此不会触发shutdown命令本身

有两种方法可以关闭实例,一种是从实例内部(脚本)关闭实例,另一种是从外部关闭实例(云shell、控制台UI…)


你能解释一下你的具体目的是什么,我可以进一步帮助你吗?

我想出了一个解决办法

from os import getpid, kill
from time import sleep
import signal
import ipykernel
import psutil


def get_active_kernels():
    active_kernels = []
    pids = psutil.pids()
    my_pid = getpid()

    for pid in pids:
        if pid == my_pid:
            continue
        try:
            p = psutil.Process(pid)
            cmd = p.cmdline()
            for arg in cmd:
                if arg.count('ipykernel'):
                    active_kernels.append(pid)
        except psutil.AccessDenied:
            continue
    return active_kernels

if __name__ == '__main__':
    kernels = get_active_kernels()
    for kernel in kernels:
        kill(kernel, signal.SIGINT)
可以将此代码用作关闭脚本。它对所有现有的jupyter内核调用键盘中断。因此,在jupyter笔记本中可以使用除键盘中断之外的块

try:
    ... #regular code
except KeyboardInterrupt:
    ... #code to save the progress

我使用jupyter进行深度学习培训。突然关机时,如果调用关机时不保存,可能会丢失经过训练的模型。很明显,我可以在每个时代之后保存模型。但这对我来说似乎不是个好办法。我希望在调用关机时触发保存。