Skip to content

Commit 22bd412

Browse files
committed
Use ContextVar for parent messages
1 parent bb1ad85 commit 22bd412

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

ipykernel/kernelbase.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import time
1717
import uuid
1818
import warnings
19+
from contextvars import ContextVar
1920

2021
try:
2122
# jupyter_client >= 5, use tz-aware now
@@ -134,7 +135,7 @@ def _default_ident(self):
134135

135136
# track associations with current request
136137
_allow_stdin = Bool(False)
137-
_parents = Dict({"shell": {}, "control": {}})
138+
_parents = Dict({"shell": ContextVar("shell_parent", default={}), "control": ContextVar("control_parent", default={})})
138139
_parent_ident = Dict({'shell': b'', 'control': b''})
139140

140141
@property
@@ -556,7 +557,7 @@ def set_parent(self, ident, parent, channel='shell'):
556557
on the stdin channel.
557558
"""
558559
self._parent_ident[channel] = ident
559-
self._parents[channel] = parent
560+
self._parents[channel].set(parent)
560561

561562
def get_parent(self, channel="shell"):
562563
"""Get the parent request associated with a channel.
@@ -573,7 +574,7 @@ def get_parent(self, channel="shell"):
573574
message : dict
574575
the parent message for the most recent request on the channel.
575576
"""
576-
return self._parents.get(channel, {})
577+
return self._parents.get(channel, {}).get({})
577578

578579
def send_response(self, stream, msg_or_type, content=None, ident=None,
579580
buffers=None, track=False, header=None, metadata=None, channel='shell'):

0 commit comments

Comments
 (0)