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