#callback #scheduler #async

sched-callback

Rust library for async callback scheduling

3 releases

0.1.2 May 25, 2024
0.1.1 May 19, 2024
0.1.0 May 18, 2024

#29 in #callback

39 downloads per month

MIT license

19KB
376 lines

sched-callback

A scheduler that executes async callback at certain point.

Overview

  • Works on tokio runtime.
  • Lightweight scheduler that only one task is executed in one task queue.

Usage

Create scheduler using queue::SchedQueue

let sq = SchedQueue::new();

Callback type:

type Callback = Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + 'static>;

Add task with callback. Callback will be triggered 1 second after the task is added, and will be rescheduled for 10 times after the callback has been triggered.

sq.add(Task::new(SchedType::Delay(Duration::from_secs(1), 10), Box::new(move || {
    Box::pin(async move {
        println!("hello world");
    })
}))).await;

Two types of task can be added to queue. SchedType::Timestamp(SystemTime) specifies the exact timestamp that the callback will be triggered at. SchedType::Delay(Duration, usize) specifies when the callback will be triggered after the task is added and how many times will it be rescheduled.

Dependencies

~2.2–8MB
~63K SLoC