Slide 1

Slide 1 text

⏱ Await for it mixing async and blocking code

Slide 2

Slide 2 text

Who am I? Sebastián Ramírez github.com/tiangolo linkedin.com/in/tiangolo twitter.com/tiangolo tiangolo.com Berlin, Germany I created:

Slide 3

Slide 3 text

⏱ async / await - concurrency @tiangolo

Slide 4

Slide 4 text

⏱ async / await - concurrency @tiangolo

Slide 5

Slide 5 text

⏱ async / await - concurrency @tiangolo

Slide 6

Slide 6 text

⏱ async / await - concurrency Photo by John Cameron: https://unsplash.com/@john_cameron @tiangolo

Slide 7

Slide 7 text

⏱ async / await - concurrency @tiangolo

Slide 8

Slide 8 text

⏱ async / await - concurrency @tiangolo

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

⏱ async / await - concurrency @tiangolo

Slide 11

Slide 11 text

@tiangolo 📕 So many terms!

Slide 12

Slide 12 text

📕 Concurrency and Parallelism @tiangolo ● Parallel: multiple at the same time ● Concurrent: multiple during the same period of time ○ Normally in turns

Slide 13

Slide 13 text

● 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

Slide 14

Slide 14 text

● 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

Slide 15

Slide 15 text

📕 So many terms! @tiangolo

Slide 16

Slide 16 text

@tiangolo 🌪 Using Async

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

󰥟 Async rules @tiangolo

Slide 19

Slide 19 text

󰥟 Async rules @tiangolo

Slide 20

Slide 20 text

🌪 Run an async program with AnyIO @tiangolo

Slide 21

Slide 21 text

🌪 Run an async program with AnyIO @tiangolo

Slide 22

Slide 22 text

@tiangolo 🌪 Async with FastAPI and HTTPX

Slide 23

Slide 23 text

🌪 Async with FastAPI and HTTPX @tiangolo

Slide 24

Slide 24 text

🌪 Async with FastAPI and HTTPX @tiangolo

Slide 25

Slide 25 text

🌪 Async with FastAPI and HTTPX @tiangolo

Slide 26

Slide 26 text

🌪 Async with FastAPI and HTTPX, or not @tiangolo

Slide 27

Slide 27 text

🌪 Async with FastAPI and HTTPX optional, or not @tiangolo

Slide 28

Slide 28 text

@tiangolo 🌪 Concurrency in your code

Slide 29

Slide 29 text

Multiple async function calls - not concurrent ⛔ @tiangolo

Slide 30

Slide 30 text

Multiple async function calls - not concurrent ⛔ @tiangolo

Slide 31

Slide 31 text

🌪 Multiple async function calls - concurrent @tiangolo

Slide 32

Slide 32 text

🔍 Asyncer soonify - inline errors @tiangolo

Slide 33

Slide 33 text

🔍 Asyncer soonify - autocompletion @tiangolo

Slide 34

Slide 34 text

🔍 Asyncer soonify - concurrent work @tiangolo

Slide 35

Slide 35 text

🎁 Return values @tiangolo

Slide 36

Slide 36 text

🎁 Asyncer - Soon Values @tiangolo

Slide 37

Slide 37 text

🎁 Asyncer - Soon Values - autocompletion @tiangolo

Slide 38

Slide 38 text

🎁 Asyncer - Soon Values - inline errors @tiangolo

Slide 39

Slide 39 text

🤓 Autocompletion and inline errors everywhere @tiangolo

Slide 40

Slide 40 text

😎 Async with Asyncer @tiangolo

Slide 41

Slide 41 text

@tiangolo 󰠼 Blocking sync code in async

Slide 42

Slide 42 text

󰠼 Blocking sync code in async - don't do this! ⛔ @tiangolo

Slide 43

Slide 43 text

󰠼 Blocking sync code in async - don't do this! ⛔ @tiangolo

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

󰥟 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

Slide 46

Slide 46 text

🌪 Blocking sync code in async - asyncify @tiangolo

Slide 47

Slide 47 text

🔍 asyncify - inline errors @tiangolo

Slide 48

Slide 48 text

󰩂 asyncify - autocompletion @tiangolo

Slide 49

Slide 49 text

󰩂 Blocking sync code in async @tiangolo

Slide 50

Slide 50 text

@tiangolo 🌪 Async in blocking sync in async

Slide 51

Slide 51 text

🌪 Async in blocking sync in async - syncify @tiangolo

Slide 52

Slide 52 text

󰩂 syncify - autocompletion - inline errors @tiangolo

Slide 53

Slide 53 text

🌪 Async in sync in async… @tiangolo

Slide 54

Slide 54 text

🌪 Async in blocking sync, dual - syncify @tiangolo

Slide 55

Slide 55 text

🌪 Async in blocking sync, dual - syncify @tiangolo ⛔ Warning: performance in mainly sync code won't be the same as async.

Slide 56

Slide 56 text

🌪 Async in blocking sync, dual - syncify @tiangolo

Slide 57

Slide 57 text

@tiangolo Libraries, what to use ❓

Slide 58

Slide 58 text

● 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

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Thank you! Sebastián Ramírez tiangolo.com fastapi.tiangolo.com github.com/tiangolo linkedin.com/in/tiangolo twitter.com/tiangolo