|
11 | 11 |
|
12 | 12 | import zmq.asyncio
|
13 | 13 | from traitlets import Any # type: ignore
|
| 14 | +from traitlets import Bool |
14 | 15 | from traitlets import Instance
|
15 | 16 | from traitlets import Type
|
16 | 17 |
|
@@ -92,7 +93,10 @@ class KernelClient(ConnectionFileMixin):
|
92 | 93 | # The PyZMQ Context to use for communication with the kernel.
|
93 | 94 | context = Instance(zmq.asyncio.Context)
|
94 | 95 |
|
| 96 | + _created_context: Bool = Bool(False) |
| 97 | + |
95 | 98 | def _context_default(self) -> zmq.asyncio.Context:
|
| 99 | + self._created_context = True |
96 | 100 | return zmq.asyncio.Context()
|
97 | 101 |
|
98 | 102 | # The classes to use for the various channels
|
@@ -282,6 +286,9 @@ def start_channels(
|
282 | 286 | :meth:`start_kernel`. If the channels have been stopped and you
|
283 | 287 | call this, :class:`RuntimeError` will be raised.
|
284 | 288 | """
|
| 289 | + # Create the context if needed. |
| 290 | + if not self._created_context: |
| 291 | + self.context = self._context_default() |
285 | 292 | if iopub:
|
286 | 293 | self.iopub_channel.start()
|
287 | 294 | if shell:
|
@@ -311,6 +318,9 @@ def stop_channels(self) -> None:
|
311 | 318 | self.hb_channel.stop()
|
312 | 319 | if self.control_channel.is_alive():
|
313 | 320 | self.control_channel.stop()
|
| 321 | + if self._created_context: |
| 322 | + self._created_context = False |
| 323 | + self.context.destroy() |
314 | 324 |
|
315 | 325 | @property
|
316 | 326 | def channels_running(self) -> bool:
|
|
0 commit comments