Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Pycon ZA
October 08, 2020
200

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.

Pycon ZA

October 08, 2020
Tweet

More Decks by Pycon ZA

Transcript

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

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

  3. 11 Cooperative scheduling • Can be implemented at the application

    level • Everything must be non-blocking Cooperation
  4. 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
  5. 14 Today's Agenda − How do threads and asyncio work?

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

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

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

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

    to use • Can cut through solid wood
  10. 24 ​ Because cooperative scheduling takes place at the application

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