pub struct AsyncRuntime {
pub name: Arc<String>,
/* private fields */
}Expand description
Offers a unified interface for awaiting both async tasks and thread-blocking tasks from any context.
Clone is intentionally not implemented so that an AsyncRuntime
can only be shared explicitly, as the destructor needs to shut down the
underlying owned Tokio runtime, so it needs to be called only once all references are dropped.
Dropping an [‘AsyncRuntime’] also extracts and shuts down the underlying owned Tokio runtime without waiting for tasks to finish. This allows dropping it also from async contexts, but users may want to ensure manually that all tasks are finished beforehand.
Fields§
§name: Arc<String>Implementations§
Source§impl AsyncRuntime
impl AsyncRuntime
Sourcepub fn new(name: impl Into<String>, tokio: Option<Runtime>) -> AsyncRuntime
pub fn new(name: impl Into<String>, tokio: Option<Runtime>) -> AsyncRuntime
Owns the passed runtime, using it only if no contextual handle is available;
if None is passed, it creates a runtime with multi-threaded support,
CPU-based thread pool size and all features enabled.
Sourcepub fn block_on_async<R>(&self, f: impl Future<Output = R>) -> R
pub fn block_on_async<R>(&self, f: impl Future<Output = R>) -> R
Blocks the current thread executing the passed future to completion. In an async context, this relinquishes an executor thread to then re-enter the async context, which is inefficient and should be done sparingly.
pub fn spawn_async<R>(
&self,
f: impl Future<Output = R> + Send + 'static,
) -> JoinHandle<R>where
R: Send + 'static,
pub fn thread_blocking<R>(&self, f: impl FnOnce() -> R) -> R
Sourcepub fn spawn_thread_blocking_send<MsgT>(
&self,
f: impl FnOnce() -> MsgT + Send + 'static,
actor_ref: impl ActorRef<MsgT> + 'static,
delay: Option<Duration>,
) -> JoinHandle<()>where
MsgT: ActorMsg + 'static,
pub fn spawn_thread_blocking_send<MsgT>(
&self,
f: impl FnOnce() -> MsgT + Send + 'static,
actor_ref: impl ActorRef<MsgT> + 'static,
delay: Option<Duration>,
) -> JoinHandle<()>where
MsgT: ActorMsg + 'static,
Spawn an async task that may allocate an executor thread to execute a possibly long-running and thread-blocking function to completion, then sending the result to the passed actor reference.
It can be called from any context but creating a dedicated thread to run the thread-blocking function, is inefficient and should be done sparingly.