Show More
@@ -1,67 +1,67 b'' | |||||
1 | """A basic kernel monitor with autorestarting. |
|
1 | """A basic kernel monitor with autorestarting. | |
2 |
|
2 | |||
3 | This watches a kernel's state using KernelManager.is_alive and auto |
|
3 | This watches a kernel's state using KernelManager.is_alive and auto | |
4 | restarts the kernel if it dies. |
|
4 | restarts the kernel if it dies. | |
5 |
|
5 | |||
6 | It is an incomplete base class, and must be subclassed. |
|
6 | It is an incomplete base class, and must be subclassed. | |
7 | """ |
|
7 | """ | |
8 |
|
8 | |||
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 | # Copyright (C) 2013 The IPython Development Team |
|
10 | # Copyright (C) 2013 The IPython Development Team | |
11 | # |
|
11 | # | |
12 | # Distributed under the terms of the BSD License. The full license is in |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
13 | # the file COPYING, distributed as part of this software. |
|
13 | # the file COPYING, distributed as part of this software. | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | #----------------------------------------------------------------------------- |
|
16 | #----------------------------------------------------------------------------- | |
17 | # Imports |
|
17 | # Imports | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 |
|
20 | |||
21 | from IPython.config.configurable import LoggingConfigurable |
|
21 | from IPython.config.configurable import LoggingConfigurable | |
22 | from IPython.utils.traitlets import ( |
|
22 | from IPython.utils.traitlets import ( | |
23 | Instance, Float, List, |
|
23 | Instance, Float, List, | |
24 | ) |
|
24 | ) | |
25 |
|
25 | |||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 | # Code |
|
27 | # Code | |
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 |
|
29 | |||
30 | class KernelRestarter(LoggingConfigurable): |
|
30 | class KernelRestarter(LoggingConfigurable): | |
31 | """Monitor and autorestart a kernel.""" |
|
31 | """Monitor and autorestart a kernel.""" | |
32 |
|
32 | |||
33 | kernel_manager = Instance('IPython.kernel.KernelManager') |
|
33 | kernel_manager = Instance('IPython.kernel.KernelManager') | |
34 |
|
34 | |||
35 | time_to_dead = Float(3.0, config=True, |
|
35 | time_to_dead = Float(3.0, config=True, | |
36 | help="""Kernel heartbeat interval in seconds.""" |
|
36 | help="""Kernel heartbeat interval in seconds.""" | |
37 | ) |
|
37 | ) | |
38 | _callbacks = List() |
|
38 | _callbacks = List() | |
39 |
|
39 | |||
40 | def start(self): |
|
40 | def start(self): | |
41 | """Start the polling of the kernel.""" |
|
41 | """Start the polling of the kernel.""" | |
42 | raise NotImplementedError("Must be implemented in a subclass") |
|
42 | raise NotImplementedError("Must be implemented in a subclass") | |
43 |
|
43 | |||
44 | def stop(self): |
|
44 | def stop(self): | |
45 | """Stop the kernel polling.""" |
|
45 | """Stop the kernel polling.""" | |
46 | raise NotImplementedError("Must be implemented in a subclass") |
|
46 | raise NotImplementedError("Must be implemented in a subclass") | |
47 |
|
47 | |||
48 | def register_callback(self, f): |
|
48 | def register_callback(self, f): | |
49 | """register a callback to fire""" |
|
49 | """register a callback to fire""" | |
50 | self.callbacks.append(f) |
|
50 | self._callbacks.append(f) | |
51 |
|
51 | |||
52 | def unregister_callback(self, f): |
|
52 | def unregister_callback(self, f): | |
53 | try: |
|
53 | try: | |
54 | self.callbacks.remove(f) |
|
54 | self._callbacks.remove(f) | |
55 | except ValueError: |
|
55 | except ValueError: | |
56 | pass |
|
56 | pass | |
57 |
|
57 | |||
58 | def poll(self): |
|
58 | def poll(self): | |
59 | self.log.debug('Polling kernel...') |
|
59 | self.log.debug('Polling kernel...') | |
60 | if not self.kernel_manager.is_alive(): |
|
60 | if not self.kernel_manager.is_alive(): | |
61 | self.log.info('KernelRestarter: restarting kernel') |
|
61 | self.log.info('KernelRestarter: restarting kernel') | |
62 | for callback in self.callbacks: |
|
62 | for callback in self._callbacks: | |
63 | try: |
|
63 | try: | |
64 | callback() |
|
64 | callback() | |
65 | except Exception as e: |
|
65 | except Exception as e: | |
66 | self.log.error("Kernel restart callback %r failed", callback, exc_info=True) |
|
66 | self.log.error("Kernel restart callback %r failed", callback, exc_info=True) | |
67 | self.kernel_manager.restart_kernel(now=True) |
|
67 | self.kernel_manager.restart_kernel(now=True) |
General Comments 0
You need to be logged in to leave comments.
Login now