如何在 Erlang 中对多个值使用发送接收?
How can I use send receive for multiple values in Erlang?
我有以下循环:
for( i = 1 ; i < V ; i++ )
{
i sends "hi" to arr[i]
}
for( i = 1 ; i <arr.size ; i++ )
{
if arr[i] receives "hi"
{
print "bye"
}
}
如何在 erlang 中实现这些代码?
我理解简单的 ping 和 pong,但我想并行创建此代码,以平衡负载。我对循环实现部分有些困惑。
这是您可以执行的操作的示例:
-module(a).
-compile(export_all).
worker() ->
receive
{hi, From} ->
From ! {bye, self()},
worker();
stop ->
io:format("Worker ~w terminated.~n", [self()]);
_Other ->
io:format("Bad message received by worker: ~w~n", [self()]),
worker()
end.
create_workers(N) ->
create_workers(N, _Pids=[]).
create_workers(0, Pids) -> Pids;
create_workers(N, Pids) ->
Pid = spawn(a, worker, []),
create_workers(N-1, [Pid|Pids]).
test()->
N = 4,
Workers = create_workers(N),
RandNum1 = rand:uniform(N),
RandNum2 = rand:uniform(N),
Worker1 = lists:nth(RandNum1, Workers),
Worker2 = lists:nth(RandNum2, Workers),
Worker1 ! hello,
Worker1 ! {hi, self()},
Worker2 ! {xxxx, self()},
Worker2 ! {hi, self()},
Results = get_results(2, _Acc=[]),
io:format("Worker results: ~w~n", [Results]),
terminate(Workers).
get_results(0, Acc) -> Acc;
get_results(N, Acc) ->
Result = receive
{Msg, _From} -> Msg
end,
get_results(N-1, [Result|Acc]).
terminate(Workers) ->
lists:foreach(fun(Worker) -> Worker ! stop end,
Workers).
在shell中:
6> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
7> a:test().
Worker results: [bye,bye]
Bad message received by worker: <0.96.0>
Bad message received by worker: <0.99.0>
Worker <0.96.0> terminated.
Worker <0.99.0> terminated.
Worker <0.98.0> terminated.
Worker <0.97.0> terminated.
ok
我有以下循环:
for( i = 1 ; i < V ; i++ )
{
i sends "hi" to arr[i]
}
for( i = 1 ; i <arr.size ; i++ )
{
if arr[i] receives "hi"
{
print "bye"
}
}
如何在 erlang 中实现这些代码?
我理解简单的 ping 和 pong,但我想并行创建此代码,以平衡负载。我对循环实现部分有些困惑。
这是您可以执行的操作的示例:
-module(a).
-compile(export_all).
worker() ->
receive
{hi, From} ->
From ! {bye, self()},
worker();
stop ->
io:format("Worker ~w terminated.~n", [self()]);
_Other ->
io:format("Bad message received by worker: ~w~n", [self()]),
worker()
end.
create_workers(N) ->
create_workers(N, _Pids=[]).
create_workers(0, Pids) -> Pids;
create_workers(N, Pids) ->
Pid = spawn(a, worker, []),
create_workers(N-1, [Pid|Pids]).
test()->
N = 4,
Workers = create_workers(N),
RandNum1 = rand:uniform(N),
RandNum2 = rand:uniform(N),
Worker1 = lists:nth(RandNum1, Workers),
Worker2 = lists:nth(RandNum2, Workers),
Worker1 ! hello,
Worker1 ! {hi, self()},
Worker2 ! {xxxx, self()},
Worker2 ! {hi, self()},
Results = get_results(2, _Acc=[]),
io:format("Worker results: ~w~n", [Results]),
terminate(Workers).
get_results(0, Acc) -> Acc;
get_results(N, Acc) ->
Result = receive
{Msg, _From} -> Msg
end,
get_results(N-1, [Result|Acc]).
terminate(Workers) ->
lists:foreach(fun(Worker) -> Worker ! stop end,
Workers).
在shell中:
6> c(a).
a.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,a}
7> a:test().
Worker results: [bye,bye]
Bad message received by worker: <0.96.0>
Bad message received by worker: <0.99.0>
Worker <0.96.0> terminated.
Worker <0.99.0> terminated.
Worker <0.98.0> terminated.
Worker <0.97.0> terminated.
ok