跳到主要内容

sleep_until

搜索

函数 sleep_until 

Source
pub fn sleep_until(deadline: Instant) -> Sleep 
展开描述

等待直到 deadline 截止时刻到达。

在等待 sleep future 完成时 不执行任何工作。 Sleep 以毫秒粒度运行, 不应用于 需要高分辨率计时器的任务。

若要按计划定期运行某些东西, 请参阅 interval

§Cancellation

取消 sleep 实例的方法是丢弃返回的 future。不需要额外的清理工作。

§示例

等待 100 毫秒并打印 "100 毫秒已过"。

use tokio::time::{sleep_until, Instant, Duration};

sleep_until(Instant::now() + Duration::from_millis(100)).await;
println!("100 ms have elapsed");

有关更多示例, 请参阅 Sleep 类型的文档。

§Panics

如果未设置当前定时器,则此函数会 panic。

Builder::enable_timeBuilder::enable_all 未包含在构建器中时, 可能会触发 panic。

It can also panic whenever a timer is created outside of a Tokio 运行时。 That is why rt.block_on(sleep(...)) will panic, since the function is executed outside of the runtime. Whereas rt.block_on(async {sleep(...).await}) doesn’t panic. And this is because wrapping the function on an async makes it lazy, and so gets executed inside the runtime successfully without panicking.