@@ -59,7 +59,7 @@ def __init__(
59
59
self .key = cast (str , self .connection_cfg ["key" ])
60
60
self .session_id = uuid .uuid4 ().hex
61
61
self .msg_cnt = 0
62
- self .execute_requests : Dict [str , Any ] = {}
62
+ self .execute_requests : Dict [str , Dict [ str , asyncio . Future ] ] = {}
63
63
self .channel_tasks : List [asyncio .Task ] = []
64
64
65
65
async def restart (self , startup_timeout : float = float ("inf" )) -> None :
@@ -109,16 +109,14 @@ async def listen_iopub(self):
109
109
msg = await receive_message (self .iopub_channel ) # type: ignore
110
110
msg_id = msg ["parent_header" ].get ("msg_id" )
111
111
if msg_id in self .execute_requests .keys ():
112
- self .execute_requests [msg_id ]["iopub_msg" ] = msg
113
- self .execute_requests [msg_id ]["iopub_event" ].set ()
112
+ self .execute_requests [msg_id ]["iopub_msg" ].set_result (msg )
114
113
115
114
async def listen_shell (self ):
116
115
while True :
117
116
msg = await receive_message (self .shell_channel ) # type: ignore
118
117
msg_id = msg ["parent_header" ].get ("msg_id" )
119
118
if msg_id in self .execute_requests .keys ():
120
- self .execute_requests [msg_id ]["shell_msg" ] = msg
121
- self .execute_requests [msg_id ]["shell_event" ].set ()
119
+ self .execute_requests [msg_id ]["shell_msg" ].set_result (msg )
122
120
123
121
async def execute (
124
122
self ,
@@ -142,35 +140,35 @@ async def execute(
142
140
if wait_for_executed :
143
141
deadline = time .time () + timeout
144
142
self .execute_requests [msg_id ] = {
145
- "iopub_event " : asyncio .Event (),
146
- "shell_event " : asyncio .Event (),
143
+ "iopub_msg " : asyncio .Future (),
144
+ "shell_msg " : asyncio .Future (),
147
145
}
148
146
while True :
149
147
try :
150
148
await asyncio .wait_for (
151
- self .execute_requests [msg_id ]["iopub_event" ]. wait () ,
149
+ self .execute_requests [msg_id ]["iopub_msg" ] ,
152
150
deadline_to_timeout (deadline ),
153
151
)
154
152
except asyncio .TimeoutError :
155
153
error_message = f"Kernel didn't respond in { timeout } seconds"
156
154
raise RuntimeError (error_message )
157
- msg = self .execute_requests [msg_id ]["iopub_msg" ]
155
+ msg = self .execute_requests [msg_id ]["iopub_msg" ]. result ()
158
156
self ._handle_outputs (cell ["outputs" ], msg )
159
157
if (
160
158
msg ["header" ]["msg_type" ] == "status"
161
159
and msg ["content" ]["execution_state" ] == "idle"
162
160
):
163
161
break
164
- self .execute_requests [msg_id ]["iopub_event" ]. clear ()
162
+ self .execute_requests [msg_id ]["iopub_msg" ] = asyncio . Future ()
165
163
try :
166
164
await asyncio .wait_for (
167
- self .execute_requests [msg_id ]["shell_event" ]. wait () ,
165
+ self .execute_requests [msg_id ]["shell_msg" ] ,
168
166
deadline_to_timeout (deadline ),
169
167
)
170
168
except asyncio .TimeoutError :
171
169
error_message = f"Kernel didn't respond in { timeout } seconds"
172
170
raise RuntimeError (error_message )
173
- msg = self .execute_requests [msg_id ]["shell_msg" ]
171
+ msg = self .execute_requests [msg_id ]["shell_msg" ]. result ()
174
172
cell ["execution_count" ] = msg ["content" ]["execution_count" ]
175
173
del self .execute_requests [msg_id ]
176
174
0 commit comments