Task.yield
yield
, go back to Task module for more information.
Specs
Temporarily blocks the current process waiting for a task reply.
Returns {:ok, reply}
if the reply is received, nil
if
no reply has arrived, or {:exit, reason}
if the task has already
exited. Keep in mind that normally a task failure also causes
the process owning the task to exit. Therefore this function can
return {:exit, reason}
only if
- the task process exited with the reason
:normal
- it isn't linked to the caller
- the caller is trapping exits
A timeout, in milliseconds or :infinity
, can be given with a default value
of 5000
. If the time runs out before a message from the task is received,
this function will return nil
and the monitor will remain active. Therefore
yield/2
can be called multiple times on the same task.
This function assumes the task's monitor is still active or the
monitor's :DOWN
message is in the message queue. If it has been
demonitored or the message already received, this function will wait
for the duration of the timeout awaiting the message.
If you intend to shut the task down if it has not responded within timeout
milliseconds, you should chain this together with shutdown/1
, like so:
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
That ensures that if the task completes after the timeout
but before shutdown/1
has been called, you will still get the result, since shutdown/1
is designed to
handle this case and return the result.