pub struct Task;Expand description
A task is a lightweight thread of execution designed to run one particular action.
Tasks can not receive messages from other processes, only send.
Calls that are allowed:
- Process::send
- GenServer::cast
Calls that would panic:
- Process::link
- Process::monitor
- GenServer::call
You can however return a value in a task and await it to receive the value.
It’s not recommended to await long running tasks in a GenServer since it will delay processing of other messages.
Instead, you should send a cast with the result of your task and handle it in handle_cast.
Implementations§
Source§impl Task
impl Task
Sourcepub fn spawn<F>(task: F) -> TaskHandle<F::Output> ⓘ
pub fn spawn<F>(task: F) -> TaskHandle<F::Output> ⓘ
Runs the provided asynchronous task.
Sourcepub fn spawn_blocking<F, R>(task: F) -> TaskHandle<R> ⓘ
pub fn spawn_blocking<F, R>(task: F) -> TaskHandle<R> ⓘ
Runs the provided synchronous task on a thread where blocking is acceptable.
Sourcepub async fn shutdown<R>(task: TaskHandle<R>) -> Result<R, TaskError>
pub async fn shutdown<R>(task: TaskHandle<R>) -> Result<R, TaskError>
Shuts down the task, and then checks for a result.
Returns the result if the task finishes while shutting down, TaskError if the task died before returning.
Sourcepub async fn await_many<R, const N: usize>(
tasks: [TaskHandle<R>; N],
) -> Result<Vec<R>, TaskError>where
R: 'static,
pub async fn await_many<R, const N: usize>(
tasks: [TaskHandle<R>; N],
) -> Result<Vec<R>, TaskError>where
R: 'static,
Await many tasks at once and returns their results in order, or returns the first error that occurs.