forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinner_async_experiment.py
39 lines (32 loc) · 1.02 KB
/
spinner_async_experiment.py
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
31
32
33
34
35
36
37
38
39
# spinner_async_experiment.py
# credits: Example by Luciano Ramalho inspired by
# Michele Simionato's multiprocessing example in the python-list:
# https://mail.python.org/pipermail/python-list/2009-February/675659.html
import asyncio
import itertools
import time
async def spin(msg: str) -> None:
for char in itertools.cycle(r'\|/-'):
status = f'\r{char} {msg}'
print(status, flush=True, end='')
try:
await asyncio.sleep(.1)
except asyncio.CancelledError:
break
print('THIS WILL NEVER BE OUTPUT')
# tag::SPINNER_ASYNC_EXPERIMENT[]
async def slow() -> int:
time.sleep(3) # <4>
return 42
async def supervisor() -> int:
spinner = asyncio.create_task(spin('thinking!')) # <1>
print(f'spinner object: {spinner}') # <2>
result = await slow() # <3>
spinner.cancel() # <5>
return result
# end::SPINNER_ASYNC_EXPERIMENT[]
def main() -> None:
result = asyncio.run(supervisor())
print(f'Answer: {result}')
if __name__ == '__main__':
main()