9.2. AsyncIO About

  • asyncio in Python standard library

  • async and await builtin keywords

  • Running asynchronously: 3s + 1s + 1s = bit over 3s [execution time]

  • Async is the future of programming

9.2.1. Advantages

  • Maximize the usage of a single thread

  • Handling I/O asynchronously

  • Enabling concurrent code using coroutines

  • Async will fill the gaps, otherwise wasted on waiting for I/O

  • You control when tasks switches occur, so locks and other synchronization are no longer needed

  • Async is the cheapest way to task switch

  • Cost task switches is incredibly low; calling a pure Python function has more overhead than restarting a generator or awaitable

  • Function builds stack each time it's called, whereas async uses generators underneath, which already has stack created

  • In terms of speed async servers blows threaded servers in means of thousands

  • Async is very cheap in means of resources

  • Async world has a huge ecosystem of support tools

  • Coding is easier to get right, than threads

9.2.2. Disadvantages

  • Async switches cooperatively, so you do need to add explicit code yield or await to cause a task to switch

  • Everything you do need a non-blocking version (for example open())

  • Increased learning curve

  • Create event loop, acquire, crate non-blocking versions of your code

  • You think you know Python, there is a second half to learn (async)

9.2.3. Sync vs Async

../../_images/asyncio-sequence-sync.png

Figure 9.5. Source: Michael Kennedy 1

../../_images/asyncio-sequence-async.png

Figure 9.6. Source: Michael Kennedy 1

../../_images/concurrency-sync-vs-async-1.png

Figure 9.7. Source: Langa, Ł. import asyncio: Learn Python's AsyncIO 4

../../_images/concurrency-sync-vs-async-2.png

Figure 9.8. Source: Langa, Ł. import asyncio: Learn Python's AsyncIO 4

9.2.4. Execution

../../_images/asyncio-execution-sync.png

Figure 9.9. Source: Michael Kennedy 1

../../_images/asyncio-execution-async.png

Figure 9.10. Source: Michael Kennedy 1

9.2.5. Example

>>> import asyncio
>>>
>>>
>>> async def a():
...     print('a: started')
...     await asyncio.sleep(0.2)
...     print('a: finished')
...     return 'a'
>>>
>>> async def b():
...     print('b: started')
...     await asyncio.sleep(0.1)
...     print('b: finished')
...     return 'b'
>>>
>>> async def c():
...     print('c: started')
...     await asyncio.sleep(0.3)
...     print('c: finished')
...     return 'c'
>>>
>>> async def main():
...     result = await asyncio.gather(a(), b(), c())
...     print(f'Result: {result}')
>>>
>>>
>>> asyncio.run(main())
a: started
b: started
c: started
b: finished
a: finished
c: finished
Result: ['a', 'b', 'c']

9.2.6. Further Reading

  • Kennedy, M. Demystifying Python's Async and Await Keywords 1

  • Kennedy, M. Async Techniques and Examples in Python 2

  • Abdalla, A. Creating a Bittorrent Client using Asyncio 3

  • Langa, Ł. import asyncio: Learn Python's AsyncIO 4

9.2.7. References

1(1,2,3,4,5)

Kennedy, M. Demystifying Python's Async and Await Keywords. Publisher: JetBrainsTV. Year: 2019. Retrieved: 2022-03-10. URL: https://www.youtube.com/watch?v=F19R_M4Nay4

2

Kennedy, M. Async Techniques and Examples in Python Course. Publisher: TalkPython. Year: 2022. Retrieved: 2022-03-10. URL: https://talkpython.fm/async

3

Abdalla, A. Creating a Bittorrent Client using Asyncio. Year: 2017. Retrieved: 2022-03-10. URL: https://www.youtube.com/watch?v=Pe3b9bdRtiE

4(1,2,3)

Langa, Ł. import asyncio: Learn Python's AsyncIO. Year: 2020. Retrieved: 2022-03-10. URL: https://www.youtube.com/playlist?list=PLhNSoGM2ik6SIkVGXWBwerucXjgP1rHmB