Md. Mushfiqur Rahman
Md. Mushfiqur Rahman

Reputation: 21

How to handle GCP preemptive shutdown for jupyter notebooks

In preemptive VM instances in Google Cloud Platform, a forced shut down can be called at any time. They allow to run a shutdown script to avoid file loss. But how do I use the script to cause a specific interrupt in my jupyter notebook?

Upvotes: 1

Views: 512

Answers (2)

Md. Mushfiqur Rahman
Md. Mushfiqur Rahman

Reputation: 21

I have come up with a solution.

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)

One can use this code as a shut-down script. It invokes a keyboard interrupt to all existing jupyter kernels. So, a simple try-except block that excepts KeyboardInterrupt can be used inside the jupyter notebook.

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

Upvotes: 1

Kim
Kim

Reputation: 336

Jupyter notebooks work with an instance VM on its backend and you can access them through ssh protocol like the other instances from the compute engine. This means that any script which works in a computer engine instance must work with a jupyter notebook.

In your description I understand you are referring to this shutdown script. This scripts saves a checkpoint while your instance is being shutdown, so it doesn't trigger the shutdown command itself.

There are many ways to shutdown an instance, either from inside the instance (script) as from outside (cloud shell, console UI ...).

Could you explain which is your specific purpose so I can help you further?

Upvotes: 0

Related Questions