Skip to content

Latest commit

 

History

History
55 lines (42 loc) · 1.67 KB

README_zh.md

File metadata and controls

55 lines (42 loc) · 1.67 KB

可暂停的未来

Crate GitHub last commit GitHub issues GitHub pull requests GitHub

其他语言版本:English, 简体中文.

描述

一个可暂停和恢复的Future/Stream,在后台任务中非常有用。

用法

将以下内容添加到你的Cargo.toml

[dependencies]
pausable_future = "~0.2"

示例

use std::time::Duration;

use pausable_future::Pausable;
use tokio::time::sleep;

#[tokio::main]
async fn main() {
    let pausable = Pausable::new(async {
        let mut count = 0;
        loop {
            sleep(Duration::from_millis(300)).await;
            count += 1;
            println!("count: {}", count);
        }
    });
    let controller = pausable.controller();
    tokio::spawn(pausable);
    println!("spawn");
    sleep(Duration::from_secs(1)).await;
    controller.pause();
    println!("paused");
    sleep(Duration::from_secs(1)).await;
    controller.resume();
    println!("resumed");
    sleep(Duration::from_secs(1)).await;
}