Struct single_executor::AsyncExecutor[][src]

pub struct AsyncExecutor<Q, CS> { /* fields omitted */ }
Expand description

An asynchronous executor that can be used to run multiple async tasks. All user code runs in a single thread becasue the v5 is single threaded. Blocked tasks will stop running and wait to be unblocked while also not blocking the main thread.

Panics

This will panic if Q::try_push ever fails.

Example

use concurrency_traits::StdThreadFunctions;
use concurrency_traits::queue::ParkQueueStd;
use single_executor::{SleepFutureRunner, spawn_blocking, AsyncExecutorStd};
use std::rc::Rc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::thread::sleep;
use std::time::Duration;

let executor = AsyncExecutorStd::new(ParkQueueStd::default());
let sleep_runner = Rc::new(SleepFutureRunner::new(ParkQueueStd::default()));

let sleep_runner_clone = sleep_runner.clone();
let loop_function = move ||{
    let sleep_runner_clone = sleep_runner_clone.clone();
    async move {
        // dummy code but shows how you can await
        sleep_runner_clone.sleep_for(Duration::from_millis(100)).await;
        // Do stuff
    }
};
executor.submit_loop(
    loop_function,
    Duration::from_millis(10),
    sleep_runner
);

/// Dummy function
async fn get_something_from_io(){}
executor.submit(get_something_from_io());

/// Dummy blocking function
fn block_for_a_while() -> usize{
    std::thread::sleep(Duration::from_millis(100));
    100
}
executor.submit(async {
    assert_eq!(spawn_blocking::<_, _, StdThreadFunctions>(block_for_a_while).0.await, 100);
});

// Nothing runs until run is called on the executor
let stop = Arc::new(AtomicBool::new(false));
let stop_clone = stop.clone();
thread::spawn(move || {
    sleep(Duration::from_secs(1));
    stop_clone.store(true, Ordering::Relaxed);
});
executor.run(stop); // Keeps running until stop is set to true

MAKE SURE NONE OF YOUR SUBMISSIONS BLOCK OR YOUR WHOLE PROGRAM WILL COME CRASHING DOWN!

Implementations

Creates a new executor from a given queue

Creates a new executor from Q’s From<T> implementation. Usually used for converting from an initial size.

Gets a handle to the executor through which tasks can be submitted.

Gets a handle to the executor through which tasks can be submitted. This handle may not be sent across threads but may submit !Send futures.

Adds a new future to the executor. This can be called from within a future. If this is a long running future (like a loop) then make use of sleep or use spawn_loop instead.

Adds a new future that will be called at a set rate. Do not do a min loop inside the future, this function handles that for you.

Runs the executor, must be called or no futures will run.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.