1- # spinner_await.py
1+ #!/usr/bin/env python3
2+
3+ # spinner_asyncio.py
24
35# credits: Example by Luciano Ramalho inspired by
46# Michele Simionato's multiprocessing example in the python-list:
57# https://mail.python.org/pipermail/python-list/2009-February/538048.html
68
7- # BEGIN SPINNER_AWAIT
9+ # BEGIN SPINNER_ASYNCIO
810import asyncio
911import itertools
1012import sys
@@ -18,31 +20,31 @@ async def spin(msg): # <1>
1820 flush ()
1921 write ('\x08 ' * len (status ))
2022 try :
21- await asyncio .sleep (.1 ) # <3 >
22- except asyncio .CancelledError : # <4 >
23+ await asyncio .sleep (.1 ) # <2 >
24+ except asyncio .CancelledError : # <3 >
2325 break
2426 write (' ' * len (status ) + '\x08 ' * len (status ))
2527
2628
27- async def slow_function (): # <5 >
29+ async def slow_function (): # <4 >
2830 # pretend waiting a long time for I/O
29- await asyncio .sleep (3 ) # <6 >
31+ await asyncio .sleep (3 ) # <5 >
3032 return 42
3133
3234
33- async def supervisor (): # <7 >
34- spinner = asyncio .create_task (spin ('thinking!' )) # <8 >
35- print ('spinner object:' , spinner ) # <9 >
36- result = await slow_function () # <10 >
37- spinner .cancel () # <11 >
35+ async def supervisor (): # <6 >
36+ spinner = asyncio .create_task (spin ('thinking!' )) # <7 >
37+ print ('spinner object:' , spinner ) # <8 >
38+ result = await slow_function () # <9 >
39+ spinner .cancel () # <10 >
3840 return result
3941
4042
4143def main ():
42- result = asyncio .run (supervisor ()) # <12 >
44+ result = asyncio .run (supervisor ()) # <11 >
4345 print ('Answer:' , result )
4446
4547
4648if __name__ == '__main__' :
4749 main ()
48- # END SPINNER_AWAIT
50+ # END SPINNER_ASYNCIO
0 commit comments