- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson05_future_result_timeout.py
More file actions
Latest commit
26 lines (24 loc) · 776 Bytes
/
Copy pathlesson05_future_result_timeout.py
File metadata and controls
26 lines (24 loc) · 776 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 timeout while waiting for task result
fromtimeimportsleep
fromconcurrent.futuresimportThreadPoolExecutor
fromconcurrent.futuresimportTimeoutError
# 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)
try:
# get the result from the task, blocks
result=future.result(timeout=0.5)
# report the result
print(f'Got Result: {result}')
exceptTimeoutError:
print('Gave up waiting for a result')