@@ -350,7 +350,7 @@ def should_handle(self, stream, msg, idents):
350350 return False
351351 return True
352352
353- async def dispatch_shell (self , msg , / , stream = None , subshell_id : str | None = None ):
353+ async def dispatch_shell (self , msg , / , subshell_id : str | None = None ):
354354 """dispatch shell requests"""
355355 if len (msg ) == 1 and msg [0 ].buffer == b"stop aborting" :
356356 # Dummy "stop aborting" message to stop aborting execute requests on this subshell.
@@ -386,11 +386,12 @@ async def dispatch_shell(self, msg, /, stream=None, subshell_id: str | None = No
386386 msg_type = msg ["header" ]["msg_type" ]
387387 assert msg ["header" ].get ("subshell_id" ) == subshell_id
388388
389- if stream is None :
390- if self ._supports_kernel_subshells :
391- stream = self .shell_channel_thread .manager .get_other_stream (subshell_id )
392- else :
393- stream = self .shell_stream
389+ if self ._supports_kernel_subshells :
390+ stream = self .shell_channel_thread .manager .get_subshell_to_shell_channel_socket (
391+ subshell_id
392+ )
393+ else :
394+ stream = self .shell_stream
394395
395396 # Only abort execute requests
396397 if msg_type == "execute_request" :
@@ -611,7 +612,7 @@ async def shell_channel_thread_main(self, msg):
611612
612613 # Find inproc pair socket to use to send message to correct subshell.
613614 subshell_manager = self .shell_channel_thread .manager
614- socket = subshell_manager .get_shell_channel_stream (subshell_id )
615+ socket = subshell_manager .get_shell_channel_to_subshell_socket (subshell_id )
615616 assert socket is not None
616617 socket .send_multipart (msg , copy = False )
617618 except Exception :
@@ -630,26 +631,25 @@ async def shell_main(self, subshell_id: str | None, msg):
630631 self .shell_channel_thread ,
631632 threading .main_thread (),
632633 )
633- # Inproc pair socket that this subshell uses to talk to shell channel thread.
634- stream = self .shell_channel_thread .manager .get_other_stream (subshell_id )
634+ socket_pair = self .shell_channel_thread .manager .get_shell_channel_to_subshell_pair (
635+ subshell_id
636+ )
635637 else :
636638 assert subshell_id is None
637639 assert threading .current_thread () == threading .main_thread ()
638- stream = self . shell_stream
640+ socket_pair = None
639641
640642 try :
641643 # Whilst executing a shell message, do not accept any other shell messages on the
642644 # same subshell, so that cells are run sequentially. Without this we can run multiple
643645 # async cells at the same time which would be a nice feature to have but is an API
644646 # change.
645- stream .stop_on_recv ()
646- await self .dispatch_shell (msg , stream = stream , subshell_id = subshell_id )
647+ if socket_pair :
648+ socket_pair .pause_on_recv ()
649+ await self .dispatch_shell (msg , subshell_id = subshell_id )
647650 finally :
648- stream .on_recv (
649- partial (self .shell_main , subshell_id ),
650- copy = False ,
651- )
652- stream .flush ()
651+ if socket_pair :
652+ socket_pair .resume_on_recv ()
653653
654654 def record_ports (self , ports ):
655655 """Record the ports that this kernel is using.
@@ -690,7 +690,7 @@ def _publish_status(self, status, channel, parent=None):
690690 def _publish_status_and_flush (self , status , channel , stream , parent = None ):
691691 """send status on IOPub and flush specified stream to ensure reply is sent before handling the next reply"""
692692 self ._publish_status (status , channel , parent )
693- if stream :
693+ if stream and hasattr ( stream , "flush" ) :
694694 stream .flush (zmq .POLLOUT )
695695
696696 def _publish_debug_event (self , event ):
@@ -835,6 +835,8 @@ async def execute_request(self, stream, ident, parent):
835835 if self ._do_exec_accepted_params ["cell_id" ]:
836836 do_execute_args ["cell_id" ] = cell_id
837837
838+ subshell_id = parent ["header" ].get ("subshell_id" )
839+
838840 # Call do_execute with the appropriate arguments
839841 reply_content = self .do_execute (** do_execute_args )
840842
@@ -1174,9 +1176,9 @@ async def create_subshell_request(self, socket, ident, parent) -> None:
11741176
11751177 # This should only be called in the control thread if it exists.
11761178 # Request is passed to shell channel thread to process.
1177- other_socket = self .shell_channel_thread .manager .get_control_other_socket ()
1178- other_socket .send_json ({"type" : "create" })
1179- reply = other_socket .recv_json ()
1179+ control_socket = self .shell_channel_thread .manager .control_to_shell_channel . from_socket
1180+ control_socket .send_json ({"type" : "create" })
1181+ reply = control_socket .recv_json ()
11801182 self .session .send (socket , "create_subshell_reply" , reply , parent , ident )
11811183
11821184 async def delete_subshell_request (self , socket , ident , parent ) -> None :
@@ -1197,9 +1199,10 @@ async def delete_subshell_request(self, socket, ident, parent) -> None:
11971199
11981200 # This should only be called in the control thread if it exists.
11991201 # Request is passed to shell channel thread to process.
1200- other_socket = self .shell_channel_thread .manager .get_control_other_socket ()
1201- other_socket .send_json ({"type" : "delete" , "subshell_id" : subshell_id })
1202- reply = other_socket .recv_json ()
1202+ control_socket = self .shell_channel_thread .manager .control_to_shell_channel .from_socket
1203+ control_socket .send_json ({"type" : "delete" , "subshell_id" : subshell_id })
1204+ reply = control_socket .recv_json ()
1205+
12031206 self .session .send (socket , "delete_subshell_reply" , reply , parent , ident )
12041207
12051208 async def list_subshell_request (self , socket , ident , parent ) -> None :
@@ -1213,9 +1216,10 @@ async def list_subshell_request(self, socket, ident, parent) -> None:
12131216
12141217 # This should only be called in the control thread if it exists.
12151218 # Request is passed to shell channel thread to process.
1216- other_socket = self .shell_channel_thread .manager .get_control_other_socket ()
1217- other_socket .send_json ({"type" : "list" })
1218- reply = other_socket .recv_json ()
1219+ control_socket = self .shell_channel_thread .manager .control_to_shell_channel .from_socket
1220+ control_socket .send_json ({"type" : "list" })
1221+ reply = control_socket .recv_json ()
1222+
12191223 self .session .send (socket , "list_subshell_reply" , reply , parent , ident )
12201224
12211225 # ---------------------------------------------------------------------------
@@ -1315,7 +1319,7 @@ def _post_dummy_stop_aborting_message(self, subshell_id: str | None) -> None:
13151319 the _aborting flag.
13161320 """
13171321 subshell_manager = self .shell_channel_thread .manager
1318- socket = subshell_manager .get_shell_channel_stream (subshell_id )
1322+ socket = subshell_manager .get_shell_channel_to_subshell_socket (subshell_id )
13191323 assert socket is not None
13201324
13211325 msg = b"stop aborting" # Magic string for dummy message.
0 commit comments