为什么Future::select先选择睡眠时间更长的未来呢?

Why does Future::select choose the future with a longer sleep period first?

我正在尝试理解Future::select:在这个例子中,首先返回具有较长时间延迟的未来。

当我阅读 this article 及其示例时,我出现了认知失调。作者写道:

The select function runs two (or more in case of select_all) futures and returns the first one coming to completion. This is useful for implementing timeouts.

好像没看懂select的意思。

extern crate futures; // v0.1 (old)
extern crate tokio_core;

use std::thread;
use std::time::Duration;
use futures::{Async, Future};
use tokio_core::reactor::Core;

struct Timeout {
    time: u32,
}

impl Timeout {
    fn new(period: u32) -> Timeout {
        Timeout { time: period }
    }
}

impl Future for Timeout {
    type Item = u32;
    type Error = String;

    fn poll(&mut self) -> Result<Async<u32>, Self::Error> {
        thread::sleep(Duration::from_secs(self.time as u64));
        println!("Timeout is done with time {}.", self.time);
        Ok(Async::Ready(self.time))
    }
}

fn main() {
    let mut reactor = Core::new().unwrap();

    let time_out1 = Timeout::new(5);
    let time_out2 = Timeout::new(1);

    let task = time_out1.select(time_out2);

    let mut reactor = Core::new().unwrap();
    reactor.run(task);
}

我需要处理时间延迟较小的早期未来,然后处理延迟较长的未来。我该怎么做?

TL;DR: 使用 tokio::time

如果有一点可以避免:从不在异步操作中执行阻塞或长运行操作.

如果你想要超时,使用来自tokio::time, such as delay_for or timeout的东西:

use futures::future::{self, Either}; // 0.3.1
use std::time::Duration;
use tokio::time; // 0.2.9

#[tokio::main]
async fn main() {
    let time_out1 = time::delay_for(Duration::from_secs(5));
    let time_out2 = time::delay_for(Duration::from_secs(1));

    match future::select(time_out1, time_out2).await {
        Either::Left(_) => println!("Timer 1 finished"),
        Either::Right(_) => println!("Timer 2 finished"),
    }
}

有什么问题?

要理解为什么会出现这种行为,您必须从高层次理解期货的实施。

当您调用 run 时,有一个循环会在传入的 future 上调用 poll。它循环直到未来 returns 成功或失败,否则未来尚未完成。

您对 poll "locks up" 的执行此循环持续 5 秒,因为没有什么可以中断对 sleep 的调用。当睡眠完成时,未来已经准备就绪,因此选择了未来。

异步超时的实现在概念上是通过在每次轮询时检查时钟来判断是否已经过了足够的时间。

最大的区别在于,当一个未来 returns 还没有准备好时,可以检查 另一个未来 。这就是 select 所做的!

戏剧性重演:

基于睡眠的定时器

core: Hey select, are you ready to go?

select: Hey future1, are you ready to go?

future1: Hold on a seconnnnnnnn [... 5 seconds pass ...] nnnnd. Yes!

基于异步的简单计时器

core: Hey select, are you ready to go?

select: Hey future1, are you ready to go?

future1: Checks watch No.

select: Hey future2, are you ready to go?

future2: Checks watch No.

core: Hey select, are you ready to go?

[... polling continues ...]

[... 1 second passes ...]

core: Hey select, are you ready to go?

select: Hey future1, are you ready to go?

future1: Checks watch No.

select: Hey future2, are you ready to go?

future2: Checks watch Yes!

这个简单的实现会一遍又一遍地轮询未来,直到它们全部完成。这不是最有效的,也不是大多数执行者所做的。

有关此类执行器的实现,请参阅

基于异步的智能定时器

core: Hey select, are you ready to go?

select: Hey future1, are you ready to go?

future1: Checks watch No, but I'll call you when something changes.

select: Hey future2, are you ready to go?

future2: Checks watch No, but I'll call you when something changes.

[... core stops polling ...]

[... 1 second passes ...]

future2: Hey core, something changed.

core: Hey select, are you ready to go?

select: Hey future1, are you ready to go?

future1: Checks watch No.

select: Hey future2, are you ready to go?

future2: Checks watch Yes!

这个更有效的实现在轮询时将 waker 交给每个未来。当未来还没有准备好时,它会保存那个唤醒者以备后用。当事情发生变化时,唤醒者通知执行者的核心,现在是重新检查期货的好时机。这允许执行程序不执行有效的忙等待。

通用解决方案

当您有一个阻塞或长操作时 运行,那么适当的做法是将该工作移出异步循环。有关详细信息和示例,请参阅