restarter.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """A basic in process kernel monitor with autorestarting.
  2. This watches a kernel's state using KernelManager.is_alive and auto
  3. restarts the kernel if it dies.
  4. """
  5. # Copyright (c) Jupyter Development Team.
  6. # Distributed under the terms of the Modified BSD License.
  7. from __future__ import absolute_import
  8. import warnings
  9. from zmq.eventloop import ioloop
  10. from jupyter_client.restarter import KernelRestarter
  11. from traitlets import (
  12. Instance,
  13. )
  14. class IOLoopKernelRestarter(KernelRestarter):
  15. """Monitor and autorestart a kernel."""
  16. loop = Instance('tornado.ioloop.IOLoop')
  17. def _loop_default(self):
  18. warnings.warn("IOLoopKernelRestarter.loop is deprecated in jupyter-client 5.2",
  19. DeprecationWarning, stacklevel=4,
  20. )
  21. return ioloop.IOLoop.current()
  22. _pcallback = None
  23. def start(self):
  24. """Start the polling of the kernel."""
  25. if self._pcallback is None:
  26. self._pcallback = ioloop.PeriodicCallback(
  27. self.poll, 1000*self.time_to_dead,
  28. )
  29. self._pcallback.start()
  30. def stop(self):
  31. """Stop the kernel polling."""
  32. if self._pcallback is not None:
  33. self._pcallback.stop()
  34. self._pcallback = None