Skip to content

xuxiaocheng0201/pausable_future

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pausable Future

Crate GitHub last commit GitHub issues GitHub pull requests GitHub

Read this in other languages: English, 简体中文.

Description

Pausable and resumable future/stream, useful in background tasks.

Usage

Add this to your Cargo.toml:

[dependencies]
pausable_future = "~0.2"

Example

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;
}

About

Pausable and resumable future/stream, useful in background tasks.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages