pub struct LocalPool { /* private fields */ }
Expand description

A single-threaded task pool for polling futures to completion.

This executor allows you to multiplex any number of tasks onto a single thread. It’s appropriate to poll strictly I/O-bound futures that do very little work in between I/O actions.

To get a handle to the pool that implements Spawn, use the spawner() method. Because the executor is single-threaded, it supports a special form of task spawning for non-Send futures, via spawn_local_obj.

Implementations§

Create a new, empty pool of tasks.

Get a clonable handle to the pool as a Spawn.

Run all tasks in the pool to completion.

use futures::executor::LocalPool;

let mut pool = LocalPool::new();

// ... spawn some initial tasks using `spawn.spawn()` or `spawn.spawn_local()`

// run *all* tasks in the pool to completion, including any newly-spawned ones.
pool.run();

The function will block the calling thread until all tasks in the pool are complete, including any spawned while running existing tasks.

Runs all the tasks in the pool until the given future completes.

use futures::executor::LocalPool;

let mut pool = LocalPool::new();

// run tasks in the pool until `my_app` completes
pool.run_until(my_app);

The function will block the calling thread only until the future f completes; there may still be incomplete tasks in the pool, which will be inert after the call completes, but can continue with further use of one of the pool’s run or poll methods. While the function is running, however, all tasks in the pool will try to make progress.

Runs all tasks and returns after completing one future or until no more progress can be made. Returns true if one future was completed, false otherwise.

use futures::executor::LocalPool;
use futures::task::LocalSpawnExt;
use futures::future::{ready, pending};

let mut pool = LocalPool::new();
let spawner = pool.spawner();

spawner.spawn_local(ready(())).unwrap();
spawner.spawn_local(ready(())).unwrap();
spawner.spawn_local(pending()).unwrap();

// Run the two ready tasks and return true for them.
pool.try_run_one(); // returns true after completing one of the ready futures
pool.try_run_one(); // returns true after completing the other ready future

// the remaining task can not be completed
assert!(!pool.try_run_one()); // returns false

This function will not block the calling thread and will return the moment that there are no tasks left for which progress can be made or after exactly one task was completed; Remaining incomplete tasks in the pool can continue with further use of one of the pool’s run or poll methods. Though only one task will be completed, progress may be made on multiple tasks.

Runs all tasks in the pool and returns if no more progress can be made on any task.

use futures::executor::LocalPool;
use futures::task::LocalSpawnExt;
use futures::future::{ready, pending};

let mut pool = LocalPool::new();
let spawner = pool.spawner();

spawner.spawn_local(ready(())).unwrap();
spawner.spawn_local(ready(())).unwrap();
spawner.spawn_local(pending()).unwrap();

// Runs the two ready task and returns.
// The empty task remains in the pool.
pool.run_until_stalled();

This function will not block the calling thread and will return the moment that there are no tasks left for which progress can be made; remaining incomplete tasks in the pool can continue with further use of one of the pool’s run or poll methods. While the function is running, all tasks in the pool will try to make progress.

Trait Implementations§

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
TODO: once 1.33.0 is the minimum supported compiler version, remove Any::type_id_compat and use StdAny::type_id instead. https://github.com/rust-lang/rust/issues/27745
The archived version of the pointer metadata for this type.
Converts some archived metadata to the pointer metadata for itself.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Deserializes using the given deserializer

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
The type for metadata in pointers and references to Self.
Should always be Self
The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Checks if self is actually part of its subset T (and can be converted to it).
Use with care! Same as self.to_subset but without any property checks. Always succeeds.
The inclusion map: converts self to the equivalent element of its superset.
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more