- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson05_future_callback.py
More file actions
Latest commit
25 lines (22 loc) · 696 Bytes
/
Copy pathlesson05_future_callback.py
File metadata and controls
25 lines (22 loc) · 696 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 of adding a done callback to a task
fromtimeimportsleep
fromconcurrent.futuresimportThreadPoolExecutor
# callback function to call when a task is completed
defcustom_callback(future):
# report a message
print(f'Custom callback got: {future.result()}')
# custom task function executed in a worker thread
defwork():
# block for a moment
sleep(1)
# return a result
return99
# protect the entry point
if__name__=='__main__':
# create a thread pool
withThreadPoolExecutor() asexe:
# execute the task
fut=exe.submit(work)
# add the custom callback
fut.add_done_callback(custom_callback)