Skip to content

Commit

Permalink
fix: refine codes
Browse files Browse the repository at this point in the history
  • Loading branch information
halajohn committed Oct 14, 2024
1 parent 7781324 commit ce02a9d
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* [Overview](ten_framework/ten_packages/overview.md)
* [App](ten_framework/ten_packages/app.md)
* [Extension](ten_framework/ten_packages/extension.md)
* [Python Async Extension](ten_framework/ten_packages/python_async_extension.md)

### TEN Manager

Expand Down
56 changes: 56 additions & 0 deletions docs/ten_framework/ten_packages/python_async_extension.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Python Async Extension

A **Python async extension** is a modular component that using Python's `asyncio` framework. If you want to wrap existing Python codes that use `asyncio` into a TEN extension, using the Python async extension is the simplest and most convenient option.

## Example: The Default Python Async Extension

Here’s how a Python async extension is structured:

```python
import asyncio
from ten import AsyncExtension, AsyncTenEnv

class DefaultAsyncExtension(AsyncExtension):
async def on_configure(self, ten_env: AsyncTenEnv) -> None:
# 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}")

# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)

# Send a new command to other extensions and wait for the result. The
# result will be returned to the original sender.
new_cmd = Cmd.create("hello")
cmd_result = await ten_env.send_cmd(new_cmd)
ten_env.return_result(cmd_result, cmd)
```

Each method simulates a delay using `await asyncio.sleep()`.

## Conclusion

TEN's Python async extension provide a powerful way to handle long-running tasks asynchronously. By integrating Python’s `asyncio` framework, the extensions ensure that operations such as network calls or file handling are efficient and non-blocking. This makes TEN a great choice for building scalable, modular applications with asynchronous capabilities.
8 changes: 4 additions & 4 deletions packages/example_extensions/aio_http_server_python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async def on_init(self, ten_env: AsyncTenEnv) -> None:
ten_env.on_init_done()

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

try:
self.server_port = ten_env.get_property_int("server_port")
Expand All @@ -124,17 +124,17 @@ async def on_start(self, ten_env: AsyncTenEnv) -> None:
ten_env.on_start_done()

async def on_deinit(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("HttpServerExtension on_deinit")
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("HttpServerExtension on_cmd")
ten_env.log_debug("on_cmd")

# Not supported command.
ten_env.return_result(CmdResult.create(StatusCode.ERROR), cmd)

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def on_deinit(self, ten_env: AsyncTenEnv) -> None:

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}")
ten_env.log_debug(f"on_cmd: {cmd_json}")

# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)
Expand All @@ -48,7 +48,7 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
ten_env.return_result(cmd_result, cmd)

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("DefaultExtension on_stop")
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 @@ -38,7 +38,7 @@ async def on_deinit(self, ten_env: AsyncTenEnv) -> None:

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}")
ten_env.log_debug(f"on_cmd: {cmd_json}")

# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)
Expand All @@ -50,7 +50,7 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
ten_env.return_result(cmd_result, cmd)

async def on_stop(self, ten_env: AsyncTenEnv) -> None:
ten_env.log_debug("DefaultExtension on_stop")
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 @@ -38,7 +38,7 @@ async def on_deinit(self, ten_env: AsyncTenEnv) -> None:

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}")
ten_env.log_debug(f"on_cmd: {cmd_json}")

# Mock async operation, e.g. network, file I/O.
await asyncio.sleep(0.5)
Expand All @@ -50,7 +50,7 @@ async def on_cmd(self, ten_env: AsyncTenEnv, cmd: Cmd) -> None:
ten_env.return_result(cmd_result, cmd)

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

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

0 comments on commit ce02a9d

Please sign in to comment.