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

Await for it: mixing async and blocking code - ...

Await for it: mixing async and blocking code - PyCon MEA - Dubai

Tips and tricks to properly mix async code with blocking code, using modern libraries, and taking advantage of the new concurrency features of the language while keeping compatibility with already existing code and libraries.

It will include the basics of what concurrency is, how to take advantage of it in the simplest use cases, and how to safely mix it with regular code.

You've heard async is great, it can improve concurrency performance, but you might have heard that you need to use only async code and use only async-compatible libraries... but the good news is, that's not entirely true.

So, how can you take advantage of these new async features in your current projects that already use blocking code and require some blocking libraries?

In this talk, I'll show you the basics of writing concurrent async code, and how to properly mix it with regular code without affecting performance (without blocking the event loop) by using thread workers underneath. All done in a simple and convenient way using modern libraries.

Sebastián Ramírez

October 19, 2023
Tweet

More Decks by Sebastián Ramírez

Other Decks in Technology

Transcript

  1. ⏱ async / await - concurrency Photo by John Cameron:

    https://unsplash.com/@john_cameron @tiangolo
  2. ⏱ async / await - concurrency Photo by Kate Townsend:

    https://unsplash.com/@k8townsend @tiangolo
  3. 📕 Concurrency and Parallelism @tiangolo • Parallel: multiple at the

    same time • Concurrent: multiple during the same period of time ◦ Normally in turns
  4. • A lot of waiting? (e.g. multiple users) ◦ Concurrency

    • A lot of the same CPU work, in multiple cores or GPUs? (e.g. image processing) ◦ Parallelism 📕 When concurrency, when parallelism @tiangolo
  5. • Throughput: amount of items passing through. E.g. requests per

    second. ◦ Concurrency helps. • Latency: how long each individual item takes. ◦ Code optimization ◦ Numpy ◦ GPUs ◦ mypyc ◦ Cython ◦ Rust 📕 Performance and Speed: throughput or latency @tiangolo
  6. 󰥟 Async rules @tiangolo • You can only use await

    inside async functions. • To call an async function, you need to use await in front of it.
  7. 󰥟 Blocking sync code in async - performance rules @tiangolo

    • Do not call blocking sync code in async code directly. • Convert it to awaitable. • Run on another thread.
  8. 󰥟 What is blocking sync code @tiangolo • Doesn't use

    async • Requires waiting for something • Interacts with something outside your code and variables ◦ Network ◦ External APIs ◦ External databases ◦ Files on disk • Or a lot of CPU for a long time
  9. 🌪 Async in blocking sync, dual - syncify @tiangolo ⛔

    Warning: performance in mainly sync code won't be the same as async.
  10. • Use AnyIO ◦ Compatible with both Asyncio and Trio

    ◦ Structured concurrency (task groups) • Maybe try Asyncer ◦ AnyIO + types: autocompletion, inline errors • Trio ◦ Structured concurrency (task groups) • Asyncio ◦ Comes with Python ◦ Next versions with task groups ❓ Libraries, what to use @tiangolo