fabianlindfors
ParallelTask: easily run functions in parallel and capture the results
Hi!
I’m quite new to the Elixir community and so far hooked on the language and ecosystem. Today I read a nice article by Ryan Sydnor on how they speed up their application by parallelising database queries. He used tasks to concurrently run the expensive queries which improved performance drastically. The full article can be read here.
Elixir really shines in cases like these and I thought it would be great to have a library for simple parallelization. That’s why I created ParallelTask, a lightweight wrapper around Task making it easy to parallelize API requests, database queries, and the like.
This is my first Elixir project and any feedback is much welcomed. Thanks!
Most Liked
axelson
I like the idea, how you can build up the tasks to execute in parallel and then execute them together. Although since the idea is conceptually very similar to Ecto.Multi I think it would make sense to mirror that API.
So instead of:
|> ParallelTask.add(first_task: fn -> "Result from first task" end)
We could write:
|> ParallelTask.add(:first_task, fn -> "Result from first task" end)
That would also allow us to more flexibly name our tasks (Ecto.Multi actually now accepts strings)
jaimeiniesta
I like it, it reads nicely!
mbuhot
Nice package! ![]()
It’s surprising that the Task module doesn’t make it easier to work with keyed tasks like this.
The closest I could get without writing a helper function is:
tasks = %{a: async(fn -> "foo" end), b: async(fn -> "bar" end)}
results = for {k, v} <- tasks, into: %{}, do: {k, Task.await(v)}
But that has slightly different semantics to your package (starts tasks eagerly and awaits each separately).
The implementation of add can benefit from some pattern matching and map update syntax:
def add(%__MODULE__{task_functions: task_functions} = object, new_functions \\ []) do
%{object | task_functions: Enum.into(task_functions, new_functions)}
end
I try not to use Enum.at wherever possible, such as in the definition of perform, I’d try to use unzip and zip to match the keys and results together:
def perform(%__MODULE__{task_functions: task_functions}, timeout \\ 5000) do
{keys, tasks} =
task_functions
|> Enum.map(fn {k, f} -> {k, Task.async(f)} end)
|> Enum.unzip()
task_results =
tasks
|> Task.yield_many(timeout)
|> Enum.map(&get_task_result/1)
keys
|> Enum.zip(task_results)
|> Map.new()
end
Popular in Announcing
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









