Skip to content

Commit

Permalink
fix: create a new Python thread to avoid blocking extension thread
Browse files Browse the repository at this point in the history
  • Loading branch information
sunxilin committed Oct 14, 2024
1 parent 411f22f commit 98b2aa0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down

0 comments on commit 98b2aa0

Please sign in to comment.