[][src]Struct async_executor::Executor

pub struct Executor { /* fields omitted */ }

Multi-threaded executor.

The executor does not spawn threads on its own. Instead, you need to call Executor::run() on manually spawned executor threads.

Examples

use async_channel::unbounded;
use async_executor::Executor;
use easy_parallel::Parallel;
use futures_lite::future;

let ex = Executor::new();
let (signal, shutdown) = unbounded::<()>();

Parallel::new()
    // Run four executor threads.
    .each(0..4, |_| ex.run(shutdown.recv()))
    // Run the main future on the current thread.
    .finish(|| future::block_on(async {
        println!("Hello world!");
        drop(signal);
    }));

Implementations

impl Executor[src]

pub fn new() -> Executor[src]

Creates a multi-threaded executor.

Examples

use async_executor::Executor;

let ex = Executor::new();

pub fn spawner(&self) -> Spawner[src]

Creates a spawner for this executor.

Examples

use async_executor::Executor;

let ex = Executor::new();
let spawner = ex.spawner();

pub fn spawn<T: Send + 'static>(
    &self,
    future: impl Future<Output = T> + Send + 'static
) -> Task<T>

Important traits for Task<T>

impl<T> Future for Task<T> type Output = T;
[src]

Spawns a task onto the executor.

Examples

use async_executor::Executor;

let ex = Executor::new();

let task = ex.spawn(async {
    println!("Hello world");
});

pub fn enter<T>(&self, f: impl FnOnce() -> T) -> T[src]

Enters the context of an executor.

Examples

use async_executor::{Executor, Task};

let ex = Executor::new();

ex.enter(|| {
    // `Task::spawn()` now knows which executor to spawn onto.
    let task = Task::spawn(async {
        println!("Hello world");
    });
});

pub fn run<T>(&self, future: impl Future<Output = T>) -> T[src]

Runs the executor until the given future completes.

Examples

use async_executor::Executor;

let ex = Executor::new();

let task = ex.spawn(async { 1 + 2 });
let res = ex.run(async { task.await * 2 });

assert_eq!(res, 6);

Trait Implementations

impl Debug for Executor[src]

impl Default for Executor[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

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

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.

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.