- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson02_initialize_threads.py
More file actions
Latest commit
23 lines (20 loc) · 669 Bytes
/
Copy pathlesson02_initialize_threads.py
File metadata and controls
23 lines (20 loc) · 669 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# SuperFastPython.com
# example initializing worker threads in the pool
fromtimeimportsleep
fromconcurrent.futuresimportThreadPoolExecutor
# custom function to be executed in a worker thread
deftask(number):
# report a message
print(f'Worker executing task {number}...')
# block for a moment
sleep(1)
# initialize a worker in the thread pool
definit():
# report a message
print('Initializing worker...')
# protect the entry point
if__name__=='__main__':
# create and configure the thread pool
withThreadPoolExecutor(2, initializer=init) asexe:
# issue tasks to the thread pool
_=exe.map(task, range(4))