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

impl<Q, CS> AsyncExecutor<Q, CS> where
    Q: 'static + TimeoutQueue<Item = AsyncTask> + Send + Sync,
    CS: ConcurrentSystem<()>, 
[src]

pub fn new(task_queue: Q) -> Self[src]

Creates a new executor from a given queue

pub fn queue_from<T>(from: T) -> Self where
    Q: From<T>, 
[src]

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

pub fn handle(&self) -> ExecutorHandle<Q>[src]

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

pub fn local_handle(&self) -> LocalExecutorHandle<Q>[src]

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.

pub fn submit(&self, future: impl Future<Output = ()> + 'static)[src]

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.

pub fn submit_loop<SQ, Func, Fut>(
    &self,
    future_func: Func,
    delay: Duration,
    sleep_runner: impl Deref<Target = SleepFutureRunner<SQ, CS>> + 'static
) where
    SQ: 'static + TimeoutQueue<Item = SleepMessage<CS>> + Send + Sync,
    Func: FnMut() -> Fut + 'static,
    Fut: Future<Output = ()>, 
[src]

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.

pub fn run(&self, stop: impl Deref<Target = AtomicBool>)[src]

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

Trait Implementations

impl<Q: Debug, CS: Debug> Debug for AsyncExecutor<Q, CS>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<Q, CS> RefUnwindSafe for AsyncExecutor<Q, CS> where
    Q: RefUnwindSafe

impl<Q, CS> !Send for AsyncExecutor<Q, CS>

impl<Q, CS> !Sync for AsyncExecutor<Q, CS>

impl<Q, CS> Unpin for AsyncExecutor<Q, CS>

impl<Q, CS> UnwindSafe for AsyncExecutor<Q, CS> where
    Q: RefUnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.