From 176dc38c4236dcc23c5b9a7d2866524717a4f444 Mon Sep 17 00:00:00 2001 From: Luciano Ramalho Date: Wed, 23 Jan 2019 20:45:59 -0200 Subject: [PATCH] updated more ch. 18 examples to Python 3.7 --- 18-asyncio-py3.7/countdown.py | 30 +++++++++++++++++ 18-asyncio-py3.7/spinner_asyncio.py | 28 ++++++++-------- 18-asyncio-py3.7/spinner_curio.py | 51 ----------------------------- 18-asyncio-py3.7/spinner_thread.py | 2 ++ 4 files changed, 47 insertions(+), 64 deletions(-) create mode 100644 18-asyncio-py3.7/countdown.py mode change 100644 => 100755 18-asyncio-py3.7/spinner_asyncio.py delete mode 100755 18-asyncio-py3.7/spinner_curio.py mode change 100644 => 100755 18-asyncio-py3.7/spinner_thread.py diff --git a/18-asyncio-py3.7/countdown.py b/18-asyncio-py3.7/countdown.py new file mode 100644 index 0000000..5f09000 --- /dev/null +++ b/18-asyncio-py3.7/countdown.py @@ -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) diff --git a/18-asyncio-py3.7/spinner_asyncio.py b/18-asyncio-py3.7/spinner_asyncio.py old mode 100644 new mode 100755 index b9fc9d5..adbd611 --- a/18-asyncio-py3.7/spinner_asyncio.py +++ b/18-asyncio-py3.7/spinner_asyncio.py @@ -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 diff --git a/18-asyncio-py3.7/spinner_curio.py b/18-asyncio-py3.7/spinner_curio.py deleted file mode 100755 index 0af06d8..0000000 --- a/18-asyncio-py3.7/spinner_curio.py +++ /dev/null @@ -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() diff --git a/18-asyncio-py3.7/spinner_thread.py b/18-asyncio-py3.7/spinner_thread.py old mode 100644 new mode 100755 index a907a25..dffcca6 --- a/18-asyncio-py3.7/spinner_thread.py +++ b/18-asyncio-py3.7/spinner_thread.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + # spinner_thread.py # credits: Adapted from Michele Simionato's