# Is there an equivalent of the Elixir Task module in Erlang?

**URL:** <https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161>\
**Category:** Questions / Help\
**Tags:** elixir, erlang\
**Created:** [November 5, 2024, 9:32pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161 "2024-11-05T21:32:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![meox](https://erlangforums.com/user_avatar/erlangforums.com/meox/32/476_2.png) [@meox](https://erlangforums.com/u/meox)\
**Post date:** [November 5, 2024, 9:32pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/1 "2024-11-05T21:32:15Z")

</div>

I’m looking for a “standard” way to have the `Task` Elixir module in a pure Erlang solution.  
Do you know if there is already a library that implement (backport 😃 ) it?

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [November 7, 2024, 9:04am UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/2 "2024-11-07T09:04:02Z")

</div>

Hi,

I am not aware of such an erlang library.  
But since your profile states that you are just getting into erlang, it might be a nice task (pun not intended) to look at

> <https://github.com/elixir-lang/elixir/blob/main/lib/elixir/lib/task.ex>

and try to port it to erlang.  
At least for me, I learn the most if I hack along myself. 🙂

---

<div class="post-metadata">

**Author:** ![ingarr](https://erlangforums.com/user_avatar/erlangforums.com/ingarr/32/3053_2.png) [@ingarr](https://erlangforums.com/u/ingarr)\
**Post date:** [November 7, 2024, 8:50pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/3 "2024-11-07T20:50:56Z")

</div>

Hi,  
I am not aware of such a library but code in native Erlang using spawn() function is pretty short and clean.

Elixir (from previous response):

```erlang
...
task = Task.async(fn -> do_some_work() end)
res = do_some_other_work()
res + Task.await(task)
...

```

Erlang:

```erlang
====== do_work.erl ============
-module(do_work).

-export([start/0]).

% Define a function to do the work with async task
start() ->
    % Save process id (pid) of the current process
    MainPid = self(),
    io:format("main pid: ~p~n", [MainPid]),

    spawn( % Spawn a new process to start a task function (this is "Async" part)
        fun() -> % "fun() -> ... end" defines a lambda function (unnamed function)
                                                    % that is executed in the async task process
            io:format("task pid: ~p~n", [self()]), % here self() has different value <> MainPid 
                                                    % because its called in a different process
            Result = do_some_work(), % do work
            MainPid ! {result, Result} % after finishing work send a result to the mailbox 
                                            % of process with MainPid (function that spawned this function)
                                            % (lanbda function body commands have access to variables 
                                            % in the context of caller function, in our case - MainPid)
        end
    ),

    % Perform some other work in the main process (not waiting finishing of spawned task)
    OtherResult = do_some_other_work(),

    % Wait for the spawned process to send back the result (this is "Await" part)
    receive % wait for response message
        {result, TaskResult} -> % message received
            io:format("result: ~p~n", [OtherResult ++ " " ++ TaskResult])
    after 5000 -> % after 5 seconds (if result not received)
        io:format("timeout~n", [])
    end.

% Example implementations of the work functions
do_some_work() ->
    % Some work
    "R2". % return result

do_some_other_work() ->
    % Some other work
    "R1". % return result
...

```

---

<div class="post-metadata">

**Author:** ![dischoen](https://erlangforums.com/user_avatar/erlangforums.com/dischoen/32/695_2.png) [@dischoen](https://erlangforums.com/u/dischoen)\
**Post date:** [November 8, 2024, 2:07pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/4 "2024-11-08T14:07:14Z")

</div>

Hi,

that’s a good starting point. Depending on how much of the Elixir.Task [1] functionality the OP  
needs, there is some more to do.

For example await() returns the return value of the function of the task. With a plain spawn() you do not get the return value.  
You could implement this with a wrapper around the function which sends the return value up to the parent. Or, if you do not want to implement all the boiler plate code yourself, use a gen\_server as wrapper and use a gen\_server:call().

And a very nice feature IMO is await\_many() which waits for the returns of many tasks and delivers their return values in the order of the tasks. This can be nicely integrated into a functional construct like map.

[edit] I started an implementation in erlang in the company, if I find it, I could post it next week.

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [November 9, 2024, 4:35pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/5 "2024-11-09T16:35:50Z")

</div>

Hi, there is no equiv that I am aware. You’ve gotten some good answers here but I wanted to respond so I could lay out some of the differences between Task in elixir and just using spawn.

Key differences :

1. Tasks are supervised, options are specifically threaded through to DynamicSupervisor to spin up the proc. This is a big difference between just using spawn and using Task to be aware of.
2. Similarly and quite related, some cases are handled for you when using Task, that you will have to handle yourself when just using spawn, such as an exit from the process.

---

<div class="post-metadata">

**Author:** ![Led](https://erlangforums.com/user_avatar/erlangforums.com/led/32/1594_2.png) [@Led](https://erlangforums.com/u/Led)\
**Post date:** [November 11, 2024, 3:50pm UTC](https://erlangforums.com/t/is-there-an-equivalent-of-the-elixir-task-module-in-erlang/4161/6 "2024-11-11T15:50:56Z")

</div>

IMO any worker pool manager is much better, even the simplest worker\_pool from rabbit\_common.
