@@ -303,7 +303,6 @@ def __init__(self, nb, km=None, **kw):
303
303
def reset_execution_trackers (self ):
304
304
"""Resets any per-execution trackers.
305
305
"""
306
- self .kc = None
307
306
self .code_cells_executed = 0
308
307
self ._display_id_map = {}
309
308
self .widget_state = {}
@@ -412,11 +411,12 @@ async def async_setup_kernel(self, **kwargs):
412
411
"""
413
412
Context manager for setting up the kernel to execute a notebook.
414
413
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`).
416
415
417
416
When control returns from the yield it stops the client's zmq channels, and shuts
418
417
down the kernel.
419
418
"""
419
+ reset_kc = kwargs .pop ('reset_kc' , False )
420
420
if self .km is None :
421
421
self .start_kernel_manager ()
422
422
@@ -425,17 +425,31 @@ async def async_setup_kernel(self, **kwargs):
425
425
try :
426
426
yield
427
427
finally :
428
- await self ._async_cleanup_kernel ()
428
+ if reset_kc :
429
+ await self ._async_cleanup_kernel ()
429
430
430
431
async def async_execute (self , ** kwargs ):
431
432
"""
432
433
Executes each code cell.
433
434
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
+
434
445
Returns
435
446
-------
436
447
nb : NotebookNode
437
448
The executed notebook.
438
449
"""
450
+ reset_kc = kwargs .get ('reset_kc' , False )
451
+ if reset_kc :
452
+ await self ._async_cleanup_kernel ()
439
453
self .reset_execution_trackers ()
440
454
441
455
async with self .async_setup_kernel (** kwargs ):
0 commit comments