txl_kernel 0.2.9__tar.gz → 0.2.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.3
2
2
  Name: txl_kernel
3
- Version: 0.2.9
3
+ Version: 0.2.11
4
4
  Summary: TXL plugin for a kernel driver
5
5
  Project-URL: Source, https://github.com/davidbrochart/jpterm/plugins/kernel
6
6
  Author-email: David Brochart <david.brochart@gmail.com>
@@ -0,0 +1 @@
1
+ __version__ = "0.2.11"
@@ -1 +0,0 @@
1
- __version__ = "0.2.9"
@@ -1,197 +0,0 @@
1
- import asyncio
2
- import time
3
- from typing import Dict, List
4
-
5
- from pycrdt import Map
6
-
7
- from .message import create_message
8
-
9
-
10
- def deadline_to_timeout(deadline: float) -> float:
11
- return max(0, deadline - time.monotonic())
12
-
13
-
14
- class Comm:
15
- def __init__(self, comm_id: str, shell_channel, session_id: str, send_message):
16
- self.comm_id = comm_id
17
- self.shell_channel = shell_channel
18
- self.session_id = session_id
19
- self.send_message = send_message
20
- self.msg_cnt = 0
21
-
22
- def send(self, buffers):
23
- msg = create_message(
24
- "comm_msg",
25
- content={"comm_id": self.comm_id},
26
- session_id=self.session_id,
27
- msg_id=self.msg_cnt,
28
- buffers=buffers,
29
- )
30
- self.msg_cnt += 1
31
- asyncio.create_task(self.send_message(msg, self.shell_channel, change_date_to_str=True))
32
-
33
-
34
- class KernelMixin:
35
- def __init__(self):
36
- self.msg_cnt = 0
37
- self.execute_requests: Dict[str, Dict[str, List[asyncio.Future]]] = {}
38
- self.recv_queue = asyncio.Queue()
39
- self.started = asyncio.Event()
40
- asyncio.create_task(self.recv())
41
-
42
- def create_message(self, *args, **kwargs):
43
- return create_message(*args, **kwargs)
44
-
45
- async def wait_for_ready(self, timeout=float("inf")):
46
- deadline = time.monotonic() + timeout
47
- new_timeout = timeout
48
- while True:
49
- msg = create_message(
50
- "kernel_info_request",
51
- session_id=self.session_id,
52
- msg_id=str(self.msg_cnt),
53
- )
54
- self.msg_cnt += 1
55
- await self.send_message(msg, self.shell_channel, change_date_to_str=True)
56
- msg_id = msg["header"]["msg_id"]
57
- self.execute_requests[msg_id] = {
58
- "iopub": [asyncio.Future()],
59
- "shell": [asyncio.Future()],
60
- }
61
- try:
62
- msg = await asyncio.wait_for(self.execute_requests[msg_id]["shell"][0], new_timeout)
63
- except asyncio.TimeoutError:
64
- del self.execute_requests[msg_id]
65
- error_message = f"Kernel didn't respond in {timeout} seconds"
66
- raise RuntimeError(error_message)
67
- if msg["header"]["msg_type"] == "kernel_info_reply":
68
- try:
69
- msg = await asyncio.wait_for(self.execute_requests[msg_id]["iopub"][0], 0.2)
70
- except asyncio.TimeoutError:
71
- pass
72
- else:
73
- break
74
- del self.execute_requests[msg_id]
75
- new_timeout = deadline_to_timeout(deadline)
76
-
77
- async def recv(self):
78
- while True:
79
- msg = await self.recv_queue.get()
80
- channel = msg.pop("channel")
81
- msg_type = msg["header"]["msg_type"]
82
- if msg_type == "comm_open":
83
- for comm_handler in self.comm_handlers:
84
- comm_id = msg["content"]["comm_id"]
85
- comm = Comm(comm_id, self.shell_channel, self.session_id, self.send_message)
86
- comm_handler.comm_open(msg, comm)
87
- elif msg_type == "comm_msg":
88
- for comm_handler in self.comm_handlers:
89
- comm_handler.comm_msg(msg)
90
- msg_id = msg["parent_header"].get("msg_id")
91
- if msg_id in self.execute_requests:
92
- # msg["header"] = str_to_date(msg["header"])
93
- # msg["parent_header"] = str_to_date(msg["parent_header"])
94
- future_msgs = self.execute_requests[msg_id][channel]
95
- if future_msgs:
96
- fut = future_msgs[-1]
97
- if fut.done():
98
- fut = asyncio.Future()
99
- future_msgs.append(fut)
100
- else:
101
- fut = asyncio.Future()
102
- future_msgs.append(fut)
103
- fut.set_result(msg)
104
-
105
- async def execute(
106
- self,
107
- ycell: Map,
108
- timeout: float = float("inf"),
109
- msg_id: str = "",
110
- wait_for_executed: bool = True,
111
- ) -> None:
112
- await self.started.wait()
113
- if ycell["cell_type"] != "code":
114
- return
115
- code = str(ycell["source"])
116
- content = {"code": code, "silent": False}
117
- msg = create_message(
118
- "execute_request",
119
- content,
120
- session_id=self.session_id,
121
- msg_id=str(self.msg_cnt),
122
- )
123
- if msg_id:
124
- msg["header"]["msg_id"] = msg_id
125
- else:
126
- msg_id = msg["header"]["msg_id"]
127
- self.msg_cnt += 1
128
- self.execute_requests[msg_id] = {
129
- "iopub": [asyncio.Future()],
130
- "shell": [asyncio.Future()],
131
- }
132
- await self.send_message(msg, self.shell_channel, change_date_to_str=True)
133
- if wait_for_executed:
134
- deadline = time.monotonic() + timeout
135
- try:
136
- await asyncio.wait_for(
137
- self.execute_requests[msg_id]["iopub"][0],
138
- deadline_to_timeout(deadline),
139
- )
140
- except asyncio.TimeoutError:
141
- del self.execute_requests[msg_id]
142
- error_message = f"Kernel didn't respond in {timeout} seconds"
143
- raise RuntimeError(error_message)
144
- await self._handle_outputs(ycell, self.execute_requests[msg_id]["iopub"])
145
- try:
146
- await asyncio.wait_for(
147
- self.execute_requests[msg_id]["shell"][0],
148
- deadline_to_timeout(deadline),
149
- )
150
- except asyncio.TimeoutError:
151
- del self.execute_requests[msg_id]
152
- error_message = f"Kernel didn't respond in {timeout} seconds"
153
- raise RuntimeError(error_message)
154
- msg = self.execute_requests[msg_id]["shell"][0].result()
155
- ycell["execution_count"] = msg["content"]["execution_count"]
156
- del self.execute_requests[msg_id]
157
-
158
- async def _handle_outputs(self, ycell: Map, future_messages: List[asyncio.Future]):
159
- ycell["outputs"] = []
160
-
161
- while True:
162
- if not future_messages:
163
- future_messages.append(asyncio.Future())
164
- fut = future_messages[0]
165
- if not fut.done():
166
- await fut
167
- future_messages.pop(0)
168
- msg = fut.result()
169
- msg_type = msg["header"]["msg_type"]
170
- content = msg["content"]
171
- outputs = list(ycell["outputs"])
172
- if msg_type == "stream":
173
- if (not outputs) or (outputs[-1]["name"] != content["name"]):
174
- outputs.append({"name": content["name"], "output_type": msg_type, "text": []})
175
- outputs[-1]["text"].append(content["text"])
176
- elif msg_type in ("display_data", "execute_result"):
177
- outputs.append(
178
- {
179
- "data": content["data"],
180
- "execution_count": content["execution_count"],
181
- "metadata": {},
182
- "output_type": msg_type,
183
- }
184
- )
185
- elif msg_type == "error":
186
- outputs.append(
187
- {
188
- "ename": content["ename"],
189
- "evalue": content["evalue"],
190
- "output_type": "error",
191
- "traceback": content["traceback"],
192
- }
193
- )
194
- elif msg_type == "status" and msg["content"]["execution_state"] == "idle":
195
- break
196
-
197
- ycell["outputs"] = outputs
File without changes
File without changes
File without changes
File without changes