From 4a7e4cb73247906512dc6dec53a5c0b86da0ba29 Mon Sep 17 00:00:00 2001 From: Nocturn9x Date: Wed, 17 May 2023 11:21:18 +0200 Subject: [PATCH] Added notes to task handling test --- tests/task_handling.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/task_handling.py b/tests/task_handling.py index c3119d2..7fae55a 100644 --- a/tests/task_handling.py +++ b/tests/task_handling.py @@ -11,6 +11,7 @@ async def main_cancel(i): print(f"[main] Child spawned, waiting {i} seconds before canceling it") await structio.sleep(i) print("[main] Cancelling child") + # Tasks can be cancelled individually, if necessary task.cancel() print(f"[main] Exited in {structio.clock() - t:.2f} seconds") @@ -19,6 +20,13 @@ async def main_wait_successful(): print("[main] Parent is alive, spawning (and explicitly waiting for) child") t = structio.clock() async with structio.create_pool() as pool: + # The spawn() method returns a Task object that can be + # independently managed if necessary. Awaiting the Task + # will wait for it to complete and return its return value, + # as well as propagate any exceptions it may raise. Note that + # in this example we could've just awaited the coroutine directly, + # so it's a bad show for the feature, but you could theoretically + # pass the object around somewhere else and do the awaiting there print(f"[main] Child has returned: {await pool.spawn(successful, 'test', 5)}") print(f"[main] Exited in {structio.clock() - t:.2f} seconds")