Expand description
§tokio-interval-set
A lightweight utility for running a closure repeatedly on a fixed time
interval, backed by tokio’s time and task system.
§Example
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::interval;
use tokio_interval_set::set_interval;
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();
rt.block_on(async {
let count = Arc::new(Mutex::new(0u64));
let count_cloned = Arc::clone(&count);
let handle = set_interval(interval(Duration::from_millis(100)), move || {
let count = Arc::clone(&count_cloned);
async move {
let mut c = count.lock().await;
*c += 1;
}
});
tokio::time::sleep(Duration::from_millis(350)).await;
handle.clear(); // stop the interval
let final_count = *count.lock().await;
assert!(final_count >= 3, "expected at least 3 ticks, got {}", final_count);
});Structs§
- Interval
Handle - A handle that controls a running interval task.
Functions§
- set_
interval - Runs
funcrepeatedly on the giveninterval.