-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner_asyncio.py
More file actions
44 lines (34 loc) · 975 Bytes
/
Copy pathspinner_asyncio.py
File metadata and controls
44 lines (34 loc) · 975 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import asyncio
import itertools
import sys
@asyncio.coroutine #1
def spin(msg): #2
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
status = char + ' ' + msg
write(status)
flush()
write('\x08' * len(status))
try:
yield from asyncio.sleep(.1) #3
except asyncio.CancelledError: #4
break
write(' ' * len(status) + '\x08' * len(status))
@asyncio.coroutine
def slow_function(): #5
yield from asyncio.sleep(3) #6
return '高科技'
@asyncio.coroutine
def supervisor(): #7
spinner = asyncio.async(spin('thinking')) #8
print('spinner object:', spinner) #9
result = yield from slow_function() #10
spinner.cancel() #11
return result
def main():
loop = asyncio.get_event_loop() #12
result = loop.run_until_complete(supervisor()) #13
loop.close()
print('Answer:', result)
if __name__ == '__main__':
main()