Added notes to task handling test

This commit is contained in:
Mattia Giambirtone 2023-05-17 11:21:18 +02:00 committed by nocturn9x
parent e46a41dd8f
commit 4a7e4cb732
Signed by: nocturn9x
GPG Key ID: 8270F9F467971E59
1 changed files with 8 additions and 0 deletions

View File

@ -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")