Futures and Promises in Erlang

Does Erlang have equivalents for Future and Promises ? Or, since the future and promises solve a problem that does not exist in Erlang systems (for example, synchronization with orchestration), and therefore we do not need them in Erlang.

If I need the semantics of Erlang futures and promises, can they be emulated using Erlang processes / participants?

+3
source share
2 answers

You can easily realize the future in Erlang as follows:

F = fun() -> fancy_function() end,

% fancy code

Pid = self(),
Other = spawn(fun() -> X = F(), Pid ! {future, self(), X} end).

% more fancy code

Value = receive {future, Other, Val} -> Val end.

, , , - . , .

+7

RPC rpc:async_call/4, , . ( node(), node) rpc:yield/1:

1> MaxTime = rpc:async_call(node(), timer, sleep, [30000]).
<0.48.0>
2> lists:sort([a,c,b]).
[a,b,c]
3> rpc:yield(MaxTime).
... [long wait] ...
ok

, rpc:nb_yield/1, rpc:nb_yield/2:

4> Key2 = rpc:async_call(node(), timer, sleep, [30000]).
<0.52.0>
5> rpc:nb_yield(Key2).
timeout
6> rpc:nb_yield(Key2).
timeout
7> rpc:nb_yield(Key2).
timeout
8> rpc:nb_yield(Key2, 1000).
timeout
9> rpc:nb_yield(Key2, 100000).
... [long wait] ...
{value,ok}

.

+6

Source: https://habr.com/ru/post/1629469/


All Articles