pub struct RuntimeHandle { /* private fields */ }Expand description
Handle for spawning tasks onto a runtime from outside async context.
Cheap to clone (shared handle backing). Use Runtime::handle to obtain one.
let runtime = RuntimeBuilder::new().build()?;
let handle = runtime.handle();
// Spawn from any thread.
let join = handle.spawn(async { compute_result().await });
let result = runtime.block_on(join);Implementations§
Source§impl RuntimeHandle
impl RuntimeHandle
Sourcepub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output> ⓘ
Spawn a task from outside async context.
Panics if the runtime is no longer available or if the root region
rejects admission. Use RuntimeHandle::try_spawn to handle those
failures explicitly.
Sourcepub fn try_spawn<F>(
&self,
future: F,
) -> Result<JoinHandle<F::Output>, SpawnError>
pub fn try_spawn<F>( &self, future: F, ) -> Result<JoinHandle<F::Output>, SpawnError>
Spawn a task from outside async context, returning runtime-availability or admission errors instead of panicking.
Sourcepub fn spawn_with_cx<F, Fut>(&self, f: F)
pub fn spawn_with_cx<F, Fut>(&self, f: F)
Spawn a task with a Cx from outside async context.
Creates a child Cx in the runtime’s root region and passes it to the factory closure. The Cx participates in structured cancellation: it will observe cancellation when the runtime shuts down.
Panics if the runtime is no longer available or if the root region
rejects admission. Use RuntimeHandle::try_spawn_with_cx to handle
those failures explicitly.
§Example
let handle = runtime.handle();
handle.spawn_with_cx(|cx| async move {
while !cx.is_cancel_requested() {
// do work
}
});Sourcepub fn try_spawn_with_cx<F, Fut>(&self, f: F) -> Result<(), SpawnError>
pub fn try_spawn_with_cx<F, Fut>(&self, f: F) -> Result<(), SpawnError>
Spawn a task with a Cx from outside async context,
returning runtime-availability or admission errors instead of panicking.
Creates a child Cx in the runtime’s root region and passes it to the factory closure. The Cx participates in structured cancellation: it will observe cancellation when the runtime shuts down.
§Example
let handle = runtime.handle();
handle.try_spawn_with_cx(|cx| async move {
while !cx.is_cancel_requested() {
// do work
}
})?;Sourcepub fn spawn_blocking<F>(&self, f: F) -> Option<BlockingTaskHandle>
pub fn spawn_blocking<F>(&self, f: F) -> Option<BlockingTaskHandle>
Spawns a blocking task on the blocking pool.
Returns None if the blocking pool is not configured or if this handle
is a stale weak handle whose runtime has already been dropped.
Sourcepub fn spawn_blocking_on_cohort<F>(
&self,
cohort: usize,
f: F,
) -> Option<BlockingTaskHandle>
pub fn spawn_blocking_on_cohort<F>( &self, cohort: usize, f: F, ) -> Option<BlockingTaskHandle>
Spawns a blocking task with an explicit preferred cohort hint.
Returns None if the blocking pool is not configured or if this handle
is stale.
Sourcepub fn blocking_handle(&self) -> Option<BlockingPoolHandle>
pub fn blocking_handle(&self) -> Option<BlockingPoolHandle>
Returns a handle to the blocking pool, if configured and the runtime is still alive.
Trait Implementations§
Source§impl Clone for RuntimeHandle
impl Clone for RuntimeHandle
Source§fn clone(&self) -> RuntimeHandle
fn clone(&self) -> RuntimeHandle
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more