Debian Codegraph @

jupyter_client/ioloop/manager.py

raw
1 """A kernel manager with a tornado IOLoop"""
2
3 # Copyright (c) Jupyter Development Team.
4 # Distributed under the terms of the Modified BSD License.
5 import typing as t
6
7 import zmq
8 from tornado import ioloop
9 from traitlets import Instance, Type
10 from zmq.eventloop.zmqstream import ZMQStream
11
12 from ..manager import AsyncKernelManager, KernelManager
13 from .restarter import AsyncIOLoopKernelRestarter, IOLoopKernelRestarter
14
15
16 def as_zmqstream(f: t.Any) -> t.Callable:
17 """Convert a socket to a zmq stream."""
18
19 def wrapped(self: t.Any, *args: t.Any, **kwargs: t.Any) -> t.Any:
20 save_socket_class = None
21 # zmqstreams only support sync sockets
22 if self.context._socket_class is not zmq.Socket:
23 save_socket_class = self.context._socket_class
24 self.context._socket_class = zmq.Socket
25 try:
26 socket = f(self, *args, **kwargs)
27 finally:
28 if save_socket_class:
29 # restore default socket class
30 self.context._socket_class = save_socket_class
31 return ZMQStream(socket, self.loop)
32
33 return wrapped
34
35
36 class IOLoopKernelManager(KernelManager):
37 """An io loop kernel manager."""
38
39 loop = Instance("tornado.ioloop.IOLoop")
40
41 def _loop_default(self) -> ioloop.IOLoop:
42 return ioloop.IOLoop.current()
43
44 restarter_class = Type(
45 default_value=IOLoopKernelRestarter,
46 klass=IOLoopKernelRestarter,
47 help=(
48 "Type of KernelRestarter to use. "
49 "Must be a subclass of IOLoopKernelRestarter.\n"
50 "Override this to customize how kernel restarts are managed."
51 ),
52 config=True,
53 )
54 _restarter: t.Any = Instance("jupyter_client.ioloop.IOLoopKernelRestarter", allow_none=True)
55
56 def start_restarter(self) -> None:
57 """Start the restarter."""
58 if self.autorestart and self.has_kernel:
59 if self._restarter is None:
60 self._restarter = self.restarter_class(
61 kernel_manager=self, loop=self.loop, parent=self, log=self.log
62 )
63 self._restarter.start()
64
65 def stop_restarter(self) -> None:
66 """Stop the restarter."""
67 if self.autorestart and self._restarter is not None:
68 self._restarter.stop()
69
70 connect_shell = as_zmqstream(KernelManager.connect_shell)
71 connect_control = as_zmqstream(KernelManager.connect_control)
72 connect_iopub = as_zmqstream(KernelManager.connect_iopub)
73 connect_stdin = as_zmqstream(KernelManager.connect_stdin)
74 connect_hb = as_zmqstream(KernelManager.connect_hb)
75
76
77 class AsyncIOLoopKernelManager(AsyncKernelManager):
78 """An async ioloop kernel manager."""
79
80 loop = Instance("tornado.ioloop.IOLoop")
81
82 def _loop_default(self) -> ioloop.IOLoop:
83 return ioloop.IOLoop.current()
84
85 restarter_class = Type(
86 default_value=AsyncIOLoopKernelRestarter,
87 klass=AsyncIOLoopKernelRestarter,
88 help=(
89 "Type of KernelRestarter to use. "
90 "Must be a subclass of AsyncIOLoopKernelManager.\n"
91 "Override this to customize how kernel restarts are managed."
92 ),
93 config=True,
94 )
95 _restarter: t.Any = Instance(
96 "jupyter_client.ioloop.AsyncIOLoopKernelRestarter", allow_none=True
97 )
98
99 def start_restarter(self) -> None:
100 """Start the restarter."""
101 if self.autorestart and self.has_kernel:
102 if self._restarter is None:
103 self._restarter = self.restarter_class(
104 kernel_manager=self, loop=self.loop, parent=self, log=self.log
105 )
106 self._restarter.start()
107
108 def stop_restarter(self) -> None:
109 """Stop the restarter."""
110 if self.autorestart and self._restarter is not None:
111 self._restarter.stop()
112
113 connect_shell = as_zmqstream(AsyncKernelManager.connect_shell)
114 connect_control = as_zmqstream(AsyncKernelManager.connect_control)
115 connect_iopub = as_zmqstream(AsyncKernelManager.connect_iopub)
116 connect_stdin = as_zmqstream(AsyncKernelManager.connect_stdin)
117 connect_hb = as_zmqstream(AsyncKernelManager.connect_hb)