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