You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB
Python

import asyncio
import itertools
import sys
async def spin(msg):
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
status = f"{char} {msg}"
write(status)
flush()
write('\x08' * len(status)) # backspace out the line
try:
await asyncio.sleep(.1)
except asyncio.CancelledError:
break
write(' ' * len(status) + '\x08' * len(status))
async def slow_function():
await asyncio.sleep(3)
return 42
async def supervisor():
spinner = loop.create_task(spin('Thinking!'))
print('Spinner Object:', spinner)
result = await slow_function()
spinner.cancel()
return result
if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
# returns a Task object, not the result objects themselves. You have to access the Task result
# to see the output
result = loop.run_until_complete(supervisor())
except Exception as e:
pass
finally:
loop.close()