- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson05_future_result.py
More file actions
Latest commit
22 lines (20 loc) · 616 Bytes
/
Copy pathlesson05_future_result.py
File metadata and controls
22 lines (20 loc) · 616 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 getting the result from a completed task
fromtimeimportsleep
fromconcurrent.futuresimportThreadPoolExecutor
# task function executed in a worker thread
defwork():
# block for a moment
sleep(1)
# return a message
return'All Done'
# protect the entry point
if__name__=='__main__':
# create a thread pool
withThreadPoolExecutor() asexe:
# start one thread
future=exe.submit(work)
# get the result from the task, blocks
result=future.result()
# report the result
print(f'Got Result: {result}')