summaryrefslogtreecommitdiff
path: root/t/random_wait.py
blob: 2cfc2905119fcb406ddcdf683309c93198068e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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())