pub struct Pool(/* private fields */);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
impl Pool
Sourcepub fn add_permits(&self, n: usize)
pub fn add_permits(&self, n: usize)
Add N permits to the underlying semaphore
Sourcepub fn forget_permits(&self, n: usize) -> usize
pub fn forget_permits(&self, n: usize) -> usize
Remove N permits from the underlying semaphore, return the actual number removed.
Sourcepub fn spawn<F>(&self, future: F) -> Handle<F::Output>
pub fn spawn<F>(&self, future: F) -> Handle<F::Output>
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).
Sourcepub fn spawn_blocking<F, R>(&self, f: F) -> Handle<Result<R>>
Available on crate feature coroutine-heavy only.
pub fn spawn_blocking<F, R>(&self, f: F) -> Handle<Result<R>>
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.