1111import os
1212import socket
1313import sys
14+ import threading
1415import time
1516import typing as t
1617import uuid
1920from functools import partial
2021from signal import SIGINT , SIGTERM , Signals , default_int_handler , signal
2122
23+ from .control import CONTROL_THREAD_NAME
24+
2225if sys .platform != "win32" :
2326 from signal import SIGKILL
2427else :
@@ -187,7 +190,7 @@ def _parent_header(self):
187190 DeprecationWarning ,
188191 stacklevel = 2 ,
189192 )
190- return self .get_parent (channel = "shell" )
193+ return self .get_parent ()
191194
192195 # Time to sleep after flushing the stdout/err buffers in each execute
193196 # cycle. While this introduces a hard limit on the minimal latency of the
@@ -598,7 +601,7 @@ def _publish_debug_event(self, event):
598601 self .iopub_socket ,
599602 "debug_event" ,
600603 event ,
601- parent = self .get_parent ("control" ),
604+ parent = self .get_parent (),
602605 ident = self ._topic ("debug_event" ),
603606 )
604607
@@ -614,7 +617,7 @@ def set_parent(self, ident, parent, channel="shell"):
614617 self ._parent_ident [channel ] = ident
615618 self ._parents [channel ] = parent
616619
617- def get_parent (self , channel = "shell" ):
620+ def get_parent (self , channel = None ):
618621 """Get the parent request associated with a channel.
619622
620623 .. versionadded:: 6
@@ -629,6 +632,14 @@ def get_parent(self, channel="shell"):
629632 message : dict
630633 the parent message for the most recent request on the channel.
631634 """
635+
636+ if channel is None :
637+ # If a channel is not specified, get information from current thread
638+ if threading .current_thread ().name == CONTROL_THREAD_NAME :
639+ channel = "control"
640+ else :
641+ channel = "shell"
642+
632643 return self ._parents .get (channel , {})
633644
634645 def send_response (
@@ -641,7 +652,7 @@ def send_response(
641652 track = False ,
642653 header = None ,
643654 metadata = None ,
644- channel = "shell" ,
655+ channel = None ,
645656 ):
646657 """Send a response to the message we're currently processing.
647658
0 commit comments