Skip to content

Commit

Permalink
Use ContextVar for parent messages
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Dec 9, 2021
1 parent bb1ad85 commit 22bd412
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions ipykernel/kernelbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import time
import uuid
import warnings
from contextvars import ContextVar

try:
# jupyter_client >= 5, use tz-aware now
Expand Down Expand Up @@ -134,7 +135,7 @@ def _default_ident(self):

# track associations with current request
_allow_stdin = Bool(False)
_parents = Dict({"shell": {}, "control": {}})
_parents = Dict({"shell": ContextVar("shell_parent", default={}), "control": ContextVar("control_parent", default={})})
_parent_ident = Dict({'shell': b'', 'control': b''})

@property
Expand Down Expand Up @@ -556,7 +557,7 @@ def set_parent(self, ident, parent, channel='shell'):
on the stdin channel.
"""
self._parent_ident[channel] = ident
self._parents[channel] = parent
self._parents[channel].set(parent)

def get_parent(self, channel="shell"):
"""Get the parent request associated with a channel.
Expand All @@ -573,7 +574,7 @@ def get_parent(self, channel="shell"):
message : dict
the parent message for the most recent request on the channel.
"""
return self._parents.get(channel, {})
return self._parents.get(channel, {}).get({})

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

0 comments on commit 22bd412

Please sign in to comment.