Skip to content

Commit 890495d

Browse files
committed
spinner with async/await
1 parent daa096e commit 890495d

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

18b-async-await/spinner_await.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,30 @@
1010
import sys
1111

1212

13-
@asyncio.coroutine # <1>
14-
def spin(msg): # <2>
13+
async def spin(msg): # <1>
1514
write, flush = sys.stdout.write, sys.stdout.flush
1615
for char in itertools.cycle('|/-\\'):
1716
status = char + ' ' + msg
1817
write(status)
1918
flush()
2019
write('\x08' * len(status))
2120
try:
22-
yield from asyncio.sleep(.1) # <3>
21+
await asyncio.sleep(.1) # <3>
2322
except asyncio.CancelledError: # <4>
2423
break
2524
write(' ' * len(status) + '\x08' * len(status))
2625

2726

28-
@asyncio.coroutine
29-
def slow_function(): # <5>
27+
async def slow_function(): # <5>
3028
# pretend waiting a long time for I/O
31-
yield from asyncio.sleep(3) # <6>
29+
await asyncio.sleep(3) # <6>
3230
return 42
3331

3432

35-
@asyncio.coroutine
36-
def supervisor(): # <7>
37-
spinner = asyncio.async(spin('thinking!')) # <8>
33+
async def supervisor(): # <7>
34+
spinner = asyncio.ensure_future(spin('thinking!')) # <8>
3835
print('spinner object:', spinner) # <9>
39-
result = yield from slow_function() # <10>
36+
result = await slow_function() # <10>
4037
spinner.cancel() # <11>
4138
return result
4239

0 commit comments

Comments
 (0)