- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlesson05_imap.py
More file actions
Latest commit
22 lines (20 loc) · 654 Bytes
/
Copy pathlesson05_imap.py
File metadata and controls
22 lines (20 loc) · 654 Bytes
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 executing multiple tasks one-by-one
fromtimeimportsleep
fromrandomimportrandom
frommultiprocessingimportPool
# custom function to be executed in a child process
deftask(arg):
# block for a random fraction of a second
sleep(random())
# 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(4) aspool:
# issue multiple tasks and process return values
forresultinpool.imap(task, range(10)):
print(result)