-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathtest_multikernelmanager.py
646 lines (558 loc) · 22.6 KB
/
test_multikernelmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
"""Tests for the notebook kernel and session manager."""
import asyncio
import concurrent.futures
import os
import sys
import uuid
from asyncio import ensure_future
from subprocess import PIPE
from unittest import TestCase
import pytest
from jupyter_core import paths
from tornado.testing import AsyncTestCase, gen_test
from traitlets.config.loader import Config
from jupyter_client import AsyncKernelManager, KernelManager
from jupyter_client.localinterfaces import localhost
from jupyter_client.multikernelmanager import AsyncMultiKernelManager, MultiKernelManager
from .utils import (
AsyncKMSubclass,
AsyncMKMSubclass,
SyncKMSubclass,
SyncMKMSubclass,
install_kernel,
skip_win32,
)
TIMEOUT = 30
async def now(awaitable):
"""Use this function ensure that this awaitable
happens before other awaitables defined after it.
"""
(out,) = await asyncio.gather(awaitable)
return out
class TestKernelManager(TestCase):
# static so picklable for multiprocessing on Windows
@staticmethod
def _get_tcp_km():
c = Config()
km = MultiKernelManager(config=c)
return km
@staticmethod
def _get_tcp_km_sub():
c = Config()
km = SyncMKMSubclass(config=c)
return km
# static so picklable for multiprocessing on Windows
@staticmethod
def _get_ipc_km():
c = Config()
c.KernelManager.transport = "ipc"
c.KernelManager.ip = "test"
km = MultiKernelManager(config=c)
return km
# static so picklable for multiprocessing on Windows
@staticmethod
def _run_lifecycle(km, test_kid=None):
if test_kid:
kid = km.start_kernel(stdout=PIPE, stderr=PIPE, kernel_id=test_kid)
assert kid == test_kid
else:
kid = km.start_kernel(stdout=PIPE, stderr=PIPE)
assert km.is_alive(kid)
assert km.get_kernel(kid).ready.done()
assert kid in km
assert kid in km.list_kernel_ids()
assert len(km) == 1, f"{len(km)} != {1}"
km.restart_kernel(kid, now=True)
assert km.is_alive(kid)
assert kid in km.list_kernel_ids()
km.interrupt_kernel(kid)
k = km.get_kernel(kid)
kc = k.client()
assert isinstance(k, KernelManager)
km.shutdown_kernel(kid, now=True)
assert kid not in km, f"{kid} not in {km}"
kc.stop_channels()
def _run_cinfo(self, km, transport, ip):
kid = km.start_kernel(stdout=PIPE, stderr=PIPE)
km.get_kernel(kid)
cinfo = km.get_connection_info(kid)
self.assertEqual(transport, cinfo["transport"])
self.assertEqual(ip, cinfo["ip"])
self.assertTrue("stdin_port" in cinfo)
self.assertTrue("iopub_port" in cinfo)
stream = km.connect_iopub(kid)
stream.close()
self.assertTrue("shell_port" in cinfo)
stream = km.connect_shell(kid)
stream.close()
self.assertTrue("hb_port" in cinfo)
stream = km.connect_hb(kid)
stream.close()
km.shutdown_kernel(kid, now=True)
# static so picklable for multiprocessing on Windows
@classmethod
def test_tcp_lifecycle(cls):
km = cls._get_tcp_km()
cls._run_lifecycle(km)
def test_tcp_lifecycle_with_kernel_id(self):
km = self._get_tcp_km()
self._run_lifecycle(km, test_kid=str(uuid.uuid4()))
def test_shutdown_all(self):
km = self._get_tcp_km()
kid = km.start_kernel(stdout=PIPE, stderr=PIPE)
self.assertIn(kid, km)
km.shutdown_all()
self.assertNotIn(kid, km)
# shutdown again is okay, because we have no kernels
km.shutdown_all()
def test_tcp_cinfo(self):
km = self._get_tcp_km()
self._run_cinfo(km, "tcp", localhost())
@skip_win32
def test_ipc_lifecycle(self):
km = self._get_ipc_km()
self._run_lifecycle(km)
@skip_win32
def test_ipc_cinfo(self):
km = self._get_ipc_km()
self._run_cinfo(km, "ipc", "test")
def test_start_sequence_tcp_kernels(self):
"""Ensure that a sequence of kernel startups doesn't break anything."""
self._run_lifecycle(self._get_tcp_km())
self._run_lifecycle(self._get_tcp_km())
self._run_lifecycle(self._get_tcp_km())
@skip_win32
def test_start_sequence_ipc_kernels(self):
"""Ensure that a sequence of kernel startups doesn't break anything."""
self._run_lifecycle(self._get_ipc_km())
self._run_lifecycle(self._get_ipc_km())
self._run_lifecycle(self._get_ipc_km())
def tcp_lifecycle_with_loop(self):
# Ensure each thread has an event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.test_tcp_lifecycle()
loop.close()
def test_start_parallel_thread_kernels(self):
self.test_tcp_lifecycle()
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as thread_executor:
future1 = thread_executor.submit(self.tcp_lifecycle_with_loop)
future2 = thread_executor.submit(self.tcp_lifecycle_with_loop)
future1.result()
future2.result()
@pytest.mark.skipif(
(sys.platform == "darwin") and (sys.version_info >= (3, 6)) and (sys.version_info < (3, 8)),
reason='"Bad file descriptor" error',
)
@pytest.mark.skipif(
sys.platform == "linux",
reason="Kernel refuses to start in process pool",
)
def test_start_parallel_process_kernels(self):
self.test_tcp_lifecycle()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as thread_executor:
future1 = thread_executor.submit(self.tcp_lifecycle_with_loop)
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as process_executor:
# Windows tests needs this target to be picklable:
future2 = process_executor.submit(self.test_tcp_lifecycle)
future2.result()
future1.result()
def test_subclass_callables(self):
km = self._get_tcp_km_sub()
km.reset_counts()
kid = km.start_kernel(stdout=PIPE, stderr=PIPE)
assert km.call_count("start_kernel") == 1
assert isinstance(km.get_kernel(kid), SyncKMSubclass)
assert km.get_kernel(kid).call_count("start_kernel") == 1
assert km.get_kernel(kid).call_count("_async_launch_kernel") == 1
assert km.is_alive(kid)
assert kid in km
assert kid in km.list_kernel_ids()
assert len(km) == 1, f"{len(km)} != {1}"
km.get_kernel(kid).reset_counts()
km.reset_counts()
km.restart_kernel(kid, now=True)
assert km.call_count("restart_kernel") == 1
assert km.call_count("get_kernel") == 1
assert km.get_kernel(kid).call_count("restart_kernel") == 1
assert km.get_kernel(kid).call_count("_async_shutdown_kernel") == 1
assert km.get_kernel(kid).call_count("_async_interrupt_kernel") == 1
assert km.get_kernel(kid).call_count("_async_kill_kernel") == 1
assert km.get_kernel(kid).call_count("_async_cleanup_resources") == 1
assert km.get_kernel(kid).call_count("_async_launch_kernel") == 1
assert km.is_alive(kid)
assert kid in km.list_kernel_ids()
km.get_kernel(kid).reset_counts()
km.reset_counts()
km.interrupt_kernel(kid)
assert km.call_count("interrupt_kernel") == 1
assert km.call_count("get_kernel") == 1
assert km.get_kernel(kid).call_count("interrupt_kernel") == 1
km.get_kernel(kid).reset_counts()
km.reset_counts()
k = km.get_kernel(kid)
assert isinstance(k, SyncKMSubclass)
assert km.call_count("get_kernel") == 1
km.get_kernel(kid).reset_counts()
km.reset_counts()
km.shutdown_all(now=True)
assert km.call_count("remove_kernel") == 1
assert km.call_count("request_shutdown") == 0
assert km.call_count("finish_shutdown") == 0
assert km.call_count("cleanup_resources") == 0
assert kid not in km, f"{kid} not in {km}"
def test_stream_on_recv(self):
mkm = self._get_tcp_km()
kid = mkm.start_kernel(stdout=PIPE, stderr=PIPE)
stream = mkm.connect_iopub(kid)
km = mkm.get_kernel(kid)
client = km.client()
session = km.session
called = False
def record_activity(msg_list):
nonlocal called
"""Record an IOPub message arriving from a kernel"""
idents, fed_msg_list = session.feed_identities(msg_list)
msg = session.deserialize(fed_msg_list, content=False)
msg_type = msg["header"]["msg_type"]
stream.send(msg)
called = True
stream.on_recv(record_activity)
while True:
client.kernel_info()
import time
time.sleep(0.1)
if called:
break
client.stop_channels()
km.shutdown_kernel(now=True)
class TestAsyncKernelManager(AsyncTestCase):
# static so picklable for multiprocessing on Windows
@staticmethod
def _get_tcp_km():
c = Config()
km = AsyncMultiKernelManager(config=c)
return km
@staticmethod
def _get_tcp_km_sub():
c = Config()
km = AsyncMKMSubclass(config=c)
return km
# static so picklable for multiprocessing on Windows
@staticmethod
def _get_ipc_km():
c = Config()
c.KernelManager.transport = "ipc"
c.KernelManager.ip = "test"
km = AsyncMultiKernelManager(config=c)
return km
@staticmethod
def _get_pending_kernels_km():
c = Config()
c.AsyncMultiKernelManager.use_pending_kernels = True
km = AsyncMultiKernelManager(config=c)
return km
# static so picklable for multiprocessing on Windows
@staticmethod
async def _run_lifecycle(km, test_kid=None):
if test_kid:
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE, kernel_id=test_kid)
assert kid == test_kid
else:
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE)
assert await km.is_alive(kid)
assert kid in km
assert kid in km.list_kernel_ids()
assert len(km) == 1, f"{len(km)} != {1}"
# Ensure we can interrupt during a restart.
fut = km.restart_kernel(kid, now=True)
await km.interrupt_kernel(kid)
assert await km.is_alive(kid)
await fut
assert kid in km.list_kernel_ids()
k = km.get_kernel(kid)
assert isinstance(k, AsyncKernelManager)
await km.shutdown_kernel(kid, now=True)
assert kid not in km, f"{kid} not in {km}"
async def _run_cinfo(self, km, transport, ip):
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE)
km.get_kernel(kid)
cinfo = km.get_connection_info(kid)
self.assertEqual(transport, cinfo["transport"])
self.assertEqual(ip, cinfo["ip"])
self.assertTrue("stdin_port" in cinfo)
self.assertTrue("iopub_port" in cinfo)
stream = km.connect_iopub(kid)
stream.close()
self.assertTrue("shell_port" in cinfo)
stream = km.connect_shell(kid)
stream.close()
self.assertTrue("hb_port" in cinfo)
stream = km.connect_hb(kid)
stream.close()
await km.shutdown_kernel(kid, now=True)
self.assertNotIn(kid, km)
@gen_test
async def test_tcp_lifecycle(self):
await self.raw_tcp_lifecycle()
@gen_test
async def test_tcp_lifecycle_with_kernel_id(self):
await self.raw_tcp_lifecycle(test_kid=str(uuid.uuid4()))
@gen_test
async def test_shutdown_all(self):
km = self._get_tcp_km()
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE)
self.assertIn(kid, km)
await km.shutdown_all()
self.assertNotIn(kid, km)
# shutdown again is okay, because we have no kernels
await km.shutdown_all()
@gen_test(timeout=20)
async def test_use_after_shutdown_all(self):
km = self._get_tcp_km()
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE)
self.assertIn(kid, km)
await km.shutdown_all()
self.assertNotIn(kid, km)
# Start another kernel
kid = await km.start_kernel(stdout=PIPE, stderr=PIPE)
self.assertIn(kid, km)
await km.shutdown_all()
self.assertNotIn(kid, km)
# shutdown again is okay, because we have no kernels
await km.shutdown_all()
@gen_test(timeout=20)
async def test_shutdown_all_while_starting(self):
km = self._get_tcp_km()
kid_future = asyncio.ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
# This is relying on the ordering of the asyncio queue, not sure if guaranteed or not:
kid, _ = await asyncio.gather(kid_future, km.shutdown_all())
self.assertNotIn(kid, km)
# Start another kernel
kid = await ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
self.assertIn(kid, km)
self.assertEqual(len(km), 1)
await km.shutdown_all()
self.assertNotIn(kid, km)
# shutdown again is okay, because we have no kernels
await km.shutdown_all()
@gen_test
async def test_use_pending_kernels(self):
km = self._get_pending_kernels_km()
kid = await ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
kernel = km.get_kernel(kid)
assert not kernel.ready.done()
assert kid in km
assert kid in km.list_kernel_ids()
assert len(km) == 1, f"{len(km)} != {1}"
# Wait for the kernel to start.
await kernel.ready
await km.restart_kernel(kid, now=True)
out = await km.is_alive(kid)
assert out
assert kid in km.list_kernel_ids()
await km.interrupt_kernel(kid)
k = km.get_kernel(kid)
assert isinstance(k, AsyncKernelManager)
await ensure_future(km.shutdown_kernel(kid, now=True))
# Wait for the kernel to shutdown
await kernel.ready
assert kid not in km, f"{kid} not in {km}"
@gen_test
async def test_use_pending_kernels_early_restart(self):
km = self._get_pending_kernels_km()
kid = await ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
kernel = km.get_kernel(kid)
assert not kernel.ready.done()
with pytest.raises(RuntimeError):
await km.restart_kernel(kid, now=True)
await kernel.ready
await ensure_future(km.shutdown_kernel(kid, now=True))
# Wait for the kernel to shutdown
await kernel.ready
assert kid not in km, f"{kid} not in {km}"
@gen_test
async def test_use_pending_kernels_early_shutdown(self):
km = self._get_pending_kernels_km()
kid = await ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
kernel = km.get_kernel(kid)
assert not kernel.ready.done()
# Try shutting down while the kernel is pending
await ensure_future(km.shutdown_kernel(kid, now=True))
# Wait for the kernel to shutdown
await kernel.ready
assert kid not in km, f"{kid} not in {km}"
@gen_test
async def test_use_pending_kernels_early_interrupt(self):
km = self._get_pending_kernels_km()
kid = await ensure_future(km.start_kernel(stdout=PIPE, stderr=PIPE))
kernel = km.get_kernel(kid)
assert not kernel.ready.done()
with pytest.raises(RuntimeError):
await km.interrupt_kernel(kid)
# Now wait for the kernel to be ready.
await kernel.ready
await ensure_future(km.shutdown_kernel(kid, now=True))
# Wait for the kernel to shutdown
await kernel.ready
assert kid not in km, f"{kid} not in {km}"
@gen_test
async def test_tcp_cinfo(self):
km = self._get_tcp_km()
await self._run_cinfo(km, "tcp", localhost())
@skip_win32
@gen_test
async def test_ipc_lifecycle(self):
km = self._get_ipc_km()
await self._run_lifecycle(km)
@skip_win32
@gen_test
async def test_ipc_cinfo(self):
km = self._get_ipc_km()
await self._run_cinfo(km, "ipc", "test")
@gen_test
async def test_start_sequence_tcp_kernels(self):
"""Ensure that a sequence of kernel startups doesn't break anything."""
await self._run_lifecycle(self._get_tcp_km())
await self._run_lifecycle(self._get_tcp_km())
await self._run_lifecycle(self._get_tcp_km())
@skip_win32
@gen_test
async def test_start_sequence_ipc_kernels(self):
"""Ensure that a sequence of kernel startups doesn't break anything."""
await self._run_lifecycle(self._get_ipc_km())
await self._run_lifecycle(self._get_ipc_km())
await self._run_lifecycle(self._get_ipc_km())
def tcp_lifecycle_with_loop(self):
# Ensure each thread has an event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.raw_tcp_lifecycle())
loop.close()
# static so picklable for multiprocessing on Windows
@classmethod
async def raw_tcp_lifecycle(cls, test_kid=None):
# Since @gen_test creates an event loop, we need a raw form of
# test_tcp_lifecycle that assumes the loop already exists.
km = cls._get_tcp_km()
await cls._run_lifecycle(km, test_kid=test_kid)
# static so picklable for multiprocessing on Windows
@classmethod
def raw_tcp_lifecycle_sync(cls, test_kid=None):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(cls.raw_tcp_lifecycle(test_kid=test_kid))
loop.close()
@gen_test
async def test_start_parallel_thread_kernels(self):
await self.raw_tcp_lifecycle()
with concurrent.futures.ThreadPoolExecutor(max_workers=2) as thread_executor:
future1 = thread_executor.submit(self.tcp_lifecycle_with_loop)
future2 = thread_executor.submit(self.tcp_lifecycle_with_loop)
future1.result()
future2.result()
@gen_test
async def test_start_parallel_process_kernels(self):
await self.raw_tcp_lifecycle()
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as thread_executor:
future1 = thread_executor.submit(self.tcp_lifecycle_with_loop)
with concurrent.futures.ProcessPoolExecutor(max_workers=1) as process_executor:
# Windows tests needs this target to be picklable:
future2 = process_executor.submit(self.raw_tcp_lifecycle_sync)
future2.result()
future1.result()
@gen_test
async def test_subclass_callables(self):
mkm = self._get_tcp_km_sub()
mkm.reset_counts()
kid = await mkm.start_kernel(stdout=PIPE, stderr=PIPE)
assert mkm.call_count("start_kernel") == 1
assert isinstance(mkm.get_kernel(kid), AsyncKMSubclass)
assert mkm.get_kernel(kid).call_count("start_kernel") == 1
assert mkm.get_kernel(kid).call_count("_async_launch_kernel") == 1
assert await mkm.is_alive(kid)
assert kid in mkm
assert kid in mkm.list_kernel_ids()
assert len(mkm) == 1, f"{len(mkm)} != {1}"
mkm.get_kernel(kid).reset_counts()
mkm.reset_counts()
await mkm.restart_kernel(kid, now=True)
assert mkm.call_count("restart_kernel") == 1
assert mkm.call_count("get_kernel") == 1
assert mkm.get_kernel(kid).call_count("restart_kernel") == 1
assert mkm.get_kernel(kid).call_count("_async_interrupt_kernel") == 1
assert mkm.get_kernel(kid).call_count("_async_kill_kernel") == 1
assert mkm.get_kernel(kid).call_count("_async_cleanup_resources") == 1
assert mkm.get_kernel(kid).call_count("_async_launch_kernel") == 1
assert await mkm.is_alive(kid)
assert kid in mkm.list_kernel_ids()
mkm.get_kernel(kid).reset_counts()
mkm.reset_counts()
await mkm.interrupt_kernel(kid)
assert mkm.call_count("interrupt_kernel") == 1
assert mkm.call_count("get_kernel") == 1
assert mkm.get_kernel(kid).call_count("interrupt_kernel") == 1
mkm.get_kernel(kid).reset_counts()
mkm.reset_counts()
k = mkm.get_kernel(kid)
assert isinstance(k, AsyncKMSubclass)
assert mkm.call_count("get_kernel") == 1
mkm.get_kernel(kid).reset_counts()
mkm.reset_counts()
await mkm.shutdown_all(now=True)
assert mkm.call_count("remove_kernel") == 1
assert mkm.call_count("_async_request_shutdown") == 0
assert mkm.call_count("_async_finish_shutdown") == 0
assert mkm.call_count("_async_cleanup_resources") == 0
assert kid not in mkm, f"{kid} not in {mkm}"
@gen_test
async def test_bad_kernelspec(self):
km = self._get_tcp_km()
install_kernel(
os.path.join(paths.jupyter_data_dir(), "kernels"),
argv=["non_existent_executable"],
name="bad",
)
with pytest.raises(FileNotFoundError):
await ensure_future(km.start_kernel(kernel_name="bad", stdout=PIPE, stderr=PIPE))
@gen_test
async def test_bad_kernelspec_pending(self):
km = self._get_pending_kernels_km()
install_kernel(
os.path.join(paths.jupyter_data_dir(), "kernels"),
argv=["non_existent_executable"],
name="bad",
)
kernel_id = await ensure_future(
km.start_kernel(kernel_name="bad", stdout=PIPE, stderr=PIPE)
)
with pytest.raises(FileNotFoundError):
await km.get_kernel(kernel_id).ready
assert kernel_id in km.list_kernel_ids()
await ensure_future(km.shutdown_kernel(kernel_id))
assert kernel_id not in km.list_kernel_ids()
@gen_test
async def test_stream_on_recv(self):
mkm = self._get_tcp_km()
kid = await mkm.start_kernel(stdout=PIPE, stderr=PIPE)
stream = mkm.connect_iopub(kid)
km = mkm.get_kernel(kid)
client = km.client()
session = km.session
called = False
def record_activity(msg_list):
nonlocal called
"""Record an IOPub message arriving from a kernel"""
idents, fed_msg_list = session.feed_identities(msg_list)
msg = session.deserialize(fed_msg_list, content=False)
msg_type = msg["header"]["msg_type"]
stream.send(msg)
called = True
stream.on_recv(record_activity)
while True:
await client.kernel_info(reply=True)
if called:
break
await asyncio.sleep(0.1)
client.stop_channels()
await km.shutdown_kernel(now=True)