Multitask

Build Status

Multitasks are processes meant to execute several functions asynchronously, and collect their return values when they are done processing or the error of the first failure.

Installation

Add multitask to your project's dependencies in mix.exs:

def deps do
  [{:multitask, "~> 0.1"}]
end

And fetch your project's dependencies:

$ mix deps.get

Usage

Multitasks are processes meant to execute several functions asynchronously, and collect their return values when they are done processing or the error of the first failure.

iex> multitask = Multitask.async([
...>   fn -> {:ok, "a"} end,
...>   fn -> {:ok, "b"} end
...> ])
iex> Multitask.await(multitask)
{:ok, ["a", "b"]}

As shown in the example above, multitasks spawned with async can be awaited on by their caller process (and only their caller). They are implemented by spawning a process that sends a message to the caller once the given computation is performed. Internally, the multitask process spawns a new process per function (linked and monitored by the multitask process) and awaits for replies.

Functions must return {:ok, result} or {:error, error} tuples to express success or failure. Multitasks implement a fail-fast behavior, reporting any failure or exit immediately and shutting down all the processes.

iex> multitask = Multitask.async([
...>   fn -> {:ok, "a"} end,
...>   fn -> {:ok, "b"} end,
...>   fn -> {:error, "c"} end
...> ])
iex> Multitask.await(multitask)
{:error, "c"}

Multitasks can also be started as part of a supervision tree and dynamically spawned on remote nodes. Check the documentation for more information.

Documentation

Documentation is available at https://hexdocs.pm/multitask

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/fertapric/multitask. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Running tests

Clone the repo and fetch its dependencies:

$ git clone https://github.com/fertapric/multitask.git
$ cd multitask
$ mix deps.get
$ mix test

Building docs

$ mix docs

License

Multitask is released under the MIT License.

Author

Fernando Tapia Rico, @fertapric