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