1414try :
1515 import ipykernel
1616
17- USAGE_IS_SUPPORTED = version .parse ("6.9.0" ) <= version .parse (ipykernel .__version__ )
17+ IPYKERNEL_VERSION = ipykernel .__version__
18+ USAGE_IS_SUPPORTED = version .parse ("6.9.0" ) <= version .parse (IPYKERNEL_VERSION )
1819except ImportError :
1920 USAGE_IS_SUPPORTED = False
21+ IPYKERNEL_VERSION = None
2022
2123
2224class ApiHandler (APIHandler ):
@@ -92,7 +94,12 @@ class KernelUsageHandler(APIHandler):
9294 @web .authenticated
9395 async def get (self , matched_part = None , * args , ** kwargs ):
9496 if not USAGE_IS_SUPPORTED :
95- self .write (json .dumps ({}))
97+ self .write (json .dumps ({
98+ "content" : {
99+ "reason" : "not_supported" ,
100+ "kernel_version" : IPYKERNEL_VERSION
101+ }
102+ }))
96103 return
97104
98105 kernel_id = matched_part
@@ -112,11 +119,16 @@ async def get(self, matched_part=None, *args, **kwargs):
112119 timeout_ms = 6_000
113120 events = dict (await poller .poll (timeout_ms ))
114121 if control_socket not in events :
115- self .write (json .dumps ({}))
122+ self .write (json .dumps ({
123+ "content" : {"reason" : "timeout" , "timeout_ms" : timeout_ms },
124+ "kernel_id" : kernel_id
125+ }))
116126 else :
117127 res = client .control_channel .get_msg (timeout = 0 )
118128 if isawaitable (res ):
119129 # control_channel.get_msg may return a Future,
120130 # depending on configured KernelManager class
121131 res = await res
132+ if res :
133+ res ["kernel_id" ] = kernel_id
122134 self .write (json .dumps (res , default = date_default ))
0 commit comments