5353from .iostream import IOPubThread
5454from .ipkernel import IPythonKernel
5555from .parentpoller import ParentPollerUnix , ParentPollerWindows
56+ from .shellchannel import ShellChannelThread
5657from .zmqshell import ZMQInteractiveShell
5758
5859# -----------------------------------------------------------------------------
@@ -143,6 +144,7 @@ class IPKernelApp(BaseIPythonApplication, InteractiveShellApp, ConnectionFileMix
143144 iopub_socket = Any ()
144145 iopub_thread = Any ()
145146 control_thread = Any ()
147+ shell_channel_thread = Any ()
146148
147149 _ports = Dict ()
148150
@@ -367,6 +369,11 @@ def init_control(self, context):
367369 self .control_socket .router_handover = 1
368370
369371 self .control_thread = ControlThread (daemon = True )
372+ self .shell_channel_thread = ShellChannelThread (
373+ context ,
374+ self .shell_socket ,
375+ daemon = True ,
376+ )
370377
371378 def init_iopub (self , context ):
372379 """Initialize the iopub channel."""
@@ -406,6 +413,10 @@ def close(self):
406413 self .log .debug ("Closing control thread" )
407414 self .control_thread .stop ()
408415 self .control_thread .join ()
416+ if self .shell_channel_thread and self .shell_channel_thread .is_alive ():
417+ self .log .debug ("Closing shell channel thread" )
418+ self .shell_channel_thread .stop ()
419+ self .shell_channel_thread .join ()
409420
410421 if self .debugpy_socket and not self .debugpy_socket .closed :
411422 self .debugpy_socket .close ()
@@ -546,10 +557,17 @@ def init_signal(self):
546557
547558 def init_kernel (self ):
548559 """Create the Kernel object itself"""
549- shell_stream = ZMQStream (self .shell_socket )
560+ if self .shell_channel_thread :
561+ shell_stream = ZMQStream (self .shell_socket , self .shell_channel_thread .io_loop )
562+ else :
563+ shell_stream = ZMQStream (self .shell_socket )
550564 control_stream = ZMQStream (self .control_socket , self .control_thread .io_loop )
551565 debugpy_stream = ZMQStream (self .debugpy_socket , self .control_thread .io_loop )
566+
552567 self .control_thread .start ()
568+ if self .shell_channel_thread :
569+ self .shell_channel_thread .start ()
570+
553571 kernel_factory = self .kernel_class .instance # type:ignore[attr-defined]
554572
555573 kernel = kernel_factory (
@@ -560,6 +578,7 @@ def init_kernel(self):
560578 debug_shell_socket = self .debug_shell_socket ,
561579 shell_stream = shell_stream ,
562580 control_thread = self .control_thread ,
581+ shell_channel_thread = self .shell_channel_thread ,
563582 iopub_thread = self .iopub_thread ,
564583 iopub_socket = self .iopub_socket ,
565584 stdin_socket = self .stdin_socket ,
0 commit comments