- Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlesson03_wait.py
More file actions
Latest commit
26 lines (23 loc) · 664 Bytes
/
Copy pathlesson03_wait.py
File metadata and controls
26 lines (23 loc) · 664 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# SuperFastPython.com
# example of waiting for all tasks to complete
fromrandomimportrandom
importasyncio
# coroutine to execute in a new task
asyncdeftask_coro(arg):
# generate a random value between 0 and 1
value=random()
# suspend for a moment
awaitasyncio.sleep(value)
# report the value
print(f'>task {arg} done with {value}')
# main coroutine
asyncdefmain():
# create many tasks
tasks= [asyncio.create_task(task_coro(i))
foriinrange(10)]
# wait for all tasks to complete
_=awaitasyncio.wait(tasks)
# report results
print('All done')
# start the asyncio program
asyncio.run(main())