nelson687

nelson687

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!

Showing Posts 1 to 6

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 OP

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?

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.

nelson687

nelson687 OP

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?

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

nelson687

nelson687 OP

excellent, makes sense now

thanks!

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews