Crates.io | pausable_future |
lib.rs | pausable_future |
version | 0.2.0 |
source | src |
created_at | 2024-04-18 15:55:54.801881 |
updated_at | 2024-07-18 02:52:51.612023 |
description | Pausable and resumable future, useful in background tasks. |
homepage | |
repository | https://github.com/xuxiaocheng0201/pausable_future/ |
max_upload_size | |
id | 1212691 |
size | 7,742 |
Read this in other languages: English, 简体中文.
Pausable and resumable future/stream, useful in background tasks.
Add this to your 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;
}