From bcc92a8ec7f3d35ef119e1873bb8f640eed6ba24 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sat, 27 Jan 2024 18:37:58 -0500 Subject: [PATCH] Save work --- real_python/README.md | 23 +++++++++++++++++++++++ real_python/countasync.py | 23 +++++++++++++++++++++++ real_python/countsync.py | 22 ++++++++++++++++++++++ real_python/randasync.py | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 real_python/README.md create mode 100644 real_python/countasync.py create mode 100644 real_python/countsync.py create mode 100644 real_python/randasync.py diff --git a/real_python/README.md b/real_python/README.md new file mode 100644 index 0000000..d91748b --- /dev/null +++ b/real_python/README.md @@ -0,0 +1,23 @@ +# Real Python AsyncIO Walkthrough + +https://realpython.com/async-io-python/ + +## Rules of Async Coroutines + +```python +async def f(x): + y = await z(x) # OK - `await` and `return` allowed in coroutines + return y + +async def g(x): + yield x # OK - this is an async generator + +async def m(x): + yield from gen(x) # No - SyntaxError + +def m(x): + y = await z(x) # Still no - SyntaxError (no `async def` here) + return y +``` + +An awaitable object is either (1) another coroutine or (2) an object defining an .__await__() dunder method that returns an iterator. \ No newline at end of file diff --git a/real_python/countasync.py b/real_python/countasync.py new file mode 100644 index 0000000..3b5e6dc --- /dev/null +++ b/real_python/countasync.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# countasync.py + +import asyncio + + +async def count(): + print("One") + await asyncio.sleep(1) + print("Two") + + +async def main(): + await asyncio.gather(count(), count(), count()) + + +if __name__ == "__main__": + import time + + s = time.perf_counter() + asyncio.run(main()) + elapsed = time.perf_counter() - s + print(f"{__file__} executed in {elapsed:0.2f} seconds.") diff --git a/real_python/countsync.py b/real_python/countsync.py new file mode 100644 index 0000000..e93e2e4 --- /dev/null +++ b/real_python/countsync.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 +# countsync.py + +import time + + +def count(): + print("One") + time.sleep(1) + print("Two") + + +def main(): + for _ in range(3): + count() + + +if __name__ == "__main__": + s = time.perf_counter() + main() + elapsed = time.perf_counter() - s + print(f"{__file__} executed in {elapsed:0.2f} seconds.") diff --git a/real_python/randasync.py b/real_python/randasync.py new file mode 100644 index 0000000..8fb2f70 --- /dev/null +++ b/real_python/randasync.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# rand.py + +import asyncio +import random + +# ANSI colors +c = ( + "\033[0m", # End of color + "\033[36m", # Cyan + "\033[91m", # Red + "\033[35m", # Magenta +) + + +async def makerandom(idx: int, threshold: int = 6) -> int: + print(c[idx + 1] + f"Initiated makerandom({idx}).") + i = random.randint(0, 10) + while i <= threshold: + print(c[idx + 1] + f"makerandom({idx}) == {i} too low; retrying.") + await asyncio.sleep(idx + 1) + i = random.randint(0, 10) + print(c[idx + 1] + f"---> Finished: makerandom({idx}) == {i}" + c[0]) + return i + + +async def main(): + res = await asyncio.gather(*(makerandom(i, 10 - i - 1) for i in range(3))) + return res + + +if __name__ == "__main__": + random.seed(444) + r1, r2, r3 = asyncio.run(main()) + print() + print(f"r1: {r1}, r2: {r2}, r3: {r3}")