Skip to main content
deleted 15 characters in body
Source Link
Eric Aya
  • 70.2k
  • 36
  • 190
  • 266

Disclaimer this is 2022 Python 3.10

This is Python 3.10

  1. 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 

Disclaimer this is 2022 Python 3.10

  1. 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 

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 
Source Link
Pieter
  • 2.2k
  • 19
  • 20

Disclaimer this is 2022 Python 3.10

  1. 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