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

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews