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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
It means that the caller process will wait (AKA be blocked) until:
Task.yieldagain (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
but what if for some reason it always time out? will I have an infinite loop if I call
Task.yieldevery time there is a timeout?peerreynders
Yes - so typically you would decrement a counter and take drastic action once it gets to 0.
Task.await/2:So
Task.await/2will automatically take drastic action after the first timeout.Task.yield/2leaves the response to a timeout entirely up to you.nelson687
got it, so the only difference between
awaitandyieldis that withyieldI 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
Pretty much.
You could also immediately issue
Task.shutdown/2for the runaway task, simply to keep your current process alive (an option whichTask.await/2doesn’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
excellent, makes sense now
thanks!