Select
Select 연산은 여러 future들 모두에 대해서 준비될 때 까지 기다리다가, 그 중 어떤 한 future가 최초로 준비 상태가 되면 해당 future의 결과값을 리턴합니다. 이것은 자바스크립트에서의 Promise.race
와 비슷합니다. 파이썬에서라면 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
-
In this example, we have a race between a cat and a dog.
first_animal_to_finish_race
listens to both channels and will pick whichever arrives first. Since the dog takes 50ms, it wins against the cat that take 500ms. -
You can use
oneshot
channels in this example as the channels are supposed to receive only onesend
. -
Try adding a deadline to the race, demonstrating selecting different sorts of futures.
-
Note that
select!
drops unmatched branches, which cancels their futures. It is easiest to use when every execution ofselect!
creates new futures.- 대안은 future 자체 대신
&mut future
를 전달하는 것입니다. 하지만 이렇게 하면 문제가 발생할 수 있습니다(Pinning을 다룰 때 자세히 설명할 예정임).
- 대안은 future 자체 대신