New function: `Task.await_many`


The Task module currently contains three functions that synchronously retrieve results from asynchronous tasks:

  • Task.await: Blocks waiting for a reply from a single task. Accepts a timeout value. If the task is successful, returns reply. If the task dies or the timeout is reached, exits with the corresponding reason.
  • Task.yield: Blocks waiting for a reply from a single task. Accepts a timeout value. If the task is successful, returns {:ok, reply}. If the task dies or the timeout is reached, returns {:exit, reason} or nil.
  • Task.yield_many: Blocks waiting for replies from a list of tasks. Accepts a timeout value. When all tasks are complete or the timeout is reached, returns a list of result tuples: {:ok, reply} for successful tasks, {:exit, reason} for dead tasks, and nil for timed-out.

Additionally, the Task module contains one function that handles both creating asynchronous tasks and retrieving the results:

  • Task.async_stream: Asynchronously applies a given function to each element in a given enumerable. Accepts a timeout value that is applied to each task separately. Returns an enumerable that emits results, blocking as needed. If a task dies, exits with the reason. When tasks complete, emits {:ok, reply}. When tasks reach the timeout, either exits or emits {:exit, :timeout}, depending on configuration options.

The discussion that eventually became Task.async_stream included an alternative suggestion of Task.async_many and Task.await_many. In the end, async_stream was chosen because it provides the ability to bound the maximum concurrency and stream results, making it the most robust way to handle intensive processing over an enumerable.


I propose this addition to retrieve results from multiple asynchronous tasks while adhering to await behavior:

Task.await_many(tasks, timeout \\ 5000)

Blocks waiting for replies from a list of tasks. If the tasks complete, returns a list of replies. If the timeout is reached, exits with :timeout. If a task dies, exits with the reason given by the task.

Task.await (together with Task.async) provides a simple solution that can be used as a drop-in replacement for synchronous code. The addition of Task.await_many will provide the building blocks for many common asynchronous flows, with unsurprising default behavior.

It fits well with the existing feature set and should be very easy for people to use and understand, provided they are familiar with the other Task functions.


Why not Task.await?

A common pattern suggested online [1][2][3] is to enumerate calls to Task.await:, &Task.await(&1, timeout))

Because the await calls happen sequentially, the timeout is reset for each element of the list. This can lead to unexpected and likely unwanted behavior in which this call may block much longer than the specified timeout.

Why not Task.yield_many?

Task.yield_many works fine for this situation, but it adheres to the semantics of Task.yield rather than Task.await. It returns a tuple instead of the bare reply, and on failure it does not exit or kill still-running tasks. To achieve the behavior of await, you must write something like this to handle the various possible results:

|> {task, result} ->
  case result do
    nil ->
      # Maybe unnecessary since we are exiting?
      Task.shutdown(task, :brutal_kill)
    {:exit, reason} ->
    {:ok, result} ->

Rather than expecting every developer to write this boilerplate (and not make any mistakes in doing so), I think it would be better to provide a construct in the standard library.

Why not Task.async_stream?

Task.async_stream is great for enumerating the same expensive operation across a list of inputs, and it absolutely should be used for that. However, it is not well-suited to situations where the collection of tasks is less uniform. Consider the following:

tasks = [
  Task.async(fn -> preheat_oven() end),
  Task.async(fn -> grease_pan(:butter) end),
  Task.async(fn -> mix_batter(2) end),

async_stream is a specialized tool that should not be applied in a generalized way. In addition, async_stream has its own return values and exit behavior that does not match that of await.

One potential harm of adding Task.await_many is that people might be tempted to use it when they would be better off using Task.async_stream. I believe this can be mitigated with proper documentation.

Why not GenStage?

GenStage provides powerful and flexible abstractions for handling asynchronous flows. For applications that have complicated needs, this is a great tool. Often, though, we have much simpler needs and applying GenStage to the problem would be massive overkill.

Task.await is easy to use and easy to reason about. The goal of Task.await_many is the same. It’s okay if it doesn’t cover every possible use case, as long as it covers the ones we most commonly encounter in a way that doesn’t encourage us to make mistakes.