diff options
| author | bd <bdunahu@operationnull.com> | 2025-07-19 23:45:36 -0600 |
|---|---|---|
| committer | bd <bdunahu@operationnull.com> | 2025-07-19 23:45:36 -0600 |
| commit | 2e208349d2e6f8f3d7fbbe853413549da9df0ce0 (patch) | |
| tree | 2a2b181cdc6125ba26bb91e07454fdf5e01160a4 /t/manual | |
| parent | 0c0af7dd5a9d1c30820b52f8209151f0f2d5610d (diff) | |
Relicensed as Apache2.0
Diffstat (limited to 't/manual')
| -rw-r--r-- | t/manual/random_wait.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/t/manual/random_wait.py b/t/manual/random_wait.py new file mode 100644 index 0000000..2cfc290 --- /dev/null +++ b/t/manual/random_wait.py @@ -0,0 +1,22 @@ +# SuperFastPython.com +# example of waiting for all tasks to complete +from random import random +import asyncio + +total = 0 + + +async def task_coro(arg): + value = random() + total += value + await asyncio.sleep(value) + print(f'>{arg} done in {value}') + + +async def main(): + tasks = [asyncio.create_task(task_coro(i)) for i in range(10)] + done, pending = await asyncio.wait(tasks) + print(f'All done. Total waiting time: {total}') + + +asyncio.run(main()) |
