##// END OF EJS Templates
Adding KerelRestarter.
Brian E. Granger -
Show More
@@ -0,0 +1,62 b''
1 """A basic in process kernel monitor with autorestarting.
2
3 This watches a kernel's state using KernelManager.is_alive and auto
4 restarts the kernel if it dies.
5 """
6
7 #-----------------------------------------------------------------------------
8 # Copyright (C) 2013 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
12 #-----------------------------------------------------------------------------
13
14 #-----------------------------------------------------------------------------
15 # Imports
16 #-----------------------------------------------------------------------------
17
18 import zmq
19 from zmq.eventloop import ioloop
20
21
22 from IPython.config.configurable import LoggingConfigurable
23 from IPython.utils.traitlets import (
24 Instance, Float
25 )
26
27 #-----------------------------------------------------------------------------
28 # Code
29 #-----------------------------------------------------------------------------
30
31 class KernelRestarter(LoggingConfigurable):
32 """Monitor and autorestart a kernel."""
33
34 loop = Instance('zmq.eventloop.ioloop.IOLoop', allow_none=False)
35 def _loop_default(self):
36 return ioloop.IOLoop.instance()
37
38 kernel_manager = Instance('IPython.kernel.kernelmanager.KernelManager')
39
40 time_to_dead = Float(3.0, config=True,
41 help="""Kernel heartbeat interval in seconds."""
42 )
43
44 def __init__(self, **kwargs):
45 super(KernelRestarter, self).__init__(**kwargs)
46
47 def start(self):
48 self.pc = ioloop.PeriodicCallback(self.poll, self.time_to_dead, self.ioloop)
49 self.pc.start()
50
51 def poll(self):
52 if not self.kernel_manager.is_alive():
53 self.stop()
54 # This restart event should leave the connection file in place so
55 # the ports are the same. Because this takes place below the
56 # MappingKernelManager, the kernel_id will also remain the same.
57 self.kernel_manager.restart_kernel(now=True);
58 self.start()
59
60 def stop(self):
61 self.pc.stop()
62 self.pc = None
General Comments 0
You need to be logged in to leave comments. Login now