the talk you’ve been .await-ing for€¦ · async/await is simpler syntax for futures* a future...

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

Upload: others

Post on 02-Oct-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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

Page 2: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 3: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 4: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 5: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo(s: String) -> i32 { // …}

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

Page 6: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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

Page 7: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async/await and Futures

Page 8: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 9: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 10: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 11: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 12: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 13: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Async/await is simpler syntax for Futures

Page 14: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Async/await is simpler syntax for Futures*

Page 15: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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

Page 16: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Let’s build a future!

Page 17: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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

Page 18: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 19: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 20: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 21: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 22: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Four rulesFor using async/await

Page 23: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo(s: String) -> i32 { // …}

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

Page 24: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

If you have a Future<Output=i32> and you want an i32, use

.await on it

Page 25: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

You can only .await inside of an async fn or

block

Page 26: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

To start executing a Future, you pass it to an

executor

Page 27: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 28: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 29: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 30: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Generators aka stackless coroutines

Page 31: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Generators are not stable

… yet

Page 32: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 33: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 34: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 35: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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

Page 36: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Tasks, Executors, & Reactors

Page 37: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

“The event loop”

Page 38: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Task: a unit of work to execute, a chain of Futures

Executor: schedules tasks

Reactor: notifies the executor that tasks are ready to execute

Page 39: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Interface to the reactor

Executor calls poll, and provides a context

Page 40: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Let’s build an executor!

Page 41: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

Page 42: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Page 43: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Executortask queue

Page 44: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Executortask queue

Calls poll() on the Future

Page 45: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Executortask queue

Calls poll() on the Future

Page 46: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Executortask queue

Calls poll() on the Future

Future calls wake() (reactor)

Page 47: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

async fn foo() { // …}

spawner.spawn(foo())

Executortask queue

Calls poll() on the Future

Future calls wake() (reactor)

Page 48: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 49: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 50: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 51: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 52: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 53: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 54: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

A quick aside about Pin<P>

Page 55: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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)

Page 56: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

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.

Page 57: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Let’s build a reactor!

Page 58: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

(We’re not gonna build a reactor)

Page 59: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

(We technically did build a reactor)

Page 60: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

Bonus round: async fn in traits

Page 61: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 62: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

A function is only one function

A trait is implemented for many types, and so

is many functions

Page 63: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 64: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 65: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

It gets way more complicated

Page 66: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 67: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 68: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 69: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!

It gets way way way more complicated

Page 70: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 71: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!
Page 72: The Talk you’ve been .await-ing for€¦ · Async/await is simpler syntax for Futures* A Future represents a value that will exist sometime in the future. Let’s build a future!