- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson03_map_multiple_arguments.py
More file actions
Latest commit
25 lines (23 loc) · 813 Bytes
/
Copy pathlesson03_map_multiple_arguments.py
File metadata and controls
25 lines (23 loc) · 813 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
# SuperFastPython.com
# example executing tasks concurrently with multiple arg
fromrandomimportrandom
fromtimeimportsleep
fromconcurrent.futuresimportProcessPoolExecutor
# custom function to be executed in a worker process
deftask(number, value):
# report a message
print(f'Task using {value}', flush=True)
# block for a moment to simulate work
sleep(value)
# return a new value
returnnumber+value
# protect the entry point
if__name__=='__main__':
# create the process pool
withProcessPoolExecutor(4) asexe:
# prepare random numbers between 0 and 1
values= [random() for_inrange(10)]
# issue tasks to execute concurrently
forresultinexe.map(task, range(10), values):
# report results
print(result)