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

1"""Implements an async kernel client""" 

2# Copyright (c) Jupyter Development Team. 

3# Distributed under the terms of the Modified BSD License. 

4 

5import zmq.asyncio 

6from traitlets import Instance, Type 

7 

8from ..channels import AsyncZMQSocketChannel, HBChannel 

9from ..client import KernelClient, reqrep 

10 

11 

12def wrapped(meth, channel): 

13 """Wrap a method on a channel and handle replies.""" 

14 

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) 

22 

23 return _ 

24 

25 

26class AsyncKernelClient(KernelClient): 

27 """A KernelClient with async APIs 

28 

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 """ 

32 

33 context = Instance(zmq.asyncio.Context) 

34 

35 def _context_default(self) -> zmq.asyncio.Context: 

36 self._created_context = True 

37 return zmq.asyncio.Context() 

38 

39 # -------------------------------------------------------------------------- 

40 # Channel proxy methods 

41 # -------------------------------------------------------------------------- 

42 

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 

47 

48 wait_for_ready = KernelClient._async_wait_for_ready 

49 

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) 

56 

57 _recv_reply = KernelClient._async_recv_reply 

58 

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) 

67 

68 is_alive = KernelClient._async_is_alive 

69 execute_interactive = KernelClient._async_execute_interactive 

70 

71 # replies come on the control channel 

72 shutdown = reqrep(wrapped, KernelClient.shutdown, channel="control")