77import json
88import pathlib
99import sys
10- from contextvars import ContextVar
1110from typing import TYPE_CHECKING , Any , ClassVar , Literal
1211
1312import anyio
2221from typing_extensions import override
2322
2423import async_kernel
24+ from async_kernel import utils
2525from async_kernel .caller import Caller
2626from async_kernel .compiler import XCachingCompiler
2727from async_kernel .typing import Content , Tags
@@ -97,7 +97,7 @@ def publish( # pyright: ignore[reportIncompatibleMethodOverride]
9797
9898 [Reference](https://jupyter-client.readthedocs.io/en/stable/messaging.html#update-display-data)
9999 """
100- async_kernel . Kernel ().iopub_send (
100+ utils . get_kernel ().iopub_send (
101101 msg_or_type = "update_display_data" if update else "display_data" ,
102102 content = {"data" : data , "metadata" : metadata or {}, "transient" : transient or {}} | kwargs ,
103103 ident = self .topic ,
@@ -112,7 +112,7 @@ def clear_output(self, wait: bool = False) -> None:
112112 instead waiting for the next display before clearing.
113113 This reduces bounce during repeated clear & display loops.
114114 """
115- async_kernel . Kernel ().iopub_send (msg_or_type = "clear_output" , content = {"wait" : wait }, ident = self .topic )
115+ utils . get_kernel ().iopub_send (msg_or_type = "clear_output" , content = {"wait" : wait }, ident = self .topic )
116116
117117
118118class AsyncInteractiveShell (InteractiveShell ):
@@ -136,7 +136,7 @@ class AsyncInteractiveShell(InteractiveShell):
136136 compile : Instance [XCachingCompiler ]
137137 user_ns_hidden = Dict ()
138138 _main_mod_cache = Dict ()
139- _execute_request_timeout : ContextVar [ float | None ] = ContextVar ( "execute_request_timeout" , default = None )
139+
140140 run_cell = None # pyright: ignore[reportAssignmentType]
141141 "**not-supported**"
142142 should_run_async = None # pyright: ignore[reportAssignmentType]
@@ -162,21 +162,7 @@ def _default_banner1(self) -> str:
162162 @property
163163 def kernel (self ) -> Kernel :
164164 "The current kernel."
165- return async_kernel .Kernel ()
166-
167- @property
168- def execute_request_timeout (self ) -> float | None :
169- """A timeout in context of the [run_cell_async][async_kernel.asyncshell.AsyncInteractiveShell].
170-
171- See also:
172-
173- - [async_kernel.typing.MetadataKeys.timeout][].
174- """
175- return self ._execute_request_timeout .get ()
176-
177- @execute_request_timeout .setter
178- def execute_request_timeout (self , value : float | None ) -> None :
179- self ._execute_request_timeout .set (value )
165+ return utils .get_kernel ()
180166
181167 @observe ("exit_now" )
182168 def _update_exit_now (self , _ ) -> None :
@@ -250,7 +236,7 @@ async def run_cell_async(
250236 This function runs [execute requests][async_kernel.Kernel.execute_request] for the kernel
251237 wrapping [InteractiveShell][IPython.core.interactiveshell.InteractiveShell.run_cell_async].
252238 """
253- with anyio .fail_after (delay = self . execute_request_timeout ):
239+ with anyio .fail_after (delay = utils . get_execute_request_timeout () ):
254240 result : ExecutionResult = await super ().run_cell_async (
255241 raw_cell = raw_cell ,
256242 store_history = store_history ,
@@ -269,7 +255,7 @@ async def run_cell_async(
269255 def _showtraceback (self , etype , evalue , stb ) -> None :
270256 if Tags .suppress_error in async_kernel .utils .get_tags ():
271257 return
272- if self . execute_request_timeout is not None and etype is self .kernel .CancelledError :
258+ if utils . get_execute_request_timeout () is not None and etype is self .kernel .CancelledError :
273259 etype , evalue , stb = TimeoutError , "Cell execute timeout" , []
274260 self .kernel .iopub_send (
275261 msg_or_type = "error" ,
@@ -294,14 +280,11 @@ class KernelMagics(Magics):
294280 @line_magic
295281 def connect_info (self , _ ) -> None :
296282 """Print information for connecting other clients to this kernel."""
297-
298- kernel = async_kernel .Kernel ()
283+ kernel = utils .get_kernel ()
299284 connection_file = pathlib .Path (kernel .connection_file )
300-
301285 # if it's in the default dir, truncate to basename
302286 if jupyter_runtime_dir () == str (connection_file .parent ):
303287 connection_file = connection_file .name
304-
305288 info = kernel .get_connection_info ()
306289 print (
307290 json .dumps (info , indent = 2 , default = json_default ),
0 commit comments