nelson687

nelson687

Difference between Task.await and Task.yield

Been reading the Hex docs but it’s still not very clear to me.
I can see this

Task.yield/2 is an alternative to await/2 where the caller will temporarily block, waiting until the task replies or crashes. If the result does not arrive within the timeout, it can be called again at a later moment. This allows checking for the result of a task multiple times. If a reply does not arrive within the desired time, Task.shutdown/2 can be used to stop the task.

When is that “later” moment? Is that the only difference between await and yield ?
I’m also lost with the caller being temporarily blocked - Does this mean that the the processes will not run in parallel? or am I missing something?

Also this snippet that is the in the docs for yield confuses me

case Task.yield(task, timeout) || Task.shutdown(task) do
  {:ok, result} ->
    result
  nil ->
    Logger.warn "Failed to get a result in #{timeout}ms"
    nil
end

When the timeout happens, what’s gonna happen? is it gonna log the warning because it returned nil or execute Task.shutdown(task)

thanks!

Most Liked

peerreynders

peerreynders

Yes - so typically you would decrement a counter and take drastic action once it gets to 0.

Task.await/2:

If the timeout is exceeded, then the current process will exit. If the task process is linked to the current process which is the case when a task is started with async, then the task process will also exit.

So Task.await/2 will automatically take drastic action after the first timeout. Task.yield/2 leaves the response to a timeout entirely up to you.

peerreynders

peerreynders

Pretty much.

You could also immediately issue Task.shutdown/2 for the runaway task, simply to keep your current process alive (an option which Task.await/2 doesn’t give you but given that the current process potentially provided faulty startup data to the task also terminating the current process may be a reasonable approach).

Qqwy

Qqwy

TypeCheck Core Team

It means that the caller process will wait (AKA be blocked) until:

  1. the task completed, or
  2. the timeout has passed. In this case you could call Task.yield again (this is ‘later’).

So yes, it will not run in parallel during this time, which is exactly the point of this call: Waiting until a result is available. Obviously, by using a low timeout, we only check if there currently is a value without really ‘waiting’ in the meantime.

nelson687

nelson687

but what if for some reason it always time out? will I have an infinite loop if I call Task.yield every time there is a timeout?

nelson687

nelson687

got it, so the only difference between await and yield is that with yield I can wait for the task to finish as much as I want (using a counter like you suggested to see how many times I want to retry after it has timed out)
is that right?

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement