- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson05_future_exception.py
More file actions
Latest commit
30 lines (28 loc) · 898 Bytes
/
Copy pathlesson05_future_exception.py
File metadata and controls
30 lines (28 loc) · 898 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
27
28
29
30
# SuperFastPython.com
# example of handling an exception raised within a task
fromtimeimportsleep
fromconcurrent.futuresimportProcessPoolExecutor
fromconcurrent.futuresimportwait
# custom task function executed by a worker process
defwork():
# block a moment
sleep(1)
# raise an exception
raiseException('Something bad happened!')
# never gets here
# protect the entry point
if__name__=='__main__':
# create a process pool
withProcessPoolExecutor() asexe:
# execute our task
future=exe.submit(work)
# wait for the task to be done
wait([future])
# get the exception
exception=future.exception()
print(f'Task exception={exception}')
# get the result from the task
try:
result=future.result()
exceptException:
print('Unable to get the result')