Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ipykernel/iostream.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@ def __init__(
self._io_loop = pub_thread.io_loop
self._buffer_lock = threading.RLock()
self._buffers = defaultdict(StringIO)
self._buffer_keys: dict[int, tuple[dict[str, Any], frozenset[Any]]] = {}
self.echo = None
self._isatty = bool(isatty)
self._should_watch = False
Expand Down Expand Up @@ -774,9 +775,13 @@ def write(self, string: str) -> int | None: # type:ignore[override]
raise ValueError(msg)

is_child = not self._is_master_process()
# frozenset(parent.items()) is slow, so keep the keys of the last few parents
entry = self._buffer_keys.get(id(parent))
if entry is None or entry[0] is not parent:
entry = self._buffer_keys[id(parent)] = (parent, frozenset(parent.items()))
# only touch the buffer in the IO thread to avoid races
with self._buffer_lock:
self._buffers[frozenset(parent.items())].write(string)
self._buffers[entry[1]].write(string)
if is_child:
# mp.Pool cannot be trusted to flush promptly (or ever),
# and this helps.
Expand Down Expand Up @@ -815,6 +820,8 @@ def _rotate_buffers(self):
with self._buffer_lock:
old_buffers = self._buffers
self._buffers = defaultdict(StringIO)
if len(self._buffer_keys) > 8:
self._buffer_keys = {}
return old_buffers

@property
Expand Down
Loading