From 98b2aa07a4c853a747fc327ca578e06753e43e6d Mon Sep 17 00:00:00 2001 From: sunxilin Date: Mon, 14 Oct 2024 19:20:04 +0800 Subject: [PATCH] fix: create a new Python thread to avoid blocking extension thread --- .../binding/python/interface/ten/async_ten_env.py | 15 ++++++++++++++- .../default_extension_python/extension.py | 2 ++ .../default_extension_python/extension.py | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py b/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py index 1c1b5a1750..059a3d89dd 100644 --- a/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py +++ b/core/src/ten_runtime/binding/python/interface/ten/async_ten_env.py @@ -19,7 +19,8 @@ def __init__( ) -> None: self._internal = ten_env._internal self._ten_loop = loop - ten_env._set_release_handler(lambda: thread.join()) + self._ten_thread = thread + ten_env._set_release_handler(lambda: self._on_release()) def __del__(self) -> None: pass @@ -43,3 +44,15 @@ async def send_json(self, json_str: str) -> CmdResult: ), # type: ignore ) return await q.get() + + def _deinit_routine(self) -> None: + self._ten_thread.join() + self._internal.on_deinit_done() + + def _on_release(self) -> None: + if hasattr(self, "_deinit_thread"): + self._deinit_thread.join() + + def on_deinit_done(self) -> None: + self._deinit_thread = threading.Thread(target=self._deinit_routine) + self._deinit_thread.start() diff --git a/tests/ten_runtime/integration/python/two_async_exts_one_group_python/two_async_exts_one_group_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/two_async_exts_one_group_python/two_async_exts_one_group_python_app/ten_packages/extension/default_extension_python/extension.py index ceeb8cb380..e09d939aa8 100644 --- a/tests/ten_runtime/integration/python/two_async_exts_one_group_python/two_async_exts_one_group_python_app/ten_packages/extension/default_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/two_async_exts_one_group_python/two_async_exts_one_group_python_app/ten_packages/extension/default_extension_python/extension.py @@ -34,6 +34,8 @@ async def on_deinit(self, ten_env: AsyncTenEnv) -> None: await asyncio.sleep(0.5) ten_env.on_deinit_done() + await asyncio.sleep(1) + async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None: cmd_json = cmd.to_json() ten_env.log_debug(f"DefaultExtension on_cmd: {cmd_json}") diff --git a/tests/ten_runtime/integration/python/two_async_exts_python/two_async_exts_python_app/ten_packages/extension/default_extension_python/extension.py b/tests/ten_runtime/integration/python/two_async_exts_python/two_async_exts_python_app/ten_packages/extension/default_extension_python/extension.py index ceeb8cb380..e09d939aa8 100644 --- a/tests/ten_runtime/integration/python/two_async_exts_python/two_async_exts_python_app/ten_packages/extension/default_extension_python/extension.py +++ b/tests/ten_runtime/integration/python/two_async_exts_python/two_async_exts_python_app/ten_packages/extension/default_extension_python/extension.py @@ -34,6 +34,8 @@ async def on_deinit(self, ten_env: AsyncTenEnv) -> None: await asyncio.sleep(0.5) ten_env.on_deinit_done() + await asyncio.sleep(1) + async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None: cmd_json = cmd.to_json() ten_env.log_debug(f"DefaultExtension on_cmd: {cmd_json}")