- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlesson04_map_async.py
More file actions
Latest commit
20 lines (18 loc) · 629 Bytes
/
Copy pathlesson04_map_async.py
File metadata and controls
20 lines (18 loc) · 629 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# SuperFastPython.com
# example executing multiple tasks asynchronously
frommultiprocessingimportPool
# custom function to be executed in a child process
deftask(arg):
# report a message
print(f'From another process {arg}', flush=True)
# return a value
returnarg*2
# protect the entry point
if__name__=='__main__':
# create the multiprocessing pool
withPool() aspool:
# issue multiple tasks to the pool
async_result=pool.map_async(task, range(10))
# process return values once all tasks completed
forresultinasync_result.get():
print(result)