From 75dc5bebd937ba914283b9145a66a5a488723cb6 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Thu, 14 May 2020 11:06:54 -0500 Subject: [PATCH 1/5] Adding reset_kc to setup_kernel --- nbclient/client.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/nbclient/client.py b/nbclient/client.py index b021e133..cae79074 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -411,6 +411,8 @@ def setup_kernel(self, **kwargs): When control returns from the yield it stops the client's zmq channels, and shuts down the kernel. """ + reset_kc = kwargs.pop('reset_kc', False) + # Can't use run_until_complete on an asynccontextmanager function :( if self.km is None: self.start_kernel_manager() @@ -420,7 +422,8 @@ def setup_kernel(self, **kwargs): try: yield finally: - self._cleanup_kernel() + if reset_kc: + self._cleanup_kernel() @asynccontextmanager async def async_setup_kernel(self, **kwargs): From a1369f37b67bfccd68ef071b6c3e9e3b8a9cc385 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Thu, 14 May 2020 11:11:23 -0500 Subject: [PATCH 2/5] flake8 --- nbclient/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nbclient/client.py b/nbclient/client.py index cae79074..ae3e50c1 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -412,7 +412,7 @@ def setup_kernel(self, **kwargs): down the kernel. """ reset_kc = kwargs.pop('reset_kc', False) - + # Can't use run_until_complete on an asynccontextmanager function :( if self.km is None: self.start_kernel_manager() From 4494b780f2c995d399a54473070d19eb54ebb007 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Tue, 19 May 2020 12:43:14 -0500 Subject: [PATCH 3/5] Clean reset_kc and cleanup_kc --- nbclient/client.py | 16 ++++++++-------- nbclient/tests/test_client.py | 30 ++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/nbclient/client.py b/nbclient/client.py index ae3e50c1..7136a041 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -411,7 +411,7 @@ def setup_kernel(self, **kwargs): When control returns from the yield it stops the client's zmq channels, and shuts down the kernel. """ - reset_kc = kwargs.pop('reset_kc', False) + cleanup_kc = kwargs.pop('cleanup_kc', True) # Can't use run_until_complete on an asynccontextmanager function :( if self.km is None: @@ -422,7 +422,7 @@ def setup_kernel(self, **kwargs): try: yield finally: - if reset_kc: + if cleanup_kc: self._cleanup_kernel() @asynccontextmanager @@ -435,7 +435,7 @@ async def async_setup_kernel(self, **kwargs): When control returns from the yield it stops the client's zmq channels, and shuts down the kernel. """ - reset_kc = kwargs.pop('reset_kc', False) + cleanup_kc = kwargs.pop('cleanup_kc', True) if self.km is None: self.start_kernel_manager() @@ -444,7 +444,7 @@ async def async_setup_kernel(self, **kwargs): try: yield finally: - if reset_kc: + if cleanup_kc: await self._async_cleanup_kernel() async def async_execute(self, **kwargs): @@ -457,16 +457,16 @@ async def async_execute(self, **kwargs): Any option for `self.kernel_manager_class.start_kernel()`. Because that defaults to AsyncKernelManager, this will likely include options accepted by `AsyncKernelManager.start_kernel()``, which includes `cwd`. - If present, `reset_kc` is passed to `self.async_setup_kernel`: - if True, the kernel client will be reset and a new one will be created - and cleaned up after execution (default: False). + + `reset_kc` if True, the kernel client will be reset and a new one + will be created (default: False). Returns ------- nb : NotebookNode The executed notebook. """ - reset_kc = kwargs.get('reset_kc', False) + reset_kc = kwargs.pop('reset_kc', False) if reset_kc: await self._async_cleanup_kernel() self.reset_execution_trackers() diff --git a/nbclient/tests/test_client.py b/nbclient/tests/test_client.py index d95f864b..810befbb 100644 --- a/nbclient/tests/test_client.py +++ b/nbclient/tests/test_client.py @@ -593,18 +593,40 @@ def test_reset_kernel_client(self): resources=self.build_resources(), ) - executor.execute() + executor.execute(cleanup_kc=False) # we didn't ask to reset the kernel client, a new one must have been created kc = executor.kc assert kc is not None - executor.execute() + + executor.execute(cleanup_kc=False) # we didn't ask to reset the kernel client, the previously created one must have been reused assert kc == executor.kc - executor.execute(reset_kc=True) + + executor.execute(reset_kc=True, cleanup_kc=False) # we asked to reset the kernel client, the previous one must have been cleaned up, - # a new one must have been created and also cleaned up + # a new one must have been created + assert kc != executor.kc + + def test_cleanup_kernel_client(self): + filename = os.path.join(current_dir, 'files', 'HelloWorld.ipynb') + + with io.open(filename) as f: + input_nb = nbformat.read(f, 4) + + executor = NotebookClient( + input_nb, + resources=self.build_resources(), + ) + + executor.execute() + # we asked to cleanup the kernel client (default is True) assert executor.kc is None + executor.execute(cleanup_kc=False) + # we didn't ask to reset the kernel client + # a new one must have been created and should still be available + assert executor.kc is not None + def test_custom_kernel_manager(self): from .fake_kernelmanager import FakeCustomKernelManager From 80fe510c0f21c55f6cf12d4d98d305b59eea30d7 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Tue, 19 May 2020 12:45:06 -0500 Subject: [PATCH 4/5] Clean arguments --- nbclient/client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nbclient/client.py b/nbclient/client.py index 07d3ec25..4b7961f0 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -450,7 +450,7 @@ async def async_setup_kernel(self, **kwargs): if cleanup_kc: await self._async_cleanup_kernel() - async def async_execute(self, **kwargs): + async def async_execute(self, reset_kc, **kwargs): """ Executes each code cell. @@ -469,7 +469,6 @@ async def async_execute(self, **kwargs): nb : NotebookNode The executed notebook. """ - reset_kc = kwargs.pop('reset_kc', False) if reset_kc and self.km: await self._async_cleanup_kernel() self.reset_execution_trackers() From 7cfe9f437d67d01960ad48690d8ae33a5ae12576 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Tue, 19 May 2020 12:48:54 -0500 Subject: [PATCH 5/5] Fix arg --- nbclient/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nbclient/client.py b/nbclient/client.py index 4b7961f0..8420f074 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -450,7 +450,7 @@ async def async_setup_kernel(self, **kwargs): if cleanup_kc: await self._async_cleanup_kernel() - async def async_execute(self, reset_kc, **kwargs): + async def async_execute(self, reset_kc=False, **kwargs): """ Executes each code cell.