Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/jupyter_client/asynchronous/client.py: 79%
39 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""Implements an async kernel client"""
2# Copyright (c) Jupyter Development Team.
3# Distributed under the terms of the Modified BSD License.
5import zmq.asyncio
6from traitlets import Instance, Type
8from ..channels import AsyncZMQSocketChannel, HBChannel
9from ..client import KernelClient, reqrep
12def wrapped(meth, channel):
13 """Wrap a method on a channel and handle replies."""
15 def _(self, *args, **kwargs):
16 reply = kwargs.pop("reply", False)
17 timeout = kwargs.pop("timeout", None)
18 msg_id = meth(self, *args, **kwargs)
19 if not reply:
20 return msg_id
21 return self._recv_reply(msg_id, timeout=timeout, channel=channel)
23 return _
26class AsyncKernelClient(KernelClient):
27 """A KernelClient with async APIs
29 ``get_[channel]_msg()`` methods wait for and return messages on channels,
30 raising :exc:`queue.Empty` if no message arrives within ``timeout`` seconds.
31 """
33 context = Instance(zmq.asyncio.Context)
35 def _context_default(self) -> zmq.asyncio.Context:
36 self._created_context = True
37 return zmq.asyncio.Context()
39 # --------------------------------------------------------------------------
40 # Channel proxy methods
41 # --------------------------------------------------------------------------
43 get_shell_msg = KernelClient._async_get_shell_msg
44 get_iopub_msg = KernelClient._async_get_iopub_msg
45 get_stdin_msg = KernelClient._async_get_stdin_msg
46 get_control_msg = KernelClient._async_get_control_msg
48 wait_for_ready = KernelClient._async_wait_for_ready
50 # The classes to use for the various channels
51 shell_channel_class = Type(AsyncZMQSocketChannel)
52 iopub_channel_class = Type(AsyncZMQSocketChannel)
53 stdin_channel_class = Type(AsyncZMQSocketChannel)
54 hb_channel_class = Type(HBChannel)
55 control_channel_class = Type(AsyncZMQSocketChannel)
57 _recv_reply = KernelClient._async_recv_reply
59 # replies come on the shell channel
60 execute = reqrep(wrapped, KernelClient.execute)
61 history = reqrep(wrapped, KernelClient.history)
62 complete = reqrep(wrapped, KernelClient.complete)
63 is_complete = reqrep(wrapped, KernelClient.is_complete)
64 inspect = reqrep(wrapped, KernelClient.inspect)
65 kernel_info = reqrep(wrapped, KernelClient.kernel_info)
66 comm_info = reqrep(wrapped, KernelClient.comm_info)
68 is_alive = KernelClient._async_is_alive
69 execute_interactive = KernelClient._async_execute_interactive
71 # replies come on the control channel
72 shutdown = reqrep(wrapped, KernelClient.shutdown, channel="control")