Skip to content

Commit a58ecf6

Browse files
committed
simplified spinner_* examples
1 parent 6de94c1 commit a58ecf6

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

18-asyncio-py3.7/spinner_asyncio.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@
99
# BEGIN SPINNER_ASYNCIO
1010
import asyncio
1111
import itertools
12-
import sys
1312

1413

1514
async def spin(msg): # <1>
16-
write, flush = sys.stdout.write, sys.stdout.flush
1715
for char in itertools.cycle('|/-\\'):
1816
status = char + ' ' + msg
19-
write(status)
20-
flush()
21-
write('\x08' * len(status))
17+
print(status, flush=True, end='\r')
2218
try:
2319
await asyncio.sleep(.1) # <2>
2420
except asyncio.CancelledError: # <3>
2521
break
26-
write(' ' * len(status) + '\x08' * len(status))
22+
print(' ' * len(status), end='\r')
2723

2824

2925
async def slow_function(): # <4>

18-asyncio-py3.7/spinner_thread.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,15 @@
1010
import threading
1111
import itertools
1212
import time
13-
import sys
1413

1514

16-
def spin(msg, done): # <2>
17-
write, flush = sys.stdout.write, sys.stdout.flush
15+
def spin(msg, done): # <1>
1816
for char in itertools.cycle('|/-\\'): # <3>
1917
status = char + ' ' + msg
20-
write(status)
21-
flush()
22-
write('\x08' * len(status)) # <4>
18+
print(status, flush=True, end='\r')
2319
if done.wait(.1): # <5>
2420
break
25-
write(' ' * len(status) + '\x08' * len(status)) # <6>
26-
21+
print(' ' * len(status), end='\r')
2722

2823
def slow_function(): # <7>
2924
# pretend waiting a long time for I/O

0 commit comments

Comments
 (0)