Skip to content

Commit a32d6ba

Browse files
authored
Merge pull request #53 from danielfrg/patch-1
Adding reset_kc to setup_kernel
2 parents 634dc8f + 7cfe9f4 commit a32d6ba

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

nbclient/client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ def setup_kernel(self, **kwargs):
414414
When control returns from the yield it stops the client's zmq channels, and shuts
415415
down the kernel.
416416
"""
417+
cleanup_kc = kwargs.pop('cleanup_kc', True)
418+
417419
# Can't use run_until_complete on an asynccontextmanager function :(
418420
if self.km is None:
419421
self.start_kernel_manager()
@@ -423,7 +425,8 @@ def setup_kernel(self, **kwargs):
423425
try:
424426
yield
425427
finally:
426-
self._cleanup_kernel()
428+
if cleanup_kc:
429+
self._cleanup_kernel()
427430

428431
@asynccontextmanager
429432
async def async_setup_kernel(self, **kwargs):
@@ -435,7 +438,7 @@ async def async_setup_kernel(self, **kwargs):
435438
When control returns from the yield it stops the client's zmq channels, and shuts
436439
down the kernel.
437440
"""
438-
reset_kc = kwargs.pop('reset_kc', False)
441+
cleanup_kc = kwargs.pop('cleanup_kc', True)
439442
if self.km is None:
440443
self.start_kernel_manager()
441444

@@ -444,10 +447,10 @@ async def async_setup_kernel(self, **kwargs):
444447
try:
445448
yield
446449
finally:
447-
if reset_kc:
450+
if cleanup_kc:
448451
await self._async_cleanup_kernel()
449452

450-
async def async_execute(self, **kwargs):
453+
async def async_execute(self, reset_kc=False, **kwargs):
451454
"""
452455
Executes each code cell.
453456
@@ -457,16 +460,15 @@ async def async_execute(self, **kwargs):
457460
Any option for `self.kernel_manager_class.start_kernel()`. Because
458461
that defaults to AsyncKernelManager, this will likely include options
459462
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).
463+
464+
`reset_kc` if True, the kernel client will be reset and a new one
465+
will be created (default: False).
463466
464467
Returns
465468
-------
466469
nb : NotebookNode
467470
The executed notebook.
468471
"""
469-
reset_kc = kwargs.get('reset_kc', False)
470472
if reset_kc and self.km:
471473
await self._async_cleanup_kernel()
472474
self.reset_execution_trackers()

nbclient/tests/test_client.py

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

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

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

0 commit comments

Comments
 (0)