Merge pull request #977 from orange-crow/fix_ipykernel_error

Fix ipykernel error in _plan_and_act of Interpreter.
This commit is contained in:
garylin2099 2024-03-11 11:29:52 +08:00 committed by GitHub
commit 543f519deb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 1 deletions

View file

@ -58,7 +58,8 @@ class ExecuteNbCode(Action):
async def terminate(self):
"""kill NotebookClient"""
await self.nb_client._async_cleanup_kernel()
if self.nb_client.km is not None:
await self.nb_client._async_cleanup_kernel()
async def reset(self):
"""reset NotebookClient"""

View file

@ -42,6 +42,10 @@ class Interpreter(Role):
def working_memory(self):
return self.rc.working_memory
async def _plan_and_act(self) -> Message:
await super()._plan_and_act()
await self.execute_code.terminate()
async def _act_on_task(self, current_task: Task) -> TaskResult:
code, result, is_success = await self._write_and_exec_code()
task_result = TaskResult(code=code, result=result, is_success=is_success)

View file

@ -104,6 +104,15 @@ async def test_terminate():
time.sleep(2)
assert executor.nb_client.km is None
for _ in range(200):
executor = ExecuteNbCode()
await executor.run(code='print("This is a code!")', language="python")
is_kernel_alive = await executor.nb_client.km.is_alive()
assert is_kernel_alive
await executor.terminate()
assert executor.nb_client.km is None
assert executor.nb_client.kc is None
await executor.terminate()
@pytest.mark.asyncio