Show More
@@ -1,442 +1,442 b'' | |||||
1 | """Base class to manage a running kernel""" |
|
1 | """Base class to manage a running kernel""" | |
2 |
|
2 | |||
3 | # Copyright (c) IPython Development Team. |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Distributed under the terms of the Modified BSD License. |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 |
|
5 | |||
6 | from __future__ import absolute_import |
|
6 | from __future__ import absolute_import | |
7 |
|
7 | |||
8 | from contextlib import contextmanager |
|
8 | from contextlib import contextmanager | |
9 | import os |
|
9 | import os | |
10 | import re |
|
10 | import re | |
11 | import signal |
|
11 | import signal | |
12 | import sys |
|
12 | import sys | |
13 | import time |
|
13 | import time | |
14 | import warnings |
|
14 | import warnings | |
15 | try: |
|
15 | try: | |
16 | from queue import Empty # Py 3 |
|
16 | from queue import Empty # Py 3 | |
17 | except ImportError: |
|
17 | except ImportError: | |
18 | from Queue import Empty # Py 2 |
|
18 | from Queue import Empty # Py 2 | |
19 |
|
19 | |||
20 | import zmq |
|
20 | import zmq | |
21 |
|
21 | |||
22 | from IPython.utils.importstring import import_item |
|
22 | from IPython.utils.importstring import import_item | |
23 | from IPython.utils.localinterfaces import is_local_ip, local_ips |
|
23 | from IPython.utils.localinterfaces import is_local_ip, local_ips | |
24 | from IPython.utils.path import get_ipython_dir |
|
24 | from IPython.utils.path import get_ipython_dir | |
25 | from IPython.utils.traitlets import ( |
|
25 | from IPython.utils.traitlets import ( | |
26 | Any, Instance, Unicode, List, Bool, Type, DottedObjectName |
|
26 | Any, Instance, Unicode, List, Bool, Type, DottedObjectName | |
27 | ) |
|
27 | ) | |
28 | from IPython.kernel import ( |
|
28 | from IPython.kernel import ( | |
29 | launch_kernel, |
|
29 | launch_kernel, | |
30 | kernelspec, |
|
30 | kernelspec, | |
31 | ) |
|
31 | ) | |
32 | from .connect import ConnectionFileMixin |
|
32 | from .connect import ConnectionFileMixin | |
33 | from .zmq.session import Session |
|
33 | from .zmq.session import Session | |
34 | from .managerabc import ( |
|
34 | from .managerabc import ( | |
35 | KernelManagerABC |
|
35 | KernelManagerABC | |
36 | ) |
|
36 | ) | |
37 |
|
37 | |||
38 |
|
38 | |||
39 | class KernelManager(ConnectionFileMixin): |
|
39 | class KernelManager(ConnectionFileMixin): | |
40 | """Manages a single kernel in a subprocess on this host. |
|
40 | """Manages a single kernel in a subprocess on this host. | |
41 |
|
41 | |||
42 | This version starts kernels with Popen. |
|
42 | This version starts kernels with Popen. | |
43 | """ |
|
43 | """ | |
44 |
|
44 | |||
45 | # The PyZMQ Context to use for communication with the kernel. |
|
45 | # The PyZMQ Context to use for communication with the kernel. | |
46 | context = Instance(zmq.Context) |
|
46 | context = Instance(zmq.Context) | |
47 | def _context_default(self): |
|
47 | def _context_default(self): | |
48 | return zmq.Context.instance() |
|
48 | return zmq.Context.instance() | |
49 |
|
49 | |||
50 | # the class to create with our `client` method |
|
50 | # the class to create with our `client` method | |
51 | client_class = DottedObjectName('IPython.kernel.blocking.BlockingKernelClient') |
|
51 | client_class = DottedObjectName('IPython.kernel.blocking.BlockingKernelClient') | |
52 | client_factory = Type() |
|
52 | client_factory = Type() | |
53 | def _client_class_changed(self, name, old, new): |
|
53 | def _client_class_changed(self, name, old, new): | |
54 | self.client_factory = import_item(str(new)) |
|
54 | self.client_factory = import_item(str(new)) | |
55 |
|
55 | |||
56 | # The kernel process with which the KernelManager is communicating. |
|
56 | # The kernel process with which the KernelManager is communicating. | |
57 | # generally a Popen instance |
|
57 | # generally a Popen instance | |
58 | kernel = Any() |
|
58 | kernel = Any() | |
59 |
|
59 | |||
60 | kernel_spec_manager = Instance(kernelspec.KernelSpecManager) |
|
60 | kernel_spec_manager = Instance(kernelspec.KernelSpecManager) | |
61 |
|
61 | |||
62 | def _kernel_spec_manager_default(self): |
|
62 | def _kernel_spec_manager_default(self): | |
63 | return kernelspec.KernelSpecManager(ipython_dir=self.ipython_dir) |
|
63 | return kernelspec.KernelSpecManager(ipython_dir=self.ipython_dir) | |
64 |
|
64 | |||
65 | kernel_name = Unicode(kernelspec.NATIVE_KERNEL_NAME) |
|
65 | kernel_name = Unicode(kernelspec.NATIVE_KERNEL_NAME) | |
66 |
|
66 | |||
67 | kernel_spec = Instance(kernelspec.KernelSpec) |
|
67 | kernel_spec = Instance(kernelspec.KernelSpec) | |
68 |
|
68 | |||
69 | def _kernel_spec_default(self): |
|
69 | def _kernel_spec_default(self): | |
70 | return self.kernel_spec_manager.get_kernel_spec(self.kernel_name) |
|
70 | return self.kernel_spec_manager.get_kernel_spec(self.kernel_name) | |
71 |
|
71 | |||
72 | def _kernel_name_changed(self, name, old, new): |
|
72 | def _kernel_name_changed(self, name, old, new): | |
73 | if new == 'python': |
|
73 | if new == 'python': | |
74 | self.kernel_name = kernelspec.NATIVE_KERNEL_NAME |
|
74 | self.kernel_name = kernelspec.NATIVE_KERNEL_NAME | |
75 | # This triggered another run of this function, so we can exit now |
|
75 | # This triggered another run of this function, so we can exit now | |
76 | return |
|
76 | return | |
77 | self.kernel_spec = self.kernel_spec_manager.get_kernel_spec(new) |
|
77 | self.kernel_spec = self.kernel_spec_manager.get_kernel_spec(new) | |
78 | self.ipython_kernel = new in {'python', 'python2', 'python3'} |
|
78 | self.ipython_kernel = new in {'python', 'python2', 'python3'} | |
79 |
|
79 | |||
80 | kernel_cmd = List(Unicode, config=True, |
|
80 | kernel_cmd = List(Unicode, config=True, | |
81 | help="""DEPRECATED: Use kernel_name instead. |
|
81 | help="""DEPRECATED: Use kernel_name instead. | |
82 |
|
82 | |||
83 | The Popen Command to launch the kernel. |
|
83 | The Popen Command to launch the kernel. | |
84 | Override this if you have a custom kernel. |
|
84 | Override this if you have a custom kernel. | |
85 | If kernel_cmd is specified in a configuration file, |
|
85 | If kernel_cmd is specified in a configuration file, | |
86 | IPython does not pass any arguments to the kernel, |
|
86 | IPython does not pass any arguments to the kernel, | |
87 | because it cannot make any assumptions about the |
|
87 | because it cannot make any assumptions about the | |
88 | arguments that the kernel understands. In particular, |
|
88 | arguments that the kernel understands. In particular, | |
89 | this means that the kernel does not receive the |
|
89 | this means that the kernel does not receive the | |
90 | option --debug if it given on the IPython command line. |
|
90 | option --debug if it given on the IPython command line. | |
91 | """ |
|
91 | """ | |
92 | ) |
|
92 | ) | |
93 |
|
93 | |||
94 | def _kernel_cmd_changed(self, name, old, new): |
|
94 | def _kernel_cmd_changed(self, name, old, new): | |
95 | warnings.warn("Setting kernel_cmd is deprecated, use kernel_spec to " |
|
95 | warnings.warn("Setting kernel_cmd is deprecated, use kernel_spec to " | |
96 | "start different kernels.") |
|
96 | "start different kernels.") | |
97 | self.ipython_kernel = False |
|
97 | self.ipython_kernel = False | |
98 |
|
98 | |||
99 | ipython_kernel = Bool(True) |
|
99 | ipython_kernel = Bool(True) | |
100 |
|
100 | |||
101 | ipython_dir = Unicode() |
|
101 | ipython_dir = Unicode() | |
102 | def _ipython_dir_default(self): |
|
102 | def _ipython_dir_default(self): | |
103 | return get_ipython_dir() |
|
103 | return get_ipython_dir() | |
104 |
|
104 | |||
105 | # Protected traits |
|
105 | # Protected traits | |
106 | _launch_args = Any() |
|
106 | _launch_args = Any() | |
107 | _control_socket = Any() |
|
107 | _control_socket = Any() | |
108 |
|
108 | |||
109 | _restarter = Any() |
|
109 | _restarter = Any() | |
110 |
|
110 | |||
111 | autorestart = Bool(False, config=True, |
|
111 | autorestart = Bool(False, config=True, | |
112 | help="""Should we autorestart the kernel if it dies.""" |
|
112 | help="""Should we autorestart the kernel if it dies.""" | |
113 | ) |
|
113 | ) | |
114 |
|
114 | |||
115 | def __del__(self): |
|
115 | def __del__(self): | |
116 | self._close_control_socket() |
|
116 | self._close_control_socket() | |
117 | self.cleanup_connection_file() |
|
117 | self.cleanup_connection_file() | |
118 |
|
118 | |||
119 | #-------------------------------------------------------------------------- |
|
119 | #-------------------------------------------------------------------------- | |
120 | # Kernel restarter |
|
120 | # Kernel restarter | |
121 | #-------------------------------------------------------------------------- |
|
121 | #-------------------------------------------------------------------------- | |
122 |
|
122 | |||
123 | def start_restarter(self): |
|
123 | def start_restarter(self): | |
124 | pass |
|
124 | pass | |
125 |
|
125 | |||
126 | def stop_restarter(self): |
|
126 | def stop_restarter(self): | |
127 | pass |
|
127 | pass | |
128 |
|
128 | |||
129 | def add_restart_callback(self, callback, event='restart'): |
|
129 | def add_restart_callback(self, callback, event='restart'): | |
130 | """register a callback to be called when a kernel is restarted""" |
|
130 | """register a callback to be called when a kernel is restarted""" | |
131 | if self._restarter is None: |
|
131 | if self._restarter is None: | |
132 | return |
|
132 | return | |
133 | self._restarter.add_callback(callback, event) |
|
133 | self._restarter.add_callback(callback, event) | |
134 |
|
134 | |||
135 | def remove_restart_callback(self, callback, event='restart'): |
|
135 | def remove_restart_callback(self, callback, event='restart'): | |
136 | """unregister a callback to be called when a kernel is restarted""" |
|
136 | """unregister a callback to be called when a kernel is restarted""" | |
137 | if self._restarter is None: |
|
137 | if self._restarter is None: | |
138 | return |
|
138 | return | |
139 | self._restarter.remove_callback(callback, event) |
|
139 | self._restarter.remove_callback(callback, event) | |
140 |
|
140 | |||
141 | #-------------------------------------------------------------------------- |
|
141 | #-------------------------------------------------------------------------- | |
142 | # create a Client connected to our Kernel |
|
142 | # create a Client connected to our Kernel | |
143 | #-------------------------------------------------------------------------- |
|
143 | #-------------------------------------------------------------------------- | |
144 |
|
144 | |||
145 | def client(self, **kwargs): |
|
145 | def client(self, **kwargs): | |
146 | """Create a client configured to connect to our kernel""" |
|
146 | """Create a client configured to connect to our kernel""" | |
147 | if self.client_factory is None: |
|
147 | if self.client_factory is None: | |
148 | self.client_factory = import_item(self.client_class) |
|
148 | self.client_factory = import_item(self.client_class) | |
149 |
|
149 | |||
150 | kw = {} |
|
150 | kw = {} | |
151 | kw.update(self.get_connection_info()) |
|
151 | kw.update(self.get_connection_info()) | |
152 | kw.update(dict( |
|
152 | kw.update(dict( | |
153 | connection_file=self.connection_file, |
|
153 | connection_file=self.connection_file, | |
154 | session=self.session, |
|
154 | session=self.session, | |
155 | parent=self, |
|
155 | parent=self, | |
156 | )) |
|
156 | )) | |
157 |
|
157 | |||
158 | # add kwargs last, for manual overrides |
|
158 | # add kwargs last, for manual overrides | |
159 | kw.update(kwargs) |
|
159 | kw.update(kwargs) | |
160 | return self.client_factory(**kw) |
|
160 | return self.client_factory(**kw) | |
161 |
|
161 | |||
162 | #-------------------------------------------------------------------------- |
|
162 | #-------------------------------------------------------------------------- | |
163 | # Kernel management |
|
163 | # Kernel management | |
164 | #-------------------------------------------------------------------------- |
|
164 | #-------------------------------------------------------------------------- | |
165 |
|
165 | |||
166 | def format_kernel_cmd(self, extra_arguments=None): |
|
166 | def format_kernel_cmd(self, extra_arguments=None): | |
167 | """replace templated args (e.g. {connection_file})""" |
|
167 | """replace templated args (e.g. {connection_file})""" | |
168 | extra_arguments = extra_arguments or [] |
|
168 | extra_arguments = extra_arguments or [] | |
169 | if self.kernel_cmd: |
|
169 | if self.kernel_cmd: | |
170 | cmd = self.kernel_cmd + extra_arguments |
|
170 | cmd = self.kernel_cmd + extra_arguments | |
171 | else: |
|
171 | else: | |
172 | cmd = self.kernel_spec.argv + extra_arguments |
|
172 | cmd = self.kernel_spec.argv + extra_arguments | |
173 |
|
173 | |||
174 | ns = dict(connection_file=self.connection_file) |
|
174 | ns = dict(connection_file=self.connection_file) | |
175 | ns.update(self._launch_args) |
|
175 | ns.update(self._launch_args) | |
176 |
|
176 | |||
177 | pat = re.compile(r'\{([A-Za-z0-9_]+)\}') |
|
177 | pat = re.compile(r'\{([A-Za-z0-9_]+)\}') | |
178 | def from_ns(match): |
|
178 | def from_ns(match): | |
179 | """Get the key out of ns if it's there, otherwise no change.""" |
|
179 | """Get the key out of ns if it's there, otherwise no change.""" | |
180 | return ns.get(match.group(1), match.group()) |
|
180 | return ns.get(match.group(1), match.group()) | |
181 |
|
181 | |||
182 | return [ pat.sub(from_ns, arg) for arg in cmd ] |
|
182 | return [ pat.sub(from_ns, arg) for arg in cmd ] | |
183 |
|
183 | |||
184 | def _launch_kernel(self, kernel_cmd, **kw): |
|
184 | def _launch_kernel(self, kernel_cmd, **kw): | |
185 | """actually launch the kernel |
|
185 | """actually launch the kernel | |
186 |
|
186 | |||
187 | override in a subclass to launch kernel subprocesses differently |
|
187 | override in a subclass to launch kernel subprocesses differently | |
188 | """ |
|
188 | """ | |
189 | return launch_kernel(kernel_cmd, **kw) |
|
189 | return launch_kernel(kernel_cmd, **kw) | |
190 |
|
190 | |||
191 | # Control socket used for polite kernel shutdown |
|
191 | # Control socket used for polite kernel shutdown | |
192 |
|
192 | |||
193 | def _connect_control_socket(self): |
|
193 | def _connect_control_socket(self): | |
194 | if self._control_socket is None: |
|
194 | if self._control_socket is None: | |
195 | self._control_socket = self.connect_control() |
|
195 | self._control_socket = self.connect_control() | |
196 | self._control_socket.linger = 100 |
|
196 | self._control_socket.linger = 100 | |
197 |
|
197 | |||
198 | def _close_control_socket(self): |
|
198 | def _close_control_socket(self): | |
199 | if self._control_socket is None: |
|
199 | if self._control_socket is None: | |
200 | return |
|
200 | return | |
201 | self._control_socket.close() |
|
201 | self._control_socket.close() | |
202 | self._control_socket = None |
|
202 | self._control_socket = None | |
203 |
|
203 | |||
204 | def start_kernel(self, **kw): |
|
204 | def start_kernel(self, **kw): | |
205 | """Starts a kernel on this host in a separate process. |
|
205 | """Starts a kernel on this host in a separate process. | |
206 |
|
206 | |||
207 | If random ports (port=0) are being used, this method must be called |
|
207 | If random ports (port=0) are being used, this method must be called | |
208 | before the channels are created. |
|
208 | before the channels are created. | |
209 |
|
209 | |||
210 | Parameters |
|
210 | Parameters | |
211 | ---------- |
|
211 | ---------- | |
212 | **kw : optional |
|
212 | **kw : optional | |
213 | keyword arguments that are passed down to build the kernel_cmd |
|
213 | keyword arguments that are passed down to build the kernel_cmd | |
214 | and launching the kernel (e.g. Popen kwargs). |
|
214 | and launching the kernel (e.g. Popen kwargs). | |
215 | """ |
|
215 | """ | |
216 | if self.transport == 'tcp' and not is_local_ip(self.ip): |
|
216 | if self.transport == 'tcp' and not is_local_ip(self.ip): | |
217 | raise RuntimeError("Can only launch a kernel on a local interface. " |
|
217 | raise RuntimeError("Can only launch a kernel on a local interface. " | |
218 | "Make sure that the '*_address' attributes are " |
|
218 | "Make sure that the '*_address' attributes are " | |
219 | "configured properly. " |
|
219 | "configured properly. " | |
220 | "Currently valid addresses are: %s" % local_ips() |
|
220 | "Currently valid addresses are: %s" % local_ips() | |
221 | ) |
|
221 | ) | |
222 |
|
222 | |||
223 | # write connection file / get default ports |
|
223 | # write connection file / get default ports | |
224 | self.write_connection_file() |
|
224 | self.write_connection_file() | |
225 |
|
225 | |||
226 | # save kwargs for use in restart |
|
226 | # save kwargs for use in restart | |
227 | self._launch_args = kw.copy() |
|
227 | self._launch_args = kw.copy() | |
228 | # build the Popen cmd |
|
228 | # build the Popen cmd | |
229 | extra_arguments = kw.pop('extra_arguments', []) |
|
229 | extra_arguments = kw.pop('extra_arguments', []) | |
230 | kernel_cmd = self.format_kernel_cmd(extra_arguments=extra_arguments) |
|
230 | kernel_cmd = self.format_kernel_cmd(extra_arguments=extra_arguments) | |
231 | if self.kernel_cmd: |
|
231 | env = os.environ.copy() | |
|
232 | # Don't allow PYTHONEXECUTABLE to be passed to kernel process. | |||
|
233 | # If set, it can bork all the things. | |||
|
234 | env.pop('PYTHONEXECUTABLE', None) | |||
|
235 | if not self.kernel_cmd: | |||
232 | # If kernel_cmd has been set manually, don't refer to a kernel spec |
|
236 | # If kernel_cmd has been set manually, don't refer to a kernel spec | |
233 | env = os.environ |
|
|||
234 | else: |
|
|||
235 | # Environment variables from kernel spec are added to os.environ |
|
|||
236 | env = os.environ.copy() |
|
|||
237 | env.update(self.kernel_spec.env or {}) |
|
237 | env.update(self.kernel_spec.env or {}) | |
238 | # launch the kernel subprocess |
|
238 | # launch the kernel subprocess | |
239 | self.kernel = self._launch_kernel(kernel_cmd, env=env, |
|
239 | self.kernel = self._launch_kernel(kernel_cmd, env=env, | |
240 | **kw) |
|
240 | **kw) | |
241 | self.start_restarter() |
|
241 | self.start_restarter() | |
242 | self._connect_control_socket() |
|
242 | self._connect_control_socket() | |
243 |
|
243 | |||
244 | def request_shutdown(self, restart=False): |
|
244 | def request_shutdown(self, restart=False): | |
245 | """Send a shutdown request via control channel |
|
245 | """Send a shutdown request via control channel | |
246 |
|
246 | |||
247 | On Windows, this just kills kernels instead, because the shutdown |
|
247 | On Windows, this just kills kernels instead, because the shutdown | |
248 | messages don't work. |
|
248 | messages don't work. | |
249 | """ |
|
249 | """ | |
250 | content = dict(restart=restart) |
|
250 | content = dict(restart=restart) | |
251 | msg = self.session.msg("shutdown_request", content=content) |
|
251 | msg = self.session.msg("shutdown_request", content=content) | |
252 | self.session.send(self._control_socket, msg) |
|
252 | self.session.send(self._control_socket, msg) | |
253 |
|
253 | |||
254 | def finish_shutdown(self, waittime=1, pollinterval=0.1): |
|
254 | def finish_shutdown(self, waittime=1, pollinterval=0.1): | |
255 | """Wait for kernel shutdown, then kill process if it doesn't shutdown. |
|
255 | """Wait for kernel shutdown, then kill process if it doesn't shutdown. | |
256 |
|
256 | |||
257 | This does not send shutdown requests - use :meth:`request_shutdown` |
|
257 | This does not send shutdown requests - use :meth:`request_shutdown` | |
258 | first. |
|
258 | first. | |
259 | """ |
|
259 | """ | |
260 | for i in range(int(waittime/pollinterval)): |
|
260 | for i in range(int(waittime/pollinterval)): | |
261 | if self.is_alive(): |
|
261 | if self.is_alive(): | |
262 | time.sleep(pollinterval) |
|
262 | time.sleep(pollinterval) | |
263 | else: |
|
263 | else: | |
264 | break |
|
264 | break | |
265 | else: |
|
265 | else: | |
266 | # OK, we've waited long enough. |
|
266 | # OK, we've waited long enough. | |
267 | if self.has_kernel: |
|
267 | if self.has_kernel: | |
268 | self._kill_kernel() |
|
268 | self._kill_kernel() | |
269 |
|
269 | |||
270 | def cleanup(self, connection_file=True): |
|
270 | def cleanup(self, connection_file=True): | |
271 | """Clean up resources when the kernel is shut down""" |
|
271 | """Clean up resources when the kernel is shut down""" | |
272 | if connection_file: |
|
272 | if connection_file: | |
273 | self.cleanup_connection_file() |
|
273 | self.cleanup_connection_file() | |
274 |
|
274 | |||
275 | self.cleanup_ipc_files() |
|
275 | self.cleanup_ipc_files() | |
276 | self._close_control_socket() |
|
276 | self._close_control_socket() | |
277 |
|
277 | |||
278 | def shutdown_kernel(self, now=False, restart=False): |
|
278 | def shutdown_kernel(self, now=False, restart=False): | |
279 | """Attempts to the stop the kernel process cleanly. |
|
279 | """Attempts to the stop the kernel process cleanly. | |
280 |
|
280 | |||
281 | This attempts to shutdown the kernels cleanly by: |
|
281 | This attempts to shutdown the kernels cleanly by: | |
282 |
|
282 | |||
283 | 1. Sending it a shutdown message over the shell channel. |
|
283 | 1. Sending it a shutdown message over the shell channel. | |
284 | 2. If that fails, the kernel is shutdown forcibly by sending it |
|
284 | 2. If that fails, the kernel is shutdown forcibly by sending it | |
285 | a signal. |
|
285 | a signal. | |
286 |
|
286 | |||
287 | Parameters |
|
287 | Parameters | |
288 | ---------- |
|
288 | ---------- | |
289 | now : bool |
|
289 | now : bool | |
290 | Should the kernel be forcible killed *now*. This skips the |
|
290 | Should the kernel be forcible killed *now*. This skips the | |
291 | first, nice shutdown attempt. |
|
291 | first, nice shutdown attempt. | |
292 | restart: bool |
|
292 | restart: bool | |
293 | Will this kernel be restarted after it is shutdown. When this |
|
293 | Will this kernel be restarted after it is shutdown. When this | |
294 | is True, connection files will not be cleaned up. |
|
294 | is True, connection files will not be cleaned up. | |
295 | """ |
|
295 | """ | |
296 | # Stop monitoring for restarting while we shutdown. |
|
296 | # Stop monitoring for restarting while we shutdown. | |
297 | self.stop_restarter() |
|
297 | self.stop_restarter() | |
298 |
|
298 | |||
299 | if now: |
|
299 | if now: | |
300 | self._kill_kernel() |
|
300 | self._kill_kernel() | |
301 | else: |
|
301 | else: | |
302 | self.request_shutdown(restart=restart) |
|
302 | self.request_shutdown(restart=restart) | |
303 | # Don't send any additional kernel kill messages immediately, to give |
|
303 | # Don't send any additional kernel kill messages immediately, to give | |
304 | # the kernel a chance to properly execute shutdown actions. Wait for at |
|
304 | # the kernel a chance to properly execute shutdown actions. Wait for at | |
305 | # most 1s, checking every 0.1s. |
|
305 | # most 1s, checking every 0.1s. | |
306 | self.finish_shutdown() |
|
306 | self.finish_shutdown() | |
307 |
|
307 | |||
308 | self.cleanup(connection_file=not restart) |
|
308 | self.cleanup(connection_file=not restart) | |
309 |
|
309 | |||
310 | def restart_kernel(self, now=False, **kw): |
|
310 | def restart_kernel(self, now=False, **kw): | |
311 | """Restarts a kernel with the arguments that were used to launch it. |
|
311 | """Restarts a kernel with the arguments that were used to launch it. | |
312 |
|
312 | |||
313 | If the old kernel was launched with random ports, the same ports will be |
|
313 | If the old kernel was launched with random ports, the same ports will be | |
314 | used for the new kernel. The same connection file is used again. |
|
314 | used for the new kernel. The same connection file is used again. | |
315 |
|
315 | |||
316 | Parameters |
|
316 | Parameters | |
317 | ---------- |
|
317 | ---------- | |
318 | now : bool, optional |
|
318 | now : bool, optional | |
319 | If True, the kernel is forcefully restarted *immediately*, without |
|
319 | If True, the kernel is forcefully restarted *immediately*, without | |
320 | having a chance to do any cleanup action. Otherwise the kernel is |
|
320 | having a chance to do any cleanup action. Otherwise the kernel is | |
321 | given 1s to clean up before a forceful restart is issued. |
|
321 | given 1s to clean up before a forceful restart is issued. | |
322 |
|
322 | |||
323 | In all cases the kernel is restarted, the only difference is whether |
|
323 | In all cases the kernel is restarted, the only difference is whether | |
324 | it is given a chance to perform a clean shutdown or not. |
|
324 | it is given a chance to perform a clean shutdown or not. | |
325 |
|
325 | |||
326 | **kw : optional |
|
326 | **kw : optional | |
327 | Any options specified here will overwrite those used to launch the |
|
327 | Any options specified here will overwrite those used to launch the | |
328 | kernel. |
|
328 | kernel. | |
329 | """ |
|
329 | """ | |
330 | if self._launch_args is None: |
|
330 | if self._launch_args is None: | |
331 | raise RuntimeError("Cannot restart the kernel. " |
|
331 | raise RuntimeError("Cannot restart the kernel. " | |
332 | "No previous call to 'start_kernel'.") |
|
332 | "No previous call to 'start_kernel'.") | |
333 | else: |
|
333 | else: | |
334 | # Stop currently running kernel. |
|
334 | # Stop currently running kernel. | |
335 | self.shutdown_kernel(now=now, restart=True) |
|
335 | self.shutdown_kernel(now=now, restart=True) | |
336 |
|
336 | |||
337 | # Start new kernel. |
|
337 | # Start new kernel. | |
338 | self._launch_args.update(kw) |
|
338 | self._launch_args.update(kw) | |
339 | self.start_kernel(**self._launch_args) |
|
339 | self.start_kernel(**self._launch_args) | |
340 |
|
340 | |||
341 | @property |
|
341 | @property | |
342 | def has_kernel(self): |
|
342 | def has_kernel(self): | |
343 | """Has a kernel been started that we are managing.""" |
|
343 | """Has a kernel been started that we are managing.""" | |
344 | return self.kernel is not None |
|
344 | return self.kernel is not None | |
345 |
|
345 | |||
346 | def _kill_kernel(self): |
|
346 | def _kill_kernel(self): | |
347 | """Kill the running kernel. |
|
347 | """Kill the running kernel. | |
348 |
|
348 | |||
349 | This is a private method, callers should use shutdown_kernel(now=True). |
|
349 | This is a private method, callers should use shutdown_kernel(now=True). | |
350 | """ |
|
350 | """ | |
351 | if self.has_kernel: |
|
351 | if self.has_kernel: | |
352 |
|
352 | |||
353 | # Signal the kernel to terminate (sends SIGKILL on Unix and calls |
|
353 | # Signal the kernel to terminate (sends SIGKILL on Unix and calls | |
354 | # TerminateProcess() on Win32). |
|
354 | # TerminateProcess() on Win32). | |
355 | try: |
|
355 | try: | |
356 | self.kernel.kill() |
|
356 | self.kernel.kill() | |
357 | except OSError as e: |
|
357 | except OSError as e: | |
358 | # In Windows, we will get an Access Denied error if the process |
|
358 | # In Windows, we will get an Access Denied error if the process | |
359 | # has already terminated. Ignore it. |
|
359 | # has already terminated. Ignore it. | |
360 | if sys.platform == 'win32': |
|
360 | if sys.platform == 'win32': | |
361 | if e.winerror != 5: |
|
361 | if e.winerror != 5: | |
362 | raise |
|
362 | raise | |
363 | # On Unix, we may get an ESRCH error if the process has already |
|
363 | # On Unix, we may get an ESRCH error if the process has already | |
364 | # terminated. Ignore it. |
|
364 | # terminated. Ignore it. | |
365 | else: |
|
365 | else: | |
366 | from errno import ESRCH |
|
366 | from errno import ESRCH | |
367 | if e.errno != ESRCH: |
|
367 | if e.errno != ESRCH: | |
368 | raise |
|
368 | raise | |
369 |
|
369 | |||
370 | # Block until the kernel terminates. |
|
370 | # Block until the kernel terminates. | |
371 | self.kernel.wait() |
|
371 | self.kernel.wait() | |
372 | self.kernel = None |
|
372 | self.kernel = None | |
373 | else: |
|
373 | else: | |
374 | raise RuntimeError("Cannot kill kernel. No kernel is running!") |
|
374 | raise RuntimeError("Cannot kill kernel. No kernel is running!") | |
375 |
|
375 | |||
376 | def interrupt_kernel(self): |
|
376 | def interrupt_kernel(self): | |
377 | """Interrupts the kernel by sending it a signal. |
|
377 | """Interrupts the kernel by sending it a signal. | |
378 |
|
378 | |||
379 | Unlike ``signal_kernel``, this operation is well supported on all |
|
379 | Unlike ``signal_kernel``, this operation is well supported on all | |
380 | platforms. |
|
380 | platforms. | |
381 | """ |
|
381 | """ | |
382 | if self.has_kernel: |
|
382 | if self.has_kernel: | |
383 | if sys.platform == 'win32': |
|
383 | if sys.platform == 'win32': | |
384 | from .zmq.parentpoller import ParentPollerWindows as Poller |
|
384 | from .zmq.parentpoller import ParentPollerWindows as Poller | |
385 | Poller.send_interrupt(self.kernel.win32_interrupt_event) |
|
385 | Poller.send_interrupt(self.kernel.win32_interrupt_event) | |
386 | else: |
|
386 | else: | |
387 | self.kernel.send_signal(signal.SIGINT) |
|
387 | self.kernel.send_signal(signal.SIGINT) | |
388 | else: |
|
388 | else: | |
389 | raise RuntimeError("Cannot interrupt kernel. No kernel is running!") |
|
389 | raise RuntimeError("Cannot interrupt kernel. No kernel is running!") | |
390 |
|
390 | |||
391 | def signal_kernel(self, signum): |
|
391 | def signal_kernel(self, signum): | |
392 | """Sends a signal to the kernel. |
|
392 | """Sends a signal to the kernel. | |
393 |
|
393 | |||
394 | Note that since only SIGTERM is supported on Windows, this function is |
|
394 | Note that since only SIGTERM is supported on Windows, this function is | |
395 | only useful on Unix systems. |
|
395 | only useful on Unix systems. | |
396 | """ |
|
396 | """ | |
397 | if self.has_kernel: |
|
397 | if self.has_kernel: | |
398 | self.kernel.send_signal(signum) |
|
398 | self.kernel.send_signal(signum) | |
399 | else: |
|
399 | else: | |
400 | raise RuntimeError("Cannot signal kernel. No kernel is running!") |
|
400 | raise RuntimeError("Cannot signal kernel. No kernel is running!") | |
401 |
|
401 | |||
402 | def is_alive(self): |
|
402 | def is_alive(self): | |
403 | """Is the kernel process still running?""" |
|
403 | """Is the kernel process still running?""" | |
404 | if self.has_kernel: |
|
404 | if self.has_kernel: | |
405 | if self.kernel.poll() is None: |
|
405 | if self.kernel.poll() is None: | |
406 | return True |
|
406 | return True | |
407 | else: |
|
407 | else: | |
408 | return False |
|
408 | return False | |
409 | else: |
|
409 | else: | |
410 | # we don't have a kernel |
|
410 | # we don't have a kernel | |
411 | return False |
|
411 | return False | |
412 |
|
412 | |||
413 |
|
413 | |||
414 | KernelManagerABC.register(KernelManager) |
|
414 | KernelManagerABC.register(KernelManager) | |
415 |
|
415 | |||
416 |
|
416 | |||
417 | def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs): |
|
417 | def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs): | |
418 | """Start a new kernel, and return its Manager and Client""" |
|
418 | """Start a new kernel, and return its Manager and Client""" | |
419 | km = KernelManager(kernel_name=kernel_name) |
|
419 | km = KernelManager(kernel_name=kernel_name) | |
420 | km.start_kernel(**kwargs) |
|
420 | km.start_kernel(**kwargs) | |
421 | kc = km.client() |
|
421 | kc = km.client() | |
422 | kc.start_channels() |
|
422 | kc.start_channels() | |
423 | kc.wait_for_ready() |
|
423 | kc.wait_for_ready() | |
424 |
|
424 | |||
425 | return km, kc |
|
425 | return km, kc | |
426 |
|
426 | |||
427 | @contextmanager |
|
427 | @contextmanager | |
428 | def run_kernel(**kwargs): |
|
428 | def run_kernel(**kwargs): | |
429 | """Context manager to create a kernel in a subprocess. |
|
429 | """Context manager to create a kernel in a subprocess. | |
430 |
|
430 | |||
431 | The kernel is shut down when the context exits. |
|
431 | The kernel is shut down when the context exits. | |
432 |
|
432 | |||
433 | Returns |
|
433 | Returns | |
434 | ------- |
|
434 | ------- | |
435 | kernel_client: connected KernelClient instance |
|
435 | kernel_client: connected KernelClient instance | |
436 | """ |
|
436 | """ | |
437 | km, kc = start_new_kernel(**kwargs) |
|
437 | km, kc = start_new_kernel(**kwargs) | |
438 | try: |
|
438 | try: | |
439 | yield kc |
|
439 | yield kc | |
440 | finally: |
|
440 | finally: | |
441 | kc.stop_channels() |
|
441 | kc.stop_channels() | |
442 | km.shutdown_kernel(now=True) |
|
442 | km.shutdown_kernel(now=True) |
General Comments 0
You need to be logged in to leave comments.
Login now