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

How Does Async Work?

How Does Async Work?

If you work with Node.js or Python you might have heard of asynchronous programming a.k.a. async/await.
It lets you do something else while you wait on some work to be done in the background - like reading a file or sending some data over the network.
But what is it, really? And are there differences between the programming languages?
Let's go on a trip through async-land and discover callbacks, event loops, and state machines.
We will look at Node's, Python's, and Rust's async features and how they work under the hood.

Matthias Endler

September 24, 2019
Tweet

More Decks by Matthias Endler

Other Decks in Technology

Transcript

  1. How does

    Async 

    work?
    1

    View full-size slide

  2. ~ Matthias Endler ~
    2

    View full-size slide

  3. ~ Matthias Endler ~
    Backend engineer at trivago
    2

    View full-size slide

  4. ~ Matthias Endler ~
    Backend engineer at trivago
    Mostly Go, Rust, Python
    2

    View full-size slide

  5. ~ Matthias Endler ~
    Backend engineer at trivago
    Mostly Go, Rust, Python
    Interested in low level stuff
    2

    View full-size slide

  6. ~ Matthias Endler ~
    Backend engineer at trivago
    Mostly Go, Rust, Python
    Interested in low level stuff
    endler.dev and Hello-rust.show
    2

    View full-size slide

  7. Python's asyncio
    * coroutines
    * tasks
    * event loop
    * select
    11

    View full-size slide

  8. 14
    coroutine
    asyncio.gather( )

    View full-size slide

  9. 14
    coroutine
    Task

    View full-size slide

  10. 15
    asyncio.run( )

    View full-size slide

  11. 18
    select
    kernel
    userspace

    View full-size slide

  12. 18
    select
    kernel
    userspace

    View full-size slide

  13. 18
    select
    kernel
    userspace

    View full-size slide

  14. 18
    select
    kernel
    userspace

    View full-size slide

  15. 18
    select
    kernel
    userspace

    View full-size slide

  16. 18
    select
    kernel
    userspace

    View full-size slide

  17. 18
    select
    kernel
    userspace

    View full-size slide

  18. 18
    select
    kernel
    userspace

    View full-size slide

  19. ~ JavaScript ~
    22

    View full-size slide

  20. Async JavaScript
    * Callbacks
    * Promises
    * async/await
    23

    View full-size slide

  21. JavaScript Callbacks
    * The problem
    * The solution
    24

    View full-size slide

  22. 26
    v8 Spidermonkey

    View full-size slide

  23. 26
    v8 Spidermonkey Chakra core

    View full-size slide

  24. 27
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  25. 28
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  26. 28
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  27. 29
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  28. 29
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  29. 30
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  30. 30
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  31. 31
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  32. 31
    Heap Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  33. 32
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop

    View full-size slide

  34. 33
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    main

    View full-size slide

  35. 34
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    main
    console.log

    View full-size slide

  36. 35
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    main

    View full-size slide

  37. 36
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    main
    SetTimeout

    View full-size slide

  38. 37
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    main cb
    One

    View full-size slide

  39. 37
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    main cb
    One

    View full-size slide

  40. 38
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    main cb
    console.log

    View full-size slide

  41. 39
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    three
    cb

    View full-size slide

  42. 40
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    three
    cb

    View full-size slide

  43. 40
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    three
    cb

    View full-size slide

  44. 41
    Stack
    Web apis
    setTimeout
    dom
    ajax
    callback queue
    Event

    loop
    One
    three
    two

    View full-size slide

  45. 42
    v8 Spidermonkey Chakra core

    View full-size slide

  46. 42
    v8 Spidermonkey Chakra core
    Node.js

    View full-size slide

  47. 44
    Everything You Need to Know About Node.js Event Loop

    View full-size slide

  48. What is Rust?
    Empowering everyoneto build

    reliable and efficient software.
    49

    View full-size slide

  49. What is Rust?
    * Systems programming language
    * Started at Mozilla (firefox)
    * used by Amazon/Microsoft/Dropbox...
    * Main goals: safety, performance
    * fearless concurrency
    50

    View full-size slide

  50. 56
    start open
    print
    error

    View full-size slide

  51. requests/s
    57
    2.000.000
    1.500.000
    1.000.000
    500.000
    0
    Rust

    (minihttp)
    java

    (rapidoid)
    go

    (fasthttp)
    go

    (stdlib)
    node.js

    (stdlib)

    View full-size slide

  52. Futures in Rust
    59
    mio
    tokio
    futures
    async/await
    select/epoll/kqueue
    Operating system
    external crates
    standard library

    View full-size slide

  53. Summary
    * asyncio in Python is based on coroutines
    * async in javascript is based on
    callbacks
    * Rust's futures are zero-cost
    60

    View full-size slide

  54. Credits
    * Pixel backgrounds by slance.com
    * Truck and engine logos by Pixelart maker
    * Jake Archibald - in the loop
    61

    View full-size slide