Select
A select operation waits until any of a set of futures is ready, and responds to
that future's result. In JavaScript, this is similar to Promise.race
. In
Python, it compares to
asyncio.wait(task_set, return_when=asyncio.FIRST_COMPLETED)
.
Similar to a match statement, the body of select!
has a number of arms, each
of the form pattern = future => statement
. When a future
is ready, its
return value is destructured by the pattern
. The statement
is then run with
the resulting variables. The statement
result becomes the result of the
select!
macro.
Speaker Notes
This slide should take about 5 minutes.
-
The
listener
async block here is a common form: wait for some async event, or for a timeout. Change thesleep
to sleep longer to see it fail. Why does thesend
also fail in this situation? -
select!
is also often used in a loop in "actor" architectures, where a task reacts to events in a loop. That has some pitfalls, which will be discussed in the next segment.