Threads vs Async: Has Asyncio Solved Concurrency? by Jacob Unna

7b0645f018c0bddc8ce3900ccc3ba70c?s=47 Pycon ZA
October 08, 2020
14

Threads vs Async: Has Asyncio Solved Concurrency? by Jacob Unna

In recent years asyncio has garnered much attention as a faster and easier alternative to threads. So do threads still have a place in the world? And what of other concurrency tech such as greenlets?

The fundamental problem solved by both async frameworks and threads is one of scheduling. This talk will put aside nitty gritty implementation details and go back to basics, examining how a cooperative scheduler (used by async frameworks) differs from a preemptive scheduler (used by threads). This basic understanding will clarify what is often considered a tricky subject and shed light on the pros and cons of each approach.

We will also see that even having decided on the async model, asyncio is not the only tool available. We will explore the pros and cons of greenlets, curio and trio as alternatives to the standard library's solution.

This talk is suitable for all levels from beginner to expert. The format will be simple and intuitive, with basic live examples of how to use threads and asyncio to do everyday tasks. You will come away not with an encyclopaedic knowledge of a specific toolchain, but with a deeper appreciation of the core concepts that power concurrency.

7b0645f018c0bddc8ce3900ccc3ba70c?s=128

Pycon ZA

October 08, 2020
Tweet

Transcript

  1. 1 PyCon ZA 2020 Has Asyncio Solved Concurrency?

  2. 2 ​ Jacob Unna ​ Software Engineer @ Deloitte Analytics

    & Cognitive ​ junna@deloitte.co.uk @JacobUnna
  3. 3 What is Concurrency?

  4. 4 Concurrency in Python − Threads − Asyncio

  5. 5 Concurrency in Python − Threads − Asyncio − How

    do they work? − Which is better? − Does asyncio spell the end for threads?
  6. 6 Scheduling

  7. 7 Scheduling

  8. 8

  9. 9 Cooperative Scheduling Asyncio

  10. 10 Cooperative scheduling • Can be implemented at the application

    level • Everything must be non-blocking
  11. 11 Cooperative scheduling • Can be implemented at the application

    level • Everything must be non-blocking Cooperation
  12. 12 Preemptive Scheduling Threads

  13. 13 Preemptive scheduling • Run any two bits of code

    at the same time • Handled by the operating system − Wouldn't be possible using pure Python − Operating system preempts running code i.e. freezes it mid-flow and switches the CPU's attention to something else • Doesn't get stuck on uncooperative code
  14. 14 Today's Agenda − How do threads and asyncio work?

    − Which is better? − Does asyncio spell the end for threads?
  15. 15 Which is better? Threads Asyncio Overhead when switching task

    High Low
  16. 16 Which is better? Threads Asyncio Overhead when switching task

    High Low Easy to reason about No Yes
  17. 17 Which is better? Threads Asyncio Overhead when switching task

    High Low Easy to reason about No Yes Handle blocking code Yes No
  18. 18 Which is better? Threads Asyncio Overhead when switching task

    High Low Easy to reason about No Yes Handle blocking code Yes No
  19. 19 Today's Agenda − How do threads and asyncio work?

    − Which is better? − Does asyncio spell the end for threads?
  20. 20 Which is better? • Cheap • Safe • Easy

    to use • Can cut through solid wood
  21. 21 Can I do this with asyncio? Decision flow Use

    asyncio Use threads Yes No
  22. 22 ​ Asyncio ≠ Cooperative Scheduling

  23. 23 ​ Because cooperative scheduling takes place at the application

    level, it is easy to create new frameworks
  24. 24 ​ Because cooperative scheduling takes place at the application

    level, it is easy to create new frameworks ​ (Relative to creating a new operating system)
  25. 25 Asyncio

  26. 26 Gevent Tornado uvloop Twisted Asyncio PyZMQ Curio Trio Diesel

  27. 27 Gevent Tornado uvloop Twisted Asyncio PyZMQ Curio Trio Diesel

  28. 28 ​ Questions? ​ Jacob Unna ​ junna@deloitte.co.uk @JacobUnna Code:

    git.io/JU6FT