Back at it again

master
Drew Bednar 3 years ago
parent 762804fb70
commit a36d3b86f8

@ -0,0 +1,3 @@
# Asyncio in python3
The example here primarily come from the Asyncio in Python3 book by Caleb Hattingh.

@ -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()

@ -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.
Loading…
Cancel
Save