Runtime¶
本节介绍每个 TongSIM Python 会话都会用到的运行时核心组件:
TongSim:同步友好的入口封装,聚合WorldContext与常用工具。WorldContext:管理专用AsyncLoop、gRPC 连接与资源生命周期。AsyncLoop:在后台线程运行 asyncio loop,便于同步代码安全驱动异步 RPC。
此外,本节也包含基础的日志初始化与版本信息查询接口。
References¶
TongSim¶
tongsim.tongsim.TongSim ¶
High-level SDK entry point for controlling a connected TongSim UE instance. All methods expose synchronous, blocking interfaces for scripts or synchronous applications.
Source code in src/tongsim/tongsim.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | |
utils
property
¶
utils: UtilFuncs
Return helper utilities that wrap frequently used runtime operations.
Returns:
| Name | Type | Description |
|---|---|---|
UtilFuncs |
UtilFuncs
|
Helper wrapper exposing convenience functions. |
context
property
¶
context: WorldContext
Access the runtime context that manages connections, event loop and task dispatch.
Returns:
| Name | Type | Description |
|---|---|---|
WorldContext |
WorldContext
|
Context object owning the async loop and gRPC resources. |
close ¶
close()
Shut down the current runtime and release all managed resources.
Source code in src/tongsim/tongsim.py
56 57 58 | |
WorldContext¶
tongsim.core.world_context.WorldContext ¶
Aggregate runtime resources for a TongSim session.
Responsibilities: - Manage the dedicated AsyncLoop. - Hold the gRPC connection (GrpcConnection and LegacyGrpcStreamClient).
Notes
- All owned resources are closed automatically during teardown.
Source code in src/tongsim/core/world_context.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
sync_run ¶
sync_run(
coro: Awaitable, timeout: float | None = None
) -> Any
Execute an async coroutine on the loop and wait for it synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable
|
Coroutine to run. |
required |
timeout
|
float | None
|
Optional timeout in seconds. Raises TimeoutError if exceeded. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
Result returned by the coroutine. |
Source code in src/tongsim/core/world_context.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
async_task ¶
async_task(coro: Awaitable[Any], name: str) -> Future[Any]
Schedule a coroutine on the loop without waiting for completion.
Source code in src/tongsim/core/world_context.py
88 89 90 | |
release ¶
release()
Release all managed resources: - cancel outstanding tasks - close the gRPC connection - stop the event loop
Source code in src/tongsim/core/world_context.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | |
AsyncLoop¶
tongsim.core.async_loop.AsyncLoop ¶
Wrap an asyncio event loop that lives on a dedicated background thread and exposes a TaskGroup for scheduling.
Features: - Persistent background thread hosting the event loop. - Managed asyncio.TaskGroup for business coroutines.
Source code in src/tongsim/core/async_loop.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
start ¶
start(timeout: float = 1.0) -> None
Launch the background thread and event loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum time to wait for the loop and TaskGroup to become ready. |
1.0
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the loop fails to start within the timeout. |
Source code in src/tongsim/core/async_loop.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
spawn ¶
spawn(coro: Awaitable[Any], name: str = '') -> Future[Any]
Submit a coroutine to the TaskGroup.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coro
|
Awaitable[Any]
|
Coroutine object to execute. |
required |
name
|
str
|
Optional name for logging. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
Future |
Future[Any]
|
A concurrent.futures.Future mirroring coroutine completion or raising the underlying exception. |
Source code in src/tongsim/core/async_loop.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
cancel_tasks ¶
cancel_tasks(timeout: float) -> None
Cancel all application tasks that were spawned via spawn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum time to wait for cancellation to finish. |
required |
Source code in src/tongsim/core/async_loop.py
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
stop ¶
stop(timeout: float = 5.0) -> None
Gracefully stop the AsyncLoop: cancel business tasks -> cancel main task -> stop loop -> join thread.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float
|
Maximum time to wait for shutdown. |
5.0
|
Source code in src/tongsim/core/async_loop.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | |
is_running ¶
is_running() -> bool
Check whether the loop thread is alive.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in src/tongsim/core/async_loop.py
209 210 211 212 213 214 215 216 | |
log_task_list ¶
log_task_list() -> None
Log all tasks currently known to the loop for diagnostics.
Source code in src/tongsim/core/async_loop.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
Logging¶
tongsim.logger.initialize_logger ¶
initialize_logger(
level: int = INFO,
log_to_file: bool = False,
log_dir: str = "logs",
)
Configure the default log level and file output options. Call once at program entry.
:param level: Default log level (e.g. logging.INFO).
:param log_to_file: Whether to write logs to a file.
:param log_dir: Directory for log files (default: logs/).
Source code in src/tongsim/logger.py
102 103 104 105 106 107 108 109 110 111 112 | |
tongsim.logger.set_log_level ¶
set_log_level(module: str, level: int)
Set the log level for a given module.
:param module: Module name.
:param level: Log level, e.g. logging.DEBUG or logging.ERROR.
Source code in src/tongsim/logger.py
124 125 126 127 128 129 130 131 | |
tongsim.logger.get_logger ¶
get_logger(module: str) -> Logger
Get a module logger. Prefix format: [TongSim_Lite][<module>] <message>.
:param module: Module name.
Source code in src/tongsim/logger.py
115 116 117 118 119 120 121 | |
Version¶
tongsim.version.get_version_info ¶
get_version_info() -> str
Get version/runtime information for TongSIM Lite.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A multi-line formatted string describing the current version state. |
Source code in src/tongsim/version.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |