example-code-2e/18-asyncio-py3.7/spinner_asyncio.py
2019-01-23 20:45:59 -02:00

51 lines
1.2 KiB
Python
Executable File

#!/usr/bin/env python3
# spinner_asyncio.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/538048.html
# BEGIN SPINNER_ASYNCIO
import asyncio
import itertools
import sys
async def spin(msg): # <1>
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
status = char + ' ' + msg
write(status)
flush()
write('\x08' * len(status))
try:
await asyncio.sleep(.1) # <2>
except asyncio.CancelledError: # <3>
break
write(' ' * len(status) + '\x08' * len(status))
async def slow_function(): # <4>
# pretend waiting a long time for I/O
await asyncio.sleep(3) # <5>
return 42
async def supervisor(): # <6>
spinner = asyncio.create_task(spin('thinking!')) # <7>
print('spinner object:', spinner) # <8>
result = await slow_function() # <9>
spinner.cancel() # <10>
return result
def main():
result = asyncio.run(supervisor()) # <11>
print('Answer:', result)
if __name__ == '__main__':
main()
# END SPINNER_ASYNCIO