What is the difference between tasks and async functions #4810
Replies: 1 comment
-
For question 1, an async function returns a future representing the computation done by that function. That future can be awaited if you are within an async context to effectively join that future with the one you are currently in (the state of the current future cannot advance until the subfuture completes now). Futures in rust are lazy and do nothing unless polled. If you want another future to be run concurrently (and in parallel if using the multi-threaded runtime), you can use For question 2, you are correct, you are blocking the thread and preventing other futures from being driven unless you have an await statement in the loop body, which would allow the current task to yield. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Question 1: What is the difference between tasks and async functions in the framework Tokio?
Create a task:
tokio::spawn
Create a async function:
async fn async_fn_name(){}. ... async_fn_name.await?;
Question 2: What happens if I run
loop{}
orwhile let
in a task, does it will occupy a thread until jumping out of the loop?Beta Was this translation helpful? Give feedback.
All reactions