Skip to main content

Pool

Struct Pool 

Source
pub struct Pool(/* private fields */);
Available on crate feature coroutine only.
Expand description

A pool for limiting async tasks

The pool is not to be used as a thread pool. It is used more as a “limiter” to ensure we only fire a limited number of resource-intensive tasks. The IO work for those tasks are drived by a single monitoring thread.

This is good for things like spawning compiler processes where the IO between child and parent processes are low, but not good for spawning CPU-bound tasks. Use something like rayon for CPU parallelism.

The pool can be cloned and shared between threads. Dropping the pool will not cause the spawned tasks to be either joined to canceled. You must use individual handles to join them.

This is constructed with cu::co::pool, since it’s shorter than cu::co::Pool::new(n).

Implementations§

Source§

impl Pool

Source

pub fn add_permits(&self, n: usize)

Add N permits to the underlying semaphore

Source

pub fn forget_permits(&self, n: usize) -> usize

Remove N permits from the underlying semaphore, return the actual number removed.

Source

pub fn spawn<F>(&self, future: F) -> Handle<F::Output>
where F: Future + Send + 'static, F::Output: Send + 'static,

Spawn a task using the background runtime

The task is spawned with cu::co::spawn, and will only start being executed when the pool has availability (permits).

Source

pub fn spawn_blocking<F, R>(&self, f: F) -> Handle<Result<R>>
where F: FnOnce() -> R + Send + 'static, R: Send + 'static,

Available on crate feature coroutine-heavy only.

Spawn a synchronous task using the background runtime

A wait task will be spawned with cu::co::spawn, and the function will only be spawned with cu::co::spawn_blocking when the pool has availability (permits).

§Performance

Even though it’s never recommended to block the async runtime, if the synchronous work is short, it might be more performant to use spawn instead.

Trait Implementations§

Source§

impl Clone for Pool

Source§

fn clone(&self) -> Pool

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Pool

§

impl RefUnwindSafe for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

§

impl UnwindSafe for Pool

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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.

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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 = !

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.