StoppableThreadPool

Struct StoppableThreadPool 

Source
pub struct StoppableThreadPool<PoolError>
where PoolError: Send + Sync + 'static,
{ /* private fields */ }
Expand description

Added functionality for the futures::executor::ThreadPool futures executor.

Futures will be spawned to and executed by the internal and exchangeable ThreadPool instance, but in such a way that all spawned futures are asked to stop on user request or in case any of them returns an error.

A notable difference to futures:executor::ThreadPool is that the user spawns futures of type Output<Result(),T> here instead of type Output<()>.

Caveats: If you do not call observe().await once all desired futures are spawned or if you spawn additional futures after the first observe().await the stopping mechanism won’t work. In other words, instances cannot be “reused” after they were being observed for the first time. For now no measures are in place to prevent a user from doing this (maybe in a future version).

Also note that spawned tasks can not be cancelled instantly. They will stop executing the next time they yield to the executor.

Implementations§

Source§

impl<PoolError> StoppableThreadPool<PoolError>
where PoolError: Send + Sync + 'static,

Source

pub fn new() -> Result<StoppableThreadPool<PoolError>, Error>

Create a new StoppableThreadPool instance using a default futures ThreadPool executor instance.

Source

pub fn new_with_pool(pool: ThreadPool) -> StoppableThreadPool<PoolError>

Create a new StoppableThreadPool instance using a user supplied futures ThreadPool executor instance.

Source

pub fn with_pool(&mut self, pool: ThreadPool) -> &mut Self

Change the underlying futures ThreadPool executor instance.

Source

pub fn spawn<Fut>(&mut self, future: Fut) -> &mut Self
where Fut: Future<Output = Result<(), PoolError>> + Send + 'static,

Start executing a future right away.

Source

pub async fn observe(&self) -> Result<(), PoolError>

Ensure that all spawned tasks are canceled on individual task error or any stop() request issued by the user. Call this function once all tasks are spawned. A task that fails before a call to observe() is being awaited will still trigger a stop as soon as you actually start awaiting here.

Source

pub async fn stop(&self, why: PoolError)

Stop the execution of all spawned tasks.

Auto Trait Implementations§

§

impl<PoolError> Freeze for StoppableThreadPool<PoolError>

§

impl<PoolError> RefUnwindSafe for StoppableThreadPool<PoolError>

§

impl<PoolError> Send for StoppableThreadPool<PoolError>

§

impl<PoolError> Sync for StoppableThreadPool<PoolError>

§

impl<PoolError> Unpin for StoppableThreadPool<PoolError>

§

impl<PoolError> UnwindSafe for StoppableThreadPool<PoolError>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.