- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson06_wait_all.py
More file actions
Latest commit
24 lines (22 loc) · 741 Bytes
/
Copy pathlesson06_wait_all.py
File metadata and controls
24 lines (22 loc) · 741 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
# SuperFastPython.com
# example of waiting for all tasks to complete
fromtimeimportsleep
fromrandomimportrandom
fromconcurrent.futuresimportProcessPoolExecutor
fromconcurrent.futuresimportwait
# custom task function executed by a worker process
deftask(number):
# block for a fraction of a second
sleep(random())
# report value
print(number, flush=True)
# protect the entry point
if__name__=='__main__':
# start the process pool
withProcessPoolExecutor(10) asexe:
# issue all tasks and gather the futures
futs= [exe.submit(task, i) foriinrange(10)]
# wait for all tasks to complete
_=wait(futs)
# report a message
print('All tasks are done!')