Skip to content

Commit cd85dea

Browse files
authored
Merge pull request #44 from davidbrochart/reset_kc
Add reset_kc option to reset_execution_trackers
2 parents 827cbf0 + 7d3a1c3 commit cd85dea

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

nbclient/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ def __init__(self, nb, km=None, **kw):
303303
def reset_execution_trackers(self):
304304
"""Resets any per-execution trackers.
305305
"""
306-
self.kc = None
307306
self.code_cells_executed = 0
308307
self._display_id_map = {}
309308
self.widget_state = {}
@@ -412,11 +411,12 @@ async def async_setup_kernel(self, **kwargs):
412411
"""
413412
Context manager for setting up the kernel to execute a notebook.
414413
415-
The assigns the Kernel Manager (`self.km`) if missing and Kernel Client(`self.kc`).
414+
This assigns the Kernel Manager (`self.km`) if missing and Kernel Client(`self.kc`).
416415
417416
When control returns from the yield it stops the client's zmq channels, and shuts
418417
down the kernel.
419418
"""
419+
reset_kc = kwargs.pop('reset_kc', False)
420420
if self.km is None:
421421
self.start_kernel_manager()
422422

@@ -425,17 +425,31 @@ async def async_setup_kernel(self, **kwargs):
425425
try:
426426
yield
427427
finally:
428-
await self._async_cleanup_kernel()
428+
if reset_kc:
429+
await self._async_cleanup_kernel()
429430

430431
async def async_execute(self, **kwargs):
431432
"""
432433
Executes each code cell.
433434
435+
Parameters
436+
----------
437+
kwargs :
438+
Any option for `self.kernel_manager_class.start_kernel()`. Because
439+
that defaults to AsyncKernelManager, this will likely include options
440+
accepted by `AsyncKernelManager.start_kernel()``, which includes `cwd`.
441+
If present, `reset_kc` is passed to `self.async_setup_kernel`:
442+
if True, the kernel client will be reset and a new one will be created
443+
and cleaned up after execution (default: False).
444+
434445
Returns
435446
-------
436447
nb : NotebookNode
437448
The executed notebook.
438449
"""
450+
reset_kc = kwargs.get('reset_kc', False)
451+
if reset_kc:
452+
await self._async_cleanup_kernel()
439453
self.reset_execution_trackers()
440454

441455
async with self.async_setup_kernel(**kwargs):

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+
assert kc is not None
562+
executor.execute()
563+
# we didn't ask to reset the kernel client, the previously created one must have been reused
564+
assert 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+
assert executor.kc is None
569+
547570
def test_custom_kernel_manager(self):
548571
from .fake_kernelmanager import FakeCustomKernelManager
549572

0 commit comments

Comments
 (0)