Task.await_many
await_many
, go back to Task module for more information.
Specs
Awaits replies from multiple tasks and returns them.
This function receives a list of tasks and waits for their replies in the
given time interval. It returns a list of the results, in the same order as
the tasks supplied in the tasks
input argument.
If any of the task processes dies, the current process will exit with the same reason as that task.
A timeout, in milliseconds or :infinity
, can be given with a default value
of 5000
. If the timeout is exceeded, then the current process will exit.
Any task processes that are linked to the current process (which is the case
when a task is started with async
) will also exit. Any task processes that
are trapping exits or not linked to the current process will continue to run.
This function assumes the tasks' monitors are still active or the monitors'
:DOWN
message is in the message queue. If any tasks have been demonitored,
or the message already received, this function will wait for the duration of
the timeout.
This function can only be called once for any given task. If you want to be
able to check multiple times if a long-running task has finished its
computation, use yield_many/2
instead.
Compatibility with OTP behaviours
It is not recommended to await
long-running tasks inside an OTP behaviour
such as GenServer
. See await/2
for more information.
Examples
iex> tasks = [
...> Task.async(fn -> 1 + 1 end),
...> Task.async(fn -> 2 + 3 end)
...> ]
iex> Task.await_many(tasks)
[2, 5]