Skip to content

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‎

Lines changed: 30 additions & 0 deletions
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+
importasyncio
7+
importtime
8+
9+
10+
asyncdefcountdown(label, delay):
11+
tabs= (ord(label) -ord('A')) *'\t'
12+
n=3
13+
whilen>0:
14+
awaitasyncio.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
Lines changed: 15 additions & 13 deletions
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
importasyncio
911
importitertools
1012
importsys
@@ -18,31 +20,31 @@ async def spin(msg): # <1>
1820
flush()
1921
write('\x08'*len(status))
2022
try:
21-
awaitasyncio.sleep(.1) # <3>
22-
exceptasyncio.CancelledError: # <4>
23+
awaitasyncio.sleep(.1) # <2>
24+
exceptasyncio.CancelledError: # <3>
2325
break
2426
write(' '*len(status) +'\x08'*len(status))
2527

2628

27-
asyncdefslow_function(): # <5>
29+
asyncdefslow_function(): # <4>
2830
# pretend waiting a long time for I/O
29-
awaitasyncio.sleep(3) # <6>
31+
awaitasyncio.sleep(3) # <5>
3032
return42
3133

3234

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

4042

4143
defmain():
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‎

Lines changed: 0 additions & 51 deletions
This file was deleted.

‎18-asyncio-py3.7/spinner_thread.py‎

100644100755
Lines changed: 2 additions & 0 deletions
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)