- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlesson05_future_status.py
More file actions
Latest commit
26 lines (24 loc) · 818 Bytes
/
Copy pathlesson05_future_status.py
File metadata and controls
26 lines (24 loc) · 818 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 checking the status of an asynchronous task
fromtimeimportsleep
fromconcurrent.futuresimportProcessPoolExecutor
# task function executed in a worker process
defwork():
# block for a moment
sleep(0.5)
# protect the entry point
if__name__=='__main__':
# create a process pool
withProcessPoolExecutor() asexe:
# start one task
future=exe.submit(work)
# confirm that the task is running
running=future.running()
done=future.done()
print(f'Future running={running}, done={done}')
# wait for the task to complete
future.result()
# confirm that the task is done
running=future.running()
done=future.done()
print(f'Future running={running}, done={done}')