updated more ch. 18 examples to Python 3.7
This commit is contained in:
parent
29d6835f3b
commit
176dc38c42
30
18-asyncio-py3.7/countdown.py
Normal file
30
18-asyncio-py3.7/countdown.py
Normal 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
28
18-asyncio-py3.7/spinner_asyncio.py
Normal file → Executable file
@ -1,10 +1,12 @@
|
||||
# spinner_await.py
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_asyncio.py
|
||||
|
||||
# credits: Example 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
|
||||
|
||||
# BEGIN SPINNER_AWAIT
|
||||
# BEGIN SPINNER_ASYNCIO
|
||||
import asyncio
|
||||
import itertools
|
||||
import sys
|
||||
@ -18,31 +20,31 @@ async def spin(msg): # <1>
|
||||
flush()
|
||||
write('\x08' * len(status))
|
||||
try:
|
||||
await asyncio.sleep(.1) # <3>
|
||||
except asyncio.CancelledError: # <4>
|
||||
await asyncio.sleep(.1) # <2>
|
||||
except asyncio.CancelledError: # <3>
|
||||
break
|
||||
write(' ' * len(status) + '\x08' * len(status))
|
||||
|
||||
|
||||
async def slow_function(): # <5>
|
||||
async def slow_function(): # <4>
|
||||
# pretend waiting a long time for I/O
|
||||
await asyncio.sleep(3) # <6>
|
||||
await asyncio.sleep(3) # <5>
|
||||
return 42
|
||||
|
||||
|
||||
async def supervisor(): # <7>
|
||||
spinner = asyncio.create_task(spin('thinking!')) # <8>
|
||||
print('spinner object:', spinner) # <9>
|
||||
result = await slow_function() # <10>
|
||||
spinner.cancel() # <11>
|
||||
async def supervisor(): # <6>
|
||||
spinner = asyncio.create_task(spin('thinking!')) # <7>
|
||||
print('spinner object:', spinner) # <8>
|
||||
result = await slow_function() # <9>
|
||||
spinner.cancel() # <10>
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
result = asyncio.run(supervisor()) # <12>
|
||||
result = asyncio.run(supervisor()) # <11>
|
||||
print('Answer:', result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
# END SPINNER_AWAIT
|
||||
# END SPINNER_ASYNCIO
|
||||
|
@ -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
2
18-asyncio-py3.7/spinner_thread.py
Normal file → Executable file
@ -1,3 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# spinner_thread.py
|
||||
|
||||
# credits: Adapted from Michele Simionato's
|
||||
|
Loading…
x
Reference in New Issue
Block a user