Erlang OTP sagte gen_server:call
ist synchron und gen_server:cast
ist asynchron.Erlang Gen_call vs Gen_cast
nach dem Test finde ich gen_server:call
ist synchron. Allerdings wird gen_server:cast
Nachricht an die Mailbox senden, aber nicht Aufgaben parallel ausführen.
Wie kann ich erstellen multi Prozesse zu laufen?
-module(example_gen).
-behaviour(gen_server).
-export([start_link/0]).
-export([init/1,
handle_call/3,
handle_cast/2,
handle_info/2,
terminate/2,
code_change/3]).
-export([
add_bycall/2,
add_bycast/2
]).
-define(SERVER, ?MODULE).
-record(state, {}).
add_bycall(Key, Value) ->
gen_server:call(?SERVER, {bycall, Key, Value}).
add_bycast(Key, Value) ->
gen_server:cast(?SERVER, {bycast, Key, Value}).
example(Key, Value) ->
timer:sleep(2000),
io:format("finished [~p, ~p] at [~p] ~n", [Key, Value, erlang:system_time(milli_seconds)]).
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
init([]) ->
{ok, #state{}}.
handle_call({bycall, Key, Value}, _From, State) ->
example(Key, Value),
{reply, ok, State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast({bycast, Key, Value}, State) ->
example(Key, Value),
{noreply, State};
handle_cast(_Request, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
den Code
[example_gen:add_bycall(K, K) || K <- lists:seq(1, 10) ].
[example_gen:add_bycast(K, K) || K <- lists:seq(1, 10) ].
Danke. es erklärt viel. – user3644708