structio/tests/task_handling.py

42 lines
1.4 KiB
Python
Raw Normal View History

import structio
from nested_pool_inner_raises import successful, failing
async def main_cancel(i):
print("[main] Parent is alive, spawning child")
t = structio.clock()
async with structio.create_pool() as pool:
task: structio.Task = pool.spawn(successful, "test", i * 2)
print(f"[main] Child spawned, waiting {i} seconds before canceling it")
await structio.sleep(i)
print("[main] Cancelling child")
task.cancel()
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
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:
print(f"[main] Child has returned: {await pool.spawn(successful, 'test', 5)}")
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
async def main_wait_failing():
print("[main] Parent is alive, spawning (and explicitly waiting for) child")
t = structio.clock()
try:
async with structio.create_pool() as pool:
print(f"[main] Child has returned: {await pool.spawn(failing, 'test', 5)}")
except TypeError:
print(f"[main] TypeError caught!")
print(f"[main] Exited in {structio.clock() - t:.2f} seconds")
# Total time should be about 15s
structio.run(main_cancel, 5)
structio.run(main_wait_successful)
structio.run(main_wait_failing)