- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson02_initialize_processes.py
More file actions
Latest commit
24 lines (21 loc) · 699 Bytes
/
Copy pathlesson02_initialize_processes.py
File metadata and controls
24 lines (21 loc) · 699 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 initializing worker processes in the pool
fromtimeimportsleep
fromconcurrent.futuresimportProcessPoolExecutor
# custom function to be executed in a worker process
deftask(number):
# report a message
print(f'Worker task {number}...', flush=True)
# block for a moment
sleep(1)
# initialize a worker in the process pool
definit():
# report a message
print('Initializing worker...', flush=True)
# protect the entry point
if__name__=='__main__':
# create and configure the process pool
withProcessPoolExecutor(2,
initializer=init) asexe:
# issue tasks to the process pool
_=exe.map(task, range(4))