1

We are using jupyterLab for some long running operations (doing physic simulations in our case). The user should be able to stop these operations safely without killing the kernel.

Is there a clean ways to do this?

Are there maybe even best practices for this?


My cell looks something like this:

environment = gym.make()
running = True
while running:
    environment.step()
    running = ???
serialize(environment)

Notes

wotanii
  • 2,470
  • 20
  • 38

1 Answers1

1

According to https://stackoverflow.com/a/19040553/, IPython interrupts the kernel by sending a SIGINT. Shouldn't it be possible to catch and handle the signal programmatically, as described in How to stop an infinite loop safely in Python?.

Edit: This sounds helpful: graceful interrupt of while loop in ipython notebook

sammy
  • 857
  • 5
  • 13
  • if possible I'd like to keep the original signal handler for emergencies, like when I want to stop the cell fast without losing the state of the kernel. That solution does seem efficient though. – wotanii Dec 02 '19 at 14:36
  • 1
    By the way, another interesting option is using a Context manager, https://stackoverflow.com/a/4205859/9907994 – sammy Dec 09 '19 at 14:46