|
23 | 23 | from trio._core._run import GLOBAL_RUN_CONTEXT as trio_global_context |
24 | 24 |
|
25 | 25 |
|
| 26 | +@enum.unique |
| 27 | +class TrunkMode(enum.Enum): |
| 28 | + "framework type" |
| 29 | + |
| 30 | + TRIO = enum.auto() |
| 31 | + CURIO = enum.auto() |
| 32 | + ASYNCIO = enum.auto() |
| 33 | + |
| 34 | + |
26 | 35 | @enum.unique |
27 | 36 | class ExecCode(enum.IntEnum): |
28 | 37 | "server response status code" |
@@ -150,15 +159,15 @@ async def wait_curio_writable(cls, fd:int) -> None: |
150 | 159 | async def wait_asyncio_readable(cls, fd:int) -> None: |
151 | 160 | loop = asyncio.get_running_loop() |
152 | 161 | future = asyncio.Future() |
153 | | - future.add_done_callback(lambda : loop.remove_reader(fd)) |
| 162 | + future.add_done_callback(lambda *args : loop.remove_reader(fd)) |
154 | 163 | loop.add_reader(fd, future.set_result, None) |
155 | 164 | await future |
156 | 165 |
|
157 | 166 | @classmethod |
158 | 167 | async def wait_asyncio_writable(cls, fd:int) -> None: |
159 | 168 | loop = asyncio.get_running_loop() |
160 | 169 | future = asyncio.Future() |
161 | | - future.add_done_callback(lambda : loop.remove_writer(fd)) |
| 170 | + future.add_done_callback(lambda *args : loop.remove_writer(fd)) |
162 | 171 | loop.add_writer(fd, future.set_result, None) |
163 | 172 | await future |
164 | 173 |
|
@@ -213,17 +222,29 @@ async def this_task(cls) -> int: |
213 | 222 | raise RuntimeError(f"no loop") |
214 | 223 |
|
215 | 224 | @classmethod |
216 | | - async def spawn_task(cls, coro_func:CoroutineType, *args) -> "AnyTask": |
| 225 | + async def spawn_task(cls, func:CoroutineType, *args) -> "AnyTask": |
217 | 226 | "create and launch new background task" |
218 | 227 | if cls.has_trio_loop(): |
219 | | - return trio.hazmat.spawn_system_task(coro_func, *args) |
| 228 | + return trio.hazmat.spawn_system_task(func, *args) |
220 | 229 | elif cls.has_curio_loop(): |
221 | | - return await curio.spawn(coro_func, *args) |
| 230 | + return await curio.spawn(func, *args) |
222 | 231 | elif cls.has_asyncio_loop(): |
223 | | - return asyncio.create_task(coro_func(*args)) |
| 232 | + return asyncio.create_task(func(*args)) |
224 | 233 | else: |
225 | 234 | raise RuntimeError(f"no loop") |
226 | 235 |
|
| 236 | + @classmethod |
| 237 | + def invoke_main(cls, mode:TrunkMode, func:CoroutineType, *args, **kwargs) -> object: |
| 238 | + "create and launch main framework task" |
| 239 | + if mode == TrunkMode.TRIO: |
| 240 | + return trio.run(func, *args, **kwargs) |
| 241 | + elif mode == TrunkMode.CURIO: |
| 242 | + return curio.run(func, *args, **kwargs) |
| 243 | + elif mode == TrunkMode.ASYNCIO: |
| 244 | + return asyncio.run(func(*args, **kwargs)) |
| 245 | + else: |
| 246 | + raise RuntimeError(f"no mode: {mode}") |
| 247 | + |
227 | 248 | @classmethod |
228 | 249 | def default_tracer(cls, |
229 | 250 | token:TrunkToken, |
|
0 commit comments