pub struct StoppableThreadPool<PoolError>{ /* 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>
impl<PoolError> StoppableThreadPool<PoolError>
Sourcepub fn new() -> Result<StoppableThreadPool<PoolError>, Error>
pub fn new() -> Result<StoppableThreadPool<PoolError>, Error>
Create a new StoppableThreadPool instance using a default futures ThreadPool executor instance.
Sourcepub fn new_with_pool(pool: ThreadPool) -> StoppableThreadPool<PoolError>
pub fn new_with_pool(pool: ThreadPool) -> StoppableThreadPool<PoolError>
Create a new StoppableThreadPool instance using a user supplied futures ThreadPool executor instance.
Sourcepub fn with_pool(&mut self, pool: ThreadPool) -> &mut Self
pub fn with_pool(&mut self, pool: ThreadPool) -> &mut Self
Change the underlying futures ThreadPool executor instance.
Sourcepub async fn observe(&self) -> Result<(), PoolError>
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.