Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: auto on_xxx_done in Python async extension #239

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ async def _thread_routine(self, ten_env: TenEnv):
)

await self.on_configure(self._async_ten_env)
ten_env.on_configure_done()

# Suspend the thread until stopEvent is set.
await self._ten_stop_event.wait()

await self.on_deinit(self._async_ten_env)
self._async_ten_env._deinit()

async def _stop_thread(self):
self._ten_stop_event.set()
Expand All @@ -57,21 +59,36 @@ def _proxy_on_configure(self, ten_env: TenEnv) -> None:
@final
def _proxy_on_init(self, ten_env: TenEnv) -> None:
asyncio.run_coroutine_threadsafe(
self.on_init(self._async_ten_env), self._ten_loop
self._proxy_async_on_init(ten_env), self._ten_loop
)

@final
async def _proxy_async_on_init(self, ten_env: TenEnv):
await self.on_init(self._async_ten_env)
ten_env.on_init_done()

@final
def _proxy_on_start(self, ten_env: TenEnv) -> None:
asyncio.run_coroutine_threadsafe(
self.on_start(self._async_ten_env), self._ten_loop
self._proxy_async_on_start(ten_env), self._ten_loop
)

@final
async def _proxy_async_on_start(self, ten_env: TenEnv):
await self.on_start(self._async_ten_env)
ten_env.on_start_done()

@final
def _proxy_on_stop(self, ten_env: TenEnv) -> None:
asyncio.run_coroutine_threadsafe(
self.on_stop(self._async_ten_env), self._ten_loop
self._proxy_async_on_stop(ten_env), self._ten_loop
)

@final
async def _proxy_async_on_stop(self, ten_env: TenEnv):
await self.on_stop(self._async_ten_env)
ten_env.on_stop_done()

@final
def _proxy_on_deinit(self, ten_env: TenEnv) -> None:
asyncio.run_coroutine_threadsafe(self._stop_thread(), self._ten_loop)
Expand Down Expand Up @@ -109,19 +126,19 @@ def _proxy_on_audio_frame(
# Override these methods in your extension

async def on_configure(self, async_ten_env: AsyncTenEnv) -> None:
async_ten_env.on_configure_done()
pass

async def on_init(self, async_ten_env: AsyncTenEnv) -> None:
async_ten_env.on_init_done()
pass

async def on_start(self, async_ten_env: AsyncTenEnv) -> None:
async_ten_env.on_start_done()
pass

async def on_stop(self, async_ten_env: AsyncTenEnv) -> None:
async_ten_env.on_stop_done()
pass

async def on_deinit(self, async_ten_env: AsyncTenEnv) -> None:
async_ten_env.on_deinit_done()
pass

async def on_cmd(self, async_ten_env: AsyncTenEnv, cmd: Cmd) -> None:
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,32 @@ def _on_release(self) -> None:
if hasattr(self, "_deinit_thread"):
self._deinit_thread.join()

def on_deinit_done(self) -> None:
def _deinit(self) -> None:
# Start the deinit thread to avoid blocking the extension thread.
self._deinit_thread = threading.Thread(target=self._deinit_routine)
self._deinit_thread.start()

def on_configure_done(self) -> None:
raise NotImplementedError(
"No need to call this method in async extension"
)

def on_init_done(self) -> None:
raise NotImplementedError(
"No need to call this method in async extension"
)

def on_start_done(self) -> None:
raise NotImplementedError(
"No need to call this method in async extension"
)

def on_stop_done(self) -> None:
raise NotImplementedError(
"No need to call this method in async extension"
)

def on_deinit_done(self) -> None:
raise NotImplementedError(
"No need to call this method in async extension"
)
8 changes: 0 additions & 8 deletions docs/ten_framework/ten_packages/python_async_extension.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,18 @@ class DefaultAsyncExtension(AsyncExtension):
# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

ten_env.on_configure_done()

async def on_init(self, ten_env: AsyncTenEnv) -> None:
# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

ten_env.on_deinit_done()

async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
cmd_json = cmd.to_json()
ten_env.log_debug(f"DefaultAsyncExtension on_cmd: {cmd_json}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,19 @@
class DefaultAsyncExtension(AsyncExtension):
async def on_init(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_init")
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_start")

# TODO: read properties, initialize resources

ten_env.on_start_done()

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

# TODO: clean up resources

ten_env.on_stop_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_deinit")
ten_env.on_deinit_done()

async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
cmd_name = cmd.get_name()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@ from ten import (
class {{class_name_prefix}}Extension(AsyncExtension):
async def on_init(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_init")
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_start")

# TODO: read properties, initialize resources

ten_env.on_start_done()

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

# TODO: clean up resources

ten_env.on_stop_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_deinit")
ten_env.on_deinit_done()
Expand Down
5 changes: 0 additions & 5 deletions packages/example_extensions/aio_http_server_python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def __init__(self, name: str) -> None:

async def on_init(self, ten_env: AsyncTenEnv) -> None:
self.ten_env = ten_env
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_start")
Expand All @@ -121,11 +120,8 @@ async def on_start(self, ten_env: AsyncTenEnv) -> None:

await self.start_server("0.0.0.0", self.server_port)

ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_deinit")
ten_env.on_deinit_done()

async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
ten_env.log_debug("on_cmd")
Expand All @@ -135,7 +131,6 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")
ten_env.on_stop_done()


@register_addon_as_extension("aio_http_server_python")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ def __init__(self, name: str) -> None:

async def on_configure(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_configure_done()

async def on_init(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
Expand All @@ -34,11 +32,8 @@ async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.set_property_string("string_field", "hello")
assert ten_env.is_property_exist("string_field") is True

ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_deinit_done()

async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
cmd_json = cmd.to_json()
Expand All @@ -60,4 +55,3 @@ async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

await asyncio.sleep(0.5)
ten_env.on_stop_done()
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ async def on_configure(self, ten_env: AsyncTenEnv) -> None:

await asyncio.sleep(0.5)

ten_env.on_configure_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_start")

Expand All @@ -39,20 +37,15 @@ async def on_start(self, ten_env: AsyncTenEnv) -> None:
testValue2 = ten_env.get_property_to_json("testKey2")
ten_env.log_info(f"testValue: {testValue}, testValue2: {testValue2}")

ten_env.on_start_done()

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

await asyncio.sleep(0.5)

ten_env.on_stop_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_deinit")

await asyncio.sleep(0.5)
ten_env.on_deinit_done()

async def greeting(self, ten_env: AsyncTenEnv) -> CmdResult:
await asyncio.sleep(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ def __init__(self, name: str) -> None:

async def on_configure(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_configure_done()

async def on_init(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.log_debug("on_start")
ten_env.on_start_done()

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:
Expand All @@ -53,4 +48,3 @@ async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

await asyncio.sleep(0.5)
ten_env.on_stop_done()
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ def __init__(self, name: str) -> None:

async def on_configure(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_configure_done()

async def on_init(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_init_done()

async def on_start(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.log_debug("on_start")
ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
await asyncio.sleep(0.5)
ten_env.on_deinit_done()

await asyncio.sleep(1)

Expand All @@ -53,4 +49,3 @@ async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("on_stop")

await asyncio.sleep(0.5)
ten_env.on_stop_done()
Loading