Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Talk You've Been Await-ing For

62 views

Published on

Video and slides synchronized, mp3 and slide download available at URL https://bit.ly/344PnB1.

Steve Klabnik goes over the deep details of how async/await works in Rust, covering concepts like coroutines, generators, stack-less vs stack-ful, "pinning", and more. Filmed at qconsf.com.

Steve Klabnik is on the core team of Rust, leads the documentation team, and is an author of "The Rust Programming Language." He is a frequent speaker at conferences and is a prolific open source contributor, previously working on projects such as Ruby and Ruby on Rails.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

The Talk You've Been Await-ing For

  1. 1. The Talk you’ve been .await-ing for @steveklabnik
  2. 2. InfoQ.com: News & Community Site • Over 1,000,000 software developers, architects and CTOs read the site world- wide every month • 250,000 senior developers subscribe to our weekly newsletter • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • 2 dedicated podcast channels: The InfoQ Podcast, with a focus on Architecture and The Engineering Culture Podcast, with a focus on building • 96 deep dives on innovative topics packed as downloadable emags and minibooks • Over 40 new content items per week Watch the video with slide synchronization on InfoQ.com! https://www.infoq.com/presentations/ rust-async-await/
  3. 3. Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide Presented at QCon San Francisco www.qconsf.com
  4. 4. async fn foo(s: String) -> i32 { // … } fn foo(s: String) -> impl Future<Output=i32> { // … }
  5. 5. Stuff we’re going to talk about ● async/await and Futures ● Generators: the secret sauce ● Tasks, Executors, & Reactors, oh my! ● … maybe async fn in traits
  6. 6. async/await and Futures
  7. 7. Async/await is simpler syntax for Futures
  8. 8. Async/await is simpler syntax for Futures*
  9. 9. A Future represents a value that will exist sometime in the future
  10. 10. Let’s build a future!
  11. 11. A timer future ● Mutex around a boolean ● Spins up a new thread that sleeps for some amount of time ● When the thread wakes up, it sets the boolean to true and ‘wakes up’ the future ● Calls to poll check the boolean to see if we’re done
  12. 12. Four rules For using async/await
  13. 13. async fn foo(s: String) -> i32 { // … } fn foo(s: String) -> impl Future<Output=i32> { // … }
  14. 14. If you have a Future<Output=i32> and you want an i32, use .await on it
  15. 15. You can only .await inside of an async fn or block
  16. 16. To start executing a Future, you pass it to an executor
  17. 17. Generators aka stackless coroutines
  18. 18. Generators are not stable … yet
  19. 19. Futures need to have poll() called over and over until a value is produced Generators let you call yield over and over to get values async/await is a simpler syntax for a generator that implements the Future trait
  20. 20. Tasks, Executors, & Reactors
  21. 21. “The event loop”
  22. 22. Task: a unit of work to execute, a chain of Futures Executor: schedules tasks Reactor: notifies the executor that tasks are ready to execute
  23. 23. Interface to the reactor Executor calls poll, and provides a context
  24. 24. Let’s build an executor!
  25. 25. async fn foo() { // … }
  26. 26. async fn foo() { // … } spawner.spawn(foo())
  27. 27. async fn foo() { // … } spawner.spawn(foo()) Executor task queue
  28. 28. async fn foo() { // … } spawner.spawn(foo()) Executor task queue Calls poll() on the Future
  29. 29. async fn foo() { // … } spawner.spawn(foo()) Executor task queue Calls poll() on the Future
  30. 30. async fn foo() { // … } spawner.spawn(foo()) Executor task queue Calls poll() on the Future Future calls wake() (reactor)
  31. 31. async fn foo() { // … } spawner.spawn(foo()) Executor task queue Calls poll() on the Future Future calls wake() (reactor)
  32. 32. A quick aside about Pin<P>
  33. 33. Before a future starts executing, we need to be able to move it around in memory. (For example, to create a task out of it, we need to move it to the heap) Once a future starts executing, it must not move in memory. (otherwise, borrows in the body of the future would become invalid)
  34. 34. When you turn some sort of pointer type into a Pin<P>, you’re promising that what the pointer to will no longer move. Box<T> turns into Pin<Box<T>> There’s an extra trait, “Unpin”, that says “I don’t care about this”, similar to how Copy says “I don’t care about move semantics.
  35. 35. Let’s build a reactor!
  36. 36. (We’re not gonna build a reactor)
  37. 37. (We technically did build a reactor)
  38. 38. Bonus round: async fn in traits
  39. 39. A function is only one function A trait is implemented for many types, and so is many functions
  40. 40. It gets way more complicated
  41. 41. It gets way way way more complicated
  42. 42. Thanks! @steveklabnik ● https://rust-lang.github.io/async-book ● https://tmandry.gitlab.io/blog/posts/optimizing-await- 1/ ● https://smallcultfollowing.com/babysteps/blog/2019/ 10/26/async-fn-in-traits-are-hard/
  43. 43. Watch the video with slide synchronization on InfoQ.com! https://www.infoq.com/presentations/ rust-async-await/

×