updated more ch. 18 examples to Python 3.7

This commit is contained in:
Luciano Ramalho 2019-01-23 20:45:59 -02:00
parent 29d6835f3b
commit 176dc38c42
4 changed files with 47 additions and 64 deletions

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
# Inspired by
# https://snarky.ca/how-the-heck-does-async-await-work-in-python-3-5/
import asyncio
import time
async def countdown(label, delay):
tabs = (ord(label) - ord('A')) * '\t'
n = 3
while n > 0:
await asyncio.sleep(delay) # <----
dt = time.perf_counter() - t0
print('' * 50)
print(f'{dt:7.4f}s \t{tabs}{label} = {n}')
n -= 1
loop = asyncio.get_event_loop()
tasks = [
loop.create_task(countdown('A', .7)),
loop.create_task(countdown('B', 2)),
loop.create_task(countdown('C', .3)),
loop.create_task(countdown('D', 1)),
]
t0 = time.perf_counter()
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
print('' * 50)

28
18-asyncio-py3.7/spinner_asyncio.py Normal file → Executable file
View File

@ -1,10 +1,12 @@
# spinner_await.py #!/usr/bin/env python3
# spinner_asyncio.py
# credits: Example by Luciano Ramalho inspired by # credits: Example by Luciano Ramalho inspired by
# Michele Simionato's multiprocessing example in the python-list: # Michele Simionato's multiprocessing example in the python-list:
# https://mail.python.org/pipermail/python-list/2009-February/538048.html # https://mail.python.org/pipermail/python-list/2009-February/538048.html
# BEGIN SPINNER_AWAIT # BEGIN SPINNER_ASYNCIO
import asyncio import asyncio
import itertools import itertools
import sys import sys
@ -18,31 +20,31 @@ async def spin(msg): # <1>
flush() flush()
write('\x08' * len(status)) write('\x08' * len(status))
try: try:
await asyncio.sleep(.1) # <3> await asyncio.sleep(.1) # <2>
except asyncio.CancelledError: # <4> except asyncio.CancelledError: # <3>
break break
write(' ' * len(status) + '\x08' * len(status)) write(' ' * len(status) + '\x08' * len(status))
async def slow_function(): # <5> async def slow_function(): # <4>
# pretend waiting a long time for I/O # pretend waiting a long time for I/O
await asyncio.sleep(3) # <6> await asyncio.sleep(3) # <5>
return 42 return 42
async def supervisor(): # <7> async def supervisor(): # <6>
spinner = asyncio.create_task(spin('thinking!')) # <8> spinner = asyncio.create_task(spin('thinking!')) # <7>
print('spinner object:', spinner) # <9> print('spinner object:', spinner) # <8>
result = await slow_function() # <10> result = await slow_function() # <9>
spinner.cancel() # <11> spinner.cancel() # <10>
return result return result
def main(): def main():
result = asyncio.run(supervisor()) # <12> result = asyncio.run(supervisor()) # <11>
print('Answer:', result) print('Answer:', result)
if __name__ == '__main__': if __name__ == '__main__':
main() main()
# END SPINNER_AWAIT # END SPINNER_ASYNCIO

View File

@ -1,51 +0,0 @@
#!/usr/bin/env python3
# spinner_curio.py
# credits:
# Example from the book Fluent Python by Luciano Ramalho inspired by
# Michele Simionato's multiprocessing example in the python-list:
# https://mail.python.org/pipermail/python-list/2009-February/538048.html
# ported to use David Beazley's `curio` library:
# https://github.com/dabeaz/curio
import curio
import itertools
import sys
async def spin(msg): # <1>
write, flush = sys.stdout.write, sys.stdout.flush
for char in itertools.cycle('|/-\\'):
status = char + ' ' + msg
write(status)
flush()
write('\x08' * len(status))
try:
await curio.sleep(.1) # <2>
except curio.CancelledError: # <3>
break
write(' ' * len(status) + '\x08' * len(status))
async def slow_function(): # <4>
# pretend waiting a long time for I/O
await curio.sleep(3) # <5>
return 42
async def supervisor(): # <6>
spinner = await curio.spawn(spin('thinking!')) # <7>
print('spinner object:\n ', repr(spinner)) # <8>
result = await slow_function() # <9>
await spinner.cancel() # <10>
return result
def main():
result = curio.run(supervisor) # <11>
print('Answer:', result)
if __name__ == '__main__':
main()

2
18-asyncio-py3.7/spinner_thread.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/env python3
# spinner_thread.py # spinner_thread.py
# credits: Adapted from Michele Simionato's # credits: Adapted from Michele Simionato's