Disclaimer this is 2022 Python 3.10
This is Python 3.10
asyncio is single threaded execution, using await to yield the cpu to other function until what is await'ed is done.
import asyncio async def my_func(t): print("Start my_func") await asyncio.sleep(t) # The await yields cpu, while we wait print("Exit my_func") async def main(): asyncio.ensure_future(my_func(10)) # Schedules on event loop, we might want to save the returned future to later check for completion. print("Start main") await asyncio.sleep(1) # The await yields cpu, giving my_func chance to start. print("running other stuff") await asyncio.sleep(15) print("Exit main") if __name__ == "__main__": asyncio.run(main()) # Starts event loop
asyncio is single threaded execution, using await to yield the cpu to other function until what is await'ed is done.
import asyncio async def my_func(t): print("Start my_func") await asyncio.sleep(t) # The await yields cpu, while we wait print("Exit my_func") async def main(): asyncio.ensure_future(my_func(10)) # Schedules on event loop, we might want to save the returned future to later check for completion. print("Start main") await asyncio.sleep(1) # The await yields cpu, giving my_func chance to start. print("running other stuff") await asyncio.sleep(15) print("Exit main") if __name__ == "__main__": asyncio.run(main()) # Starts event loop