Crate scheduled_executor [] [src]

The library

This library provides a series of utilities for scheduling and executing tasks (functions and closures). Tasks can be executed at fixed interval or at fixed rates, and can be executed sequentially in the main executor thread or in parallel using a thread pool.

Executors

Task group

The scheduled-executor crate also provides an abstraction for the execution of groups of tasks called TaskGroup. A TaskGroup requires a method for the generation of the collection of tasks, which will be executed at the beginning of each cycle, and a method for the execution of individual task, which will be executed for each task.

To see a task group in action, check out the task_group.rs example.

Documentation

Examples

Scheduling periodic task is very simple. Here is an example using a thread pool:

// Starts a new thread-pool based executor with 4 threads
let executor = ThreadPoolExecutor::new(4)?;

executor.schedule_fixed_rate(
    Duration::from_secs(2),  // Wait 2 seconds before scheduling the first task
    Duration::from_secs(5),  // and schedule every following task at 5 seconds intervals
    |remote| {
        // Code to be scheduled. The code will run on one of the threads in the thread pool.
        // The `remote` handle can be used to schedule additional work on the event loop,
        // if needed.
    },
);

Reexports

pub use executor::CoreExecutor;
pub use executor::ThreadPoolExecutor;
pub use task_group::TaskGroup;
pub use task_group::TaskGroupScheduler;

Modules

executor

Executors allow easy scheduling and execution of functions or closures. The CoreExecutor uses a single thread for scheduling and execution, while the ThreadPoolExecutor uses multiple threads to execute the function. Internally, each executor uses a tokio_core::reactor::Core as event loop, that will drive the scheduling of the functions (and for the CoreExecutor, also their execution). A reference to the event loop is passed to every function when executed, allowing it to register additional events if needed.

task_group

Task groups can be used whenever there is a sequence of tasks that need to be executed at regular intervals, and the sequence can change across different cycles.