the talk you ve been await ing for
play

The Talk youve been .await-ing for @steveklabnik async fn foo(s: - PowerPoint PPT Presentation

The Talk youve been .await-ing for @steveklabnik async fn foo(s: String) -> i32 { // } fn foo(s: String) -> impl Future<Output=i32> { // } Stuff were going to talk about async/await and Futures Generators:


  1. The Talk you’ve been .await-ing for @steveklabnik

  2. async fn foo(s: String) -> i32 { // … } fn foo(s: String) -> impl Future<Output=i32> { // … }

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

  4. async/await and Futures

  5. Async/await is simpler syntax for Futures

  6. Async/await is simpler syntax for Futures*

  7. A Future represents a value that will exist sometime in the future

  8. Let’s build a future!

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

  10. Four rules For using async/await

  11. async fn foo(s: String) -> i32 { // … } fn foo(s: String) -> impl Future<Output=i32> { // … }

  12. If you have a Future<Output=i32> and you want an i32 , use .await on it

  13. You can only .await inside of an async fn or block

  14. To start executing a Future , you pass it to an executor

  15. Generators aka stackless coroutines

  16. Generators are not stable … yet

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

  18. Tasks, Executors, & Reactors

  19. “The event loop”

  20. Task: a unit of work to execute, a chain of Future s Executor: schedules tasks Reactor: notifies the executor that tasks are ready to execute

  21. Executor calls poll, and provides a context Interface to the reactor

  22. Let’s build an executor!

  23. async fn foo() { // … }

  24. async fn foo() { spawner.spawn(foo()) // … }

  25. async fn foo() { spawner.spawn(foo()) // … } Executor task queue

  26. async fn foo() { spawner.spawn(foo()) // … } Executor task queue Calls poll() on the Future

  27. async fn foo() { spawner.spawn(foo()) // … } Executor task queue Calls poll() on the Future

  28. async fn foo() { spawner.spawn(foo()) // … } Executor task queue Calls poll() on Future calls the Future wake() (reactor)

  29. async fn foo() { spawner.spawn(foo()) // … } Executor task queue Calls poll() on Future calls the Future wake() (reactor)

  30. A quick aside about Pin<P>

  31. 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)

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

  33. Let’s build a reactor!

  34. (We’re not gonna build a reactor)

  35. (We technically did build a reactor)

  36. Bonus round: async fn in traits

  37. A function is only one function A trait is implemented for many types, and so is many functions

  38. It gets way more complicated

  39. It gets way way way more complicated

  40. ● https://rust-lang.github.io/async-book Thanks! ● https://tmandry.gitlab.io/blog/posts/optimizing-await- 1/ ● https://smallcultfollowing.com/babysteps/blog/2019/ @steveklabnik 10/26/async-fn-in-traits-are-hard/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend