From a36d3b86f8d05abf3b5d3a3e97d705792dda3319 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Sun, 13 Feb 2022 09:44:36 -0500 Subject: [PATCH] Back at it again --- asyncio_in_python3_book/README.md | 3 ++ asyncio_in_python3_book/quickstart_exe.py | 36 +++++++++++++++++++ fluent_python_content/README.md | 5 +++ app.py => fluent_python_content/app.py | 0 .../async_spinner.py | 0 .../coroutine_chaining.py | 0 .../hello_coroutine.py | 0 .../sleep_gather.py | 0 .../sync_socket.py | 0 9 files changed, 44 insertions(+) create mode 100644 asyncio_in_python3_book/README.md create mode 100644 asyncio_in_python3_book/quickstart_exe.py create mode 100644 fluent_python_content/README.md rename app.py => fluent_python_content/app.py (100%) rename async_spinner.py => fluent_python_content/async_spinner.py (100%) rename coroutine_chaining.py => fluent_python_content/coroutine_chaining.py (100%) rename hello_coroutine.py => fluent_python_content/hello_coroutine.py (100%) rename sleep_gather.py => fluent_python_content/sleep_gather.py (100%) rename sync_socket.py => fluent_python_content/sync_socket.py (100%) diff --git a/asyncio_in_python3_book/README.md b/asyncio_in_python3_book/README.md new file mode 100644 index 0000000..b477ac0 --- /dev/null +++ b/asyncio_in_python3_book/README.md @@ -0,0 +1,3 @@ +# Asyncio in python3 + +The example here primarily come from the Asyncio in Python3 book by Caleb Hattingh. diff --git a/asyncio_in_python3_book/quickstart_exe.py b/asyncio_in_python3_book/quickstart_exe.py new file mode 100644 index 0000000..8115b8c --- /dev/null +++ b/asyncio_in_python3_book/quickstart_exe.py @@ -0,0 +1,36 @@ +import asyncio +import time + + +async def main(): + print(f"{time.ctime()} Hello!") + await asyncio.sleep(1.0) + print(f"{time.ctime()} Goodbye!") + loop.stop() + + +def blocking(): + # This code only works because our blocking sleep is less than the main functions Asyncio sleep call. If + # this function used here was time.sleep(2) then we would get a RuntimeError because the eventloop was + # Already closed when the Future's job completed + time.sleep(0.5) + print(f"{time.ctime()} Hello from thread!") + + +loop = asyncio.get_event_loop() + +loop.create_task(main()) +# Returns a asyncio Future not a asyncio Task +loop.run_in_executor(None, blocking) + +loop.run_forever() + +# Deprecated Style +# pending = asyncio.Task.all_tasks(loop=loop) +pending = asyncio.all_tasks(loop=loop) + +# asyncio.gather also returns a future +group = asyncio.gather(*pending) +print(f"The type returned of asyncio.gather is: {type(group)}") +loop.run_until_complete(group) +loop.close() diff --git a/fluent_python_content/README.md b/fluent_python_content/README.md new file mode 100644 index 0000000..88cafdc --- /dev/null +++ b/fluent_python_content/README.md @@ -0,0 +1,5 @@ +# Readme + +I don't know where this content came from. It was 4 years old when I last committed it. Might have +been from the Fluent Python book. + diff --git a/app.py b/fluent_python_content/app.py similarity index 100% rename from app.py rename to fluent_python_content/app.py diff --git a/async_spinner.py b/fluent_python_content/async_spinner.py similarity index 100% rename from async_spinner.py rename to fluent_python_content/async_spinner.py diff --git a/coroutine_chaining.py b/fluent_python_content/coroutine_chaining.py similarity index 100% rename from coroutine_chaining.py rename to fluent_python_content/coroutine_chaining.py diff --git a/hello_coroutine.py b/fluent_python_content/hello_coroutine.py similarity index 100% rename from hello_coroutine.py rename to fluent_python_content/hello_coroutine.py diff --git a/sleep_gather.py b/fluent_python_content/sleep_gather.py similarity index 100% rename from sleep_gather.py rename to fluent_python_content/sleep_gather.py diff --git a/sync_socket.py b/fluent_python_content/sync_socket.py similarity index 100% rename from sync_socket.py rename to fluent_python_content/sync_socket.py