Skip to content
This repository was archived by the owner on Dec 2, 2021. It is now read-only.

Commit 176dc38

Browse files
committed
updated more ch. 18 examples to Python 3.7
1 parent 29d6835 commit 176dc38

File tree

4 files changed

+47
-64
lines changed

4 files changed

+47
-64
lines changed

18-asyncio-py3.7/countdown.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
3+
# Inspired by
4+
# https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
5+
6+
import asyncio
7+
import time
8+
9+
10+
async def countdown(label, delay):
11+
tabs = (ord(label) - ord('A')) * '\t'
12+
n = 3
13+
while n > 0:
14+
await asyncio.sleep(delay) # <----
15+
dt = time.perf_counter() - t0
16+
print('━' * 50)
17+
print(f'{dt:7.4f}s \t{tabs}{label} = {n}')
18+
n -= 1
19+
20+
loop = asyncio.get_event_loop()
21+
tasks = [
22+
loop.create_task(countdown('A', .7)),
23+
loop.create_task(countdown('B', 2)),
24+
loop.create_task(countdown('C', .3)),
25+
loop.create_task(countdown('D', 1)),
26+
]
27+
t0 = time.perf_counter()
28+
loop.run_until_complete(asyncio.wait(tasks))
29+
loop.close()
30+
print('━' * 50)

18-asyncio-py3.7/spinner_asyncio.py

100644100755
+15-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
# spinner_await.py
1+
#!/usr/bin/env python3
2+
3+
# spinner_asyncio.py
24

35
# credits: Example by Luciano Ramalho inspired by
46
# Michele Simionato's multiprocessing example in the python-list:
57
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
68

7-
# BEGIN SPINNER_AWAIT
9+
# BEGIN SPINNER_ASYNCIO
810
import asyncio
911
import itertools
1012
import sys
@@ -18,31 +20,31 @@ async def spin(msg): # <1>
1820
flush()
1921
write('\x08' * len(status))
2022
try:
21-
await asyncio.sleep(.1) # <3>
22-
except asyncio.CancelledError: # <4>
23+
await asyncio.sleep(.1) # <2>
24+
except asyncio.CancelledError: # <3>
2325
break
2426
write(' ' * len(status) + '\x08' * len(status))
2527

2628

27-
async def slow_function(): # <5>
29+
async def slow_function(): # <4>
2830
# pretend waiting a long time for I/O
29-
await asyncio.sleep(3) # <6>
31+
await asyncio.sleep(3) # <5>
3032
return 42
3133

3234

33-
async def supervisor(): # <7>
34-
spinner = asyncio.create_task(spin('thinking!')) # <8>
35-
print('spinner object:', spinner) # <9>
36-
result = await slow_function() # <10>
37-
spinner.cancel() # <11>
35+
async def supervisor(): # <6>
36+
spinner = asyncio.create_task(spin('thinking!')) # <7>
37+
print('spinner object:', spinner) # <8>
38+
result = await slow_function() # <9>
39+
spinner.cancel() # <10>
3840
return result
3941

4042

4143
def main():
42-
result = asyncio.run(supervisor()) # <12>
44+
result = asyncio.run(supervisor()) # <11>
4345
print('Answer:', result)
4446

4547

4648
if __name__ == '__main__':
4749
main()
48-
# END SPINNER_AWAIT
50+
# END SPINNER_ASYNCIO

18-asyncio-py3.7/spinner_curio.py

-51
This file was deleted.

18-asyncio-py3.7/spinner_thread.py

100644100755
+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env python3
2+
13
# spinner_thread.py
24

35
# credits: Adapted from Michele Simionato's

0 commit comments

Comments
 (0)