futures_cpupool

Struct CpuPool

Source
pub struct CpuPool { /* private fields */ }
Expand description

A thread pool intended to run CPU intensive work.

This thread pool will hand out futures representing the completed work that happens on the thread pool itself, and the futures can then be later composed with other work as part of an overall computation.

The worker threads associated with a thread pool are kept alive so long as there is an open handle to the CpuPool or there is work running on them. Once all work has been drained and all references have gone away the worker threads will be shut down.

Currently CpuPool implements Clone which just clones a new reference to the underlying thread pool.

Note: if you use CpuPool inside a library it’s better accept a Builder object for thread configuration rather than configuring just pool size. This not only future proof for other settings but also allows user to attach monitoring tools to lifecycle hooks.

Implementations§

Source§

impl CpuPool

Source

pub fn new(size: usize) -> CpuPool

Creates a new thread pool with size worker threads associated with it.

The returned handle can use execute to run work on this thread pool, and clones can be made of it to get multiple references to the same thread pool.

This is a shortcut for:

Builder::new().pool_size(size).create()
§Panics

Panics if size == 0.

Source

pub fn new_num_cpus() -> CpuPool

Creates a new thread pool with a number of workers equal to the number of CPUs on the host.

This is a shortcut for:

Builder::new().create()
Source

pub fn spawn<F>(&self, f: F) -> CpuFuture<F::Item, F::Error>
where F: Future + Send + 'static, F::Item: Send + 'static, F::Error: Send + 'static,

Spawns a future to run on this thread pool, returning a future representing the produced value.

This function will execute the future f on the associated thread pool, and return a future representing the finished computation. The returned future serves as a proxy to the computation that F is running.

To simply run an arbitrary closure on a thread pool and extract the result, you can use the future::lazy combinator to defer work to executing on the thread pool itself.

Note that if the future f panics it will be caught by default and the returned future will propagate the panic. That is, panics will not tear down the thread pool and will be propagated to the returned future’s poll method if queried.

If the returned future is dropped then this CpuPool will attempt to cancel the computation, if possible. That is, if the computation is in the middle of working, it will be interrupted when possible.

Source

pub fn spawn_fn<F, R>(&self, f: F) -> CpuFuture<R::Item, R::Error>
where F: FnOnce() -> R + Send + 'static, R: IntoFuture + 'static, R::Future: Send + 'static, R::Item: Send + 'static, R::Error: Send + 'static,

Spawns a closure on this thread pool.

This function is a convenience wrapper around the spawn function above for running a closure wrapped in future::lazy. It will spawn the function f provided onto the thread pool, and continue to run the future returned by f on the thread pool as well.

The returned future will be a handle to the result produced by the future that f returns.

Trait Implementations§

Source§

impl Clone for CpuPool

Source§

fn clone(&self) -> CpuPool

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CpuPool

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for CpuPool

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<F> Executor<F> for CpuPool
where F: Future<Item = (), Error = ()> + Send + 'static,

Source§

fn execute(&self, future: F) -> Result<(), ExecuteError<F>>

Spawns a future to run on this Executor, typically in the “background”. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.