Skip to content

Commit dc22d8e

Browse files
committed
Add test for kernel client reset
1 parent 75bc0e7 commit dc22d8e

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

nbclient/client.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,11 @@ def __init__(self, nb, km=None, **kw):
298298
super().__init__(**kw)
299299
self.nb = nb
300300
self.km = km
301-
self.reset_execution_trackers(reset_kc=True)
301+
self.reset_execution_trackers()
302302

303-
def reset_execution_trackers(self, reset_kc=False):
303+
def reset_execution_trackers(self):
304304
"""Resets any per-execution trackers.
305305
"""
306-
if reset_kc:
307-
self.kc = None
308306
self.code_cells_executed = 0
309307
self._display_id_map = {}
310308
self.widget_state = {}
@@ -418,6 +416,7 @@ async def async_setup_kernel(self, **kwargs):
418416
When control returns from the yield it stops the client's zmq channels, and shuts
419417
down the kernel.
420418
"""
419+
reset_kc = kwargs.pop('reset_kc', False)
421420
if self.km is None:
422421
self.start_kernel_manager()
423422

@@ -426,7 +425,8 @@ async def async_setup_kernel(self, **kwargs):
426425
try:
427426
yield
428427
finally:
429-
await self._async_cleanup_kernel()
428+
if reset_kc:
429+
await self._async_cleanup_kernel()
430430

431431
async def async_execute(self, **kwargs):
432432
"""
@@ -438,17 +438,19 @@ async def async_execute(self, **kwargs):
438438
Any option for `self.kernel_manager_class.start_kernel()`. Because
439439
that defaults to AsyncKernelManager, this will likely include options
440440
accepted by `AsyncKernelManager.start_kernel()``, which includes `cwd`.
441-
If present, `reset_kc` is passed to `self.reset_execution_trackers`:
441+
If present, `reset_kc` is passed to `self.async_setup_kernel`:
442442
if True, the kernel client will be reset and a new one will be created
443-
(default: False).
443+
and cleaned up after execution (default: False).
444444
445445
Returns
446446
-------
447447
nb : NotebookNode
448448
The executed notebook.
449449
"""
450-
reset_kc = kwargs.pop('reset_kc', False)
451-
self.reset_execution_trackers(reset_kc=reset_kc)
450+
reset_kc = kwargs.get('reset_kc', False)
451+
if reset_kc:
452+
await self._async_cleanup_kernel()
453+
self.reset_execution_trackers()
452454

453455
async with self.async_setup_kernel(**kwargs):
454456
self.log.info("Executing notebook with kernel: %s" % self.kernel_name)

nbclient/tests/test_client.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,29 @@ def test_force_raise_errors(self):
544544
else:
545545
assert u"# üñîçø∂é".encode('utf8', 'replace') in str(exc.value)
546546

547+
def test_reset_kernel_client(self):
548+
filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb')
549+
550+
with io.open(filename) as f:
551+
input_nb = nbformat.read(f, 4)
552+
553+
executor = NotebookClient(
554+
input_nb,
555+
resources=self.build_resources(),
556+
)
557+
558+
executor.execute()
559+
# we didn't ask to reset the kernel client, a new one must have been created
560+
kc = executor.kc
561+
self.assertNotEqual(kc, None)
562+
executor.execute()
563+
# we didn't ask to reset the kernel client, the previously created one must have been reused
564+
self.assertEqual(kc, executor.kc)
565+
executor.execute(reset_kc=True)
566+
# we asked to reset the kernel client, the previous one must have been cleaned up,
567+
# a new one must have been created and also cleaned up
568+
self.assertEqual(executor.kc, None)
569+
547570
def test_custom_kernel_manager(self):
548571
from .fake_kernelmanager import FakeCustomKernelManager
549572

0 commit comments

Comments
 (0)