#background-task #async #future #pause #async-task #pausable

pausable_future

Pausable and resumable future, useful in background tasks

2 unstable releases

0.2.0 Jul 18, 2024
0.1.0 Apr 18, 2024

#517 in Asynchronous

Download history 7/week @ 2024-07-26 15/week @ 2024-08-02 11/week @ 2024-08-09 12/week @ 2024-08-16 2/week @ 2024-08-23 9/week @ 2024-08-30 2/week @ 2024-09-06 22/week @ 2024-09-13 24/week @ 2024-09-20 21/week @ 2024-09-27 12/week @ 2024-10-04 5/week @ 2024-10-11 18/week @ 2024-10-18 5/week @ 2024-10-25 41/week @ 2024-11-01 28/week @ 2024-11-08

92 downloads per month

MIT license

7KB
83 lines

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

Dependencies

~175KB