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.

Ed9e9992fe069a0d3de05b69d8d187c3?s=128

Matthias Endler

September 24, 2019
Tweet

Transcript

  1. How does
 Async 
 work? 1

  2. ~ Matthias Endler ~ 2

  3. ~ Matthias Endler ~ Backend engineer at trivago 2

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

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

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

    Rust, Python Interested in low level stuff endler.dev and Hello-rust.show 2
  7. 3

  8. 3

  9. 4 move

  10. 4 move

  11. 4 move

  12. 4 move

  13. ~ Python ~ 5

  14. 6

  15. 7

  16. 7

  17. 8

  18. 9

  19. 10

  20. 10

  21. Python's asyncio * coroutines * tasks * event loop *

    select 11
  22. 12

  23. 13

  24. 13 coroutine

  25. 14 coroutine asyncio.gather( )

  26. 14 coroutine Task

  27. 15 asyncio.run( )

  28. 15 Task

  29. 16

  30. 16

  31. 17

  32. 18 select kernel userspace

  33. 18 select kernel userspace

  34. 18 select kernel userspace

  35. 18 select kernel userspace

  36. 18 select kernel userspace

  37. 18 select kernel userspace

  38. 18 select kernel userspace

  39. 18 select kernel userspace

  40. 19

  41. 20

  42. 21

  43. 21

  44. ~ JavaScript ~ 22

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

  46. JavaScript Callbacks * The problem * The solution 24

  47. 25

  48. 25

  49. 26

  50. 26 v8

  51. 26 v8 Spidermonkey

  52. 26 v8 Spidermonkey Chakra core

  53. 27 Heap Stack Web apis setTimeout dom ajax callback queue

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

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

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

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

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

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

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

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

    Event
 loop
  62. 32 Stack Web apis setTimeout dom ajax callback queue Event


    loop
  63. 33 Stack Web apis setTimeout dom ajax callback queue Event


    loop main
  64. 34 Stack Web apis setTimeout dom ajax callback queue Event


    loop main console.log
  65. 35 Stack Web apis setTimeout dom ajax callback queue Event


    loop One main
  66. 36 Stack Web apis setTimeout dom ajax callback queue Event


    loop One main SetTimeout
  67. 37 Stack Web apis setTimeout dom ajax callback queue Event


    loop main cb One
  68. 37 Stack Web apis setTimeout dom ajax callback queue Event


    loop main cb One
  69. 38 Stack Web apis setTimeout dom ajax callback queue Event


    loop One main cb console.log
  70. 39 Stack Web apis setTimeout dom ajax callback queue Event


    loop One three cb
  71. 40 Stack Web apis setTimeout dom ajax callback queue Event


    loop One three cb
  72. 40 Stack Web apis setTimeout dom ajax callback queue Event


    loop One three cb
  73. 41 Stack Web apis setTimeout dom ajax callback queue Event


    loop One three two
  74. 42 v8 Spidermonkey Chakra core

  75. 42 v8 Spidermonkey Chakra core Node.js

  76. 43

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

  78. 45

  79. 46

  80. 47

  81. ~ Rust ~ 48

  82. What is Rust? Empowering everyoneto build
 reliable and efficient software.

    49
  83. What is Rust? * Systems programming language * Started at

    Mozilla (firefox) * used by Amazon/Microsoft/Dropbox... * Main goals: safety, performance * fearless concurrency 50
  84. 51

  85. 52 Future

  86. 53

  87. 54

  88. 55

  89. 56 start open print error

  90. 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)
  91. 58

  92. Futures in Rust 59 mio tokio futures async/await select/epoll/kqueue Operating

    system external crates standard library
  93. Summary * asyncio in Python is based on coroutines *

    async in javascript is based on callbacks * Rust's futures are zero-cost 60
  94. Credits * Pixel backgrounds by slance.com * Truck and engine

    logos by Pixelart maker * Jake Archibald - in the loop 61