Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 17, 2024
1 parent 35a45d1 commit 7f11923
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions ipykernel/subshell_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,39 @@ def __init__(
# Inproc pair sockets for control channel and main shell (parent subshell).
# Each inproc pair has a "shell_channel" socket used in the shell channel
# thread, and an "other" socket used in the other thread.
self._control_shell_channel_socket = self._create_inproc_pair_socket("control", True)
self._control_other_socket = self._create_inproc_pair_socket("control", False)
self._parent_shell_channel_socket = self._create_inproc_pair_socket(None, True)
self._parent_other_socket = self._create_inproc_pair_socket(None, False)
self.__control_shell_channel_socket: zmq_anyio.Socket | None = None
self.__control_other_socket: zmq_anyio.Socket | None = None
self.__parent_shell_channel_socket: zmq_anyio.Socket | None = None
self.__parent_other_socket: zmq_anyio.Socket | None = None

# anyio memory object stream for async queue-like communication between tasks.
# Used by _create_subshell to tell listen_from_subshells to spawn a new task.
self._send_stream, self._receive_stream = create_memory_object_stream[str]()

@property
def _control_shell_channel_socket(self) -> zmq_anyio.Socket:
if self.__control_shell_channel_socket is None:
self.__control_shell_channel_socket = self._create_inproc_pair_socket("control", True)
return self.__control_shell_channel_socket

@property
def _control_other_socket(self) -> zmq_anyio.Socket:
if self.__control_other_socket is None:
self.__control_other_socket = self._create_inproc_pair_socket("control", False)
return self.__control_other_socket

@property
def _parent_shell_channel_socket(self) -> zmq_anyio.Socket:
if self.__parent_shell_channel_socket is None:
self.__parent_shell_channel_socket = self._create_inproc_pair_socket(None, True)
return self.__parent_shell_channel_socket

@property
def _parent_other_socket(self) -> zmq_anyio.Socket:
if self.__parent_other_socket is None:
self.__parent_other_socket = self._create_inproc_pair_socket(None, False)
return self.__parent_other_socket

def close(self) -> None:
"""Stop all subshells and close all resources."""
assert current_thread().name == SHELL_CHANNEL_THREAD_NAME
Expand All @@ -73,10 +97,10 @@ def close(self) -> None:
self._receive_stream.close()

for socket in (
self._control_shell_channel_socket,
self._control_other_socket,
self._parent_shell_channel_socket,
self._parent_other_socket,
self.__control_shell_channel_socket,
self.__control_other_socket,
self.__parent_shell_channel_socket,
self.__parent_other_socket,
):
if socket is not None:
socket.close()
Expand Down

0 comments on commit 7f11923

Please sign in to comment.