Skip to content

Commit 4494b78

Browse files
committed
Clean reset_kc and cleanup_kc
1 parent a1369f3 commit 4494b78

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

nbclient/client.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def setup_kernel(self, **kwargs):
411411
When control returns from the yield it stops the client's zmq channels, and shuts
412412
down the kernel.
413413
"""
414-
reset_kc = kwargs.pop('reset_kc', False)
414+
cleanup_kc = kwargs.pop('cleanup_kc', True)
415415

416416
# Can't use run_until_complete on an asynccontextmanager function :(
417417
if self.km is None:
@@ -422,7 +422,7 @@ def setup_kernel(self, **kwargs):
422422
try:
423423
yield
424424
finally:
425-
if reset_kc:
425+
if cleanup_kc:
426426
self._cleanup_kernel()
427427

428428
@asynccontextmanager
@@ -435,7 +435,7 @@ async def async_setup_kernel(self, **kwargs):
435435
When control returns from the yield it stops the client's zmq channels, and shuts
436436
down the kernel.
437437
"""
438-
reset_kc = kwargs.pop('reset_kc', False)
438+
cleanup_kc = kwargs.pop('cleanup_kc', True)
439439
if self.km is None:
440440
self.start_kernel_manager()
441441

@@ -444,7 +444,7 @@ async def async_setup_kernel(self, **kwargs):
444444
try:
445445
yield
446446
finally:
447-
if reset_kc:
447+
if cleanup_kc:
448448
await self._async_cleanup_kernel()
449449

450450
async def async_execute(self, **kwargs):
@@ -457,16 +457,16 @@ async def async_execute(self, **kwargs):
457457
Any option for `self.kernel_manager_class.start_kernel()`. Because
458458
that defaults to AsyncKernelManager, this will likely include options
459459
accepted by `AsyncKernelManager.start_kernel()``, which includes `cwd`.
460-
If present, `reset_kc` is passed to `self.async_setup_kernel`:
461-
if True, the kernel client will be reset and a new one will be created
462-
and cleaned up after execution (default: False).
460+
461+
`reset_kc` if True, the kernel client will be reset and a new one
462+
will be created (default: False).
463463
464464
Returns
465465
-------
466466
nb : NotebookNode
467467
The executed notebook.
468468
"""
469-
reset_kc = kwargs.get('reset_kc', False)
469+
reset_kc = kwargs.pop('reset_kc', False)
470470
if reset_kc:
471471
await self._async_cleanup_kernel()
472472
self.reset_execution_trackers()

nbclient/tests/test_client.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,18 +593,40 @@ def test_reset_kernel_client(self):
593593
resources=self.build_resources(),
594594
)
595595

596-
executor.execute()
596+
executor.execute(cleanup_kc=False)
597597
# we didn't ask to reset the kernel client, a new one must have been created
598598
kc = executor.kc
599599
assert kc is not None
600-
executor.execute()
600+
601+
executor.execute(cleanup_kc=False)
601602
# we didn't ask to reset the kernel client, the previously created one must have been reused
602603
assert kc == executor.kc
603-
executor.execute(reset_kc=True)
604+
605+
executor.execute(reset_kc=True, cleanup_kc=False)
604606
# we asked to reset the kernel client, the previous one must have been cleaned up,
605-
# a new one must have been created and also cleaned up
607+
# a new one must have been created
608+
assert kc != executor.kc
609+
610+
def test_cleanup_kernel_client(self):
611+
filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb')
612+
613+
with io.open(filename) as f:
614+
input_nb = nbformat.read(f, 4)
615+
616+
executor = NotebookClient(
617+
input_nb,
618+
resources=self.build_resources(),
619+
)
620+
621+
executor.execute()
622+
# we asked to cleanup the kernel client (default is True)
606623
assert executor.kc is None
607624

625+
executor.execute(cleanup_kc=False)
626+
# we didn't ask to reset the kernel client
627+
# a new one must have been created and should still be available
628+
assert executor.kc is not None
629+
608630
def test_custom_kernel_manager(self):
609631
from .fake_kernelmanager import FakeCustomKernelManager
610632

0 commit comments

Comments
 (0)