- 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
26 lines (23 loc) · 720 Bytes
/
Copy pathlesson05_future_callback.py
File metadata and controls
26 lines (23 loc) · 720 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
26
# SuperFastPython.com
# example of adding a done callback to a task
fromtimeimportsleep
fromconcurrent.futuresimportProcessPoolExecutor
# callback function to call when a task is completed
defcustom_callback(future):
# report a message
print(f'Custom callback got: {future.result()}',
flush=True)
# custom task function executed in a worker process
defwork():
# block for a moment
sleep(1)
# return a result
return99
# protect the entry point
if__name__=='__main__':
# create a process pool
withProcessPoolExecutor() asexe:
# execute the task
fut=exe.submit(work)
# add the custom callback
fut.add_done_callback(custom_callback)