【发布时间】:2019-08-14 09:34:00
【问题描述】:
我正在尝试使用tokio:timer:Timeout 在我的 RPC 请求中引入超时:
use std::time::{Duration, Instant};
use tokio::prelude::*;
use tokio::timer::Delay;
fn main() {
let when = Instant::now() + Duration::from_millis(4000);
let task = Delay::new(when)
.and_then(|_| {
println!("Hello world!");
Ok(())
})
.map_err(|e| panic!("delay errored; err={:?}", e));
let task_with_timeout = task
.timeout(Duration::from_millis(3000))
.map_err(|e| println!("Timeout hit {:?}", e));
let _ = task_with_timeout.wait().expect("Failure");
// tokio::run(task_with_timeout);
}
如果我用tokio::run() 运行我的future_with_timeout,它会按预期工作。
但是,在 task_with_timeout 上调用 wait 会导致 task 未来出现错误:
thread 'main' panicked at 'delay errored; err=Error(Shutdown)'
而不是得到
Timeout hit Error(Elapsed)
我不明白这里使用tokio::run() 和wait() 之间的区别。
如何使用wait 使代码工作?
【问题讨论】:
标签: rust rust-tokio