Taskmaster v0.1.0 Taskmaster View Source

A set of convenience functions for concurrent, asynchronous tasks, loosely inspired by JavaScript's Promises.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Callback implementation for GenServer.init/1.

Creates a process, that runs funs concurrently and when the first one resolves, sends a {:race_won, result} message to the caller.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Callback implementation for GenServer.init/1.

Specs

race(funs :: [function(), ...], opts :: [{:timeout, integer()}]) :: {:ok, pid()}

Creates a process, that runs funs concurrently and when the first one resolves, sends a {:race_won, result} message to the caller.

Function resolves either by:

  • returning a value, which results in a {:race_won, value} message
  • crashing or returning a {:error, reason} tuple, which results in a {:race_interrupted, {:error | :exit, reason}} message
  • exceeding a :timeout options, which results in a {:race_interrupted, :timeout} message

The process created by race/2 isn't linked to the caller process. It terminates after the race is won.

Options

  • :timeout - a timeout for each function (defaults to 5000)

Example:

iex(1)> Taskmaster.race([
...(1)>         fn ->
...(1)>           :one
...(1)>         end,
...(1)>         fn ->
...(1)>           :timer.sleep(200)
...(1)>           :two
...(1)>         end,
...(1)>         fn ->
...(1)>           :timer.sleep(300)
...(1)>           :three
...(1)>         end
...(1)>       ])
{:ok, #PID<0.178.0>}
iex(2)> flush
{:race_won, :one}
:ok

The process created by race/2 isn't linked to the caller process. It terminates after the race is won, by one of the functions either returning a value or crashing.