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!

1 Like

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.

2 Likes

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?

1 Like

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.

6 Likes

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?

1 Like

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).

3 Likes

excellent, makes sense now

thanks!

1 Like