我正在制作一个程序,将ID号分类为房间和组。我试图从多个并行进程中订购一些输出,但输出就像这样
{'C003','Group A',1}
{'C002','Group B',3}
{'C015','Group C',5}
{'C016','Group D',7}
{'C003','Group A',2}
{'C002','Group B',4}
{'C015','Group C',6}
{'C016','Group D',8}
但我想要这样:
{'C003','Group A',1}
{'C003','Group A',2}
{'C002','Group B',3}
{'C002','Group B',4}
{'C015','Group C',5}
{'C015','Group C',6}
{'C016','Group D',7}
{'C016','Group D',8}
我想也许我可以将数字发送到流程并让它以某种方式按顺序打印它们和它们的组但是我不确定如何在仍然使流程并行发生时这样做。我认为一个解决方案可能是选择性接收,但我似乎无法用这种方式解决这个问题。任何人都可以帮我这个吗? 这是我主程序的一段代码 注意:我是Erlang的新手
-module(ppp).
-compile([export_all]).
categorise(L) ->
Size = len(L) div 4,
Rem = len(L) rem 4,
spawn(ppp, cat, [self(), 'Group A', L, 0, Size + Rem]),
spawn(ppp, cat, [self(), 'Group B', L, (Size + Rem), Size]),
spawn(ppp, cat, [self(), 'Group C', L, (2*Size + Rem), Size]),
spawn(ppp, cat, [self(), 'Group D', L, (3*Size + Rem), Size]),
wait(4).
wait(0) -> {done};
wait(N) ->
receive
done -> wait(N-1)
end.
cat(P, Name, L, Start, Elements) ->
Extract = lists:split(Start, L),
Group = element(2, Extract),
AGroup = lists:sublist(Group, Elements),
spawn(ppp, putInRoom, [P, Name, AGroup]).
putInRoom(P, _, []) -> P ! done;
putInRoom(P, GroupName, [H|T]) ->
if GroupName == 'Group A' ->
io:format("~w~n", [{'C003', GroupName, H}]),
putInRoom(P, GroupName, T);
GroupName == 'Group B' ->
io:format("~w~n", [{'C002', GroupName, H}]),
putInRoom(P, GroupName, T);
GroupName == 'Group C' ->
io:format("~w~n", [{'C015', GroupName, H}]),
putInRoom(P, GroupName, T);
GroupName == 'Group D' ->
io:format("~w~n", [{'C016', GroupName, H}]),
putInRoom(P, GroupName, T)
end.
len(L) ->
count(L, 0).
count([], Acc) -> Acc;
count([_|T], Acc) -> count(T, Acc + 1).
答案 0 :(得分:0)
不是为每个组生成putInRoom
进程,而是为组中的每个元素生成。
-module(ppp).
-compile([export_all]).
categorise(L) ->
Size = len(L) div 4,
Rem = len(L) rem 4,
spawn(ppp, cat, [self(), 'Group A', L, 0, Size + Rem]),
spawn(ppp, cat, [self(), 'Group B', L, (Size + Rem), Size]),
spawn(ppp, cat, [self(), 'Group C', L, (2*Size + Rem), Size]),
spawn(ppp, cat, [self(), 'Group D', L, (3*Size + Rem), Size]),
wait(4).
wait(0) -> {done};
wait(N) ->
receive
done -> wait(N-1)
after 2000 ->
ok
end.
cat(P, Name, L, Start, Elements) ->
% io:format("cat: ~p L: ~p~n", [P,L]),
Extract = lists:split(Start, L),
Group = element(2, Extract),
AGroup = lists:sublist(Group, Elements),
% io:format("AGroup: ~p~n", [AGroup]),
lists:map(fun(G) ->
spawn(ppp, putInRoom, [P, Name, G])
end, AGroup).
putInRoom(P, GroupName, H) ->
% io:format("putInRoom: ~p, List: ~p~n", [GroupName, H]),
if GroupName == 'Group A' ->
io:format("~w~n", [{'C003', GroupName, H}]);
GroupName == 'Group B' ->
io:format("~w~n", [{'C002', GroupName, H}]);
GroupName == 'Group C' ->
io:format("~w~n", [{'C015', GroupName, H}]);
GroupName == 'Group D' ->
io:format("~w~n", [{'C016', GroupName, H}])
end,
P ! done.
len(L) ->
count(L, 0).
count([], Acc) -> Acc;
count([_|T], Acc) -> count(T, Acc + 1).
按顺序打印组。
2> ppp:categorise(lists:seq(1,8)).
{'C003','Group A',1}
{'C003','Group A',2}
{'C002','Group B',3}
{'C002','Group B',4}
{'C015','Group C',5}
{'C015','Group C',6}
{'C016','Group D',7}
{'C016','Group D',8}
{done}
在产生timer:sleep(100)
进程之间添加cat
使用延迟。但是,你对并发性质有点宽容。因此,如果您想按顺序打印消息,则必须提出不同的算法。
-module(ppp).
-compile([export_all]).
categorise(L) ->
Size = len(L) div 4,
Rem = len(L) rem 4,
spawn(ppp, cat, [self(), 'Group A', L, 0, Size + Rem]),
timer:sleep(100),
spawn(ppp, cat, [self(), 'Group B', L, (Size + Rem), Size]),
timer:sleep(100),
spawn(ppp, cat, [self(), 'Group C', L, (2*Size + Rem), Size]),
timer:sleep(100),
spawn(ppp, cat, [self(), 'Group D', L, (3*Size + Rem), Size]),
wait(4).
wait(0) -> {done};
wait(N) ->
receive
done -> wait(N-1)
after 2000 ->
ok
end.
cat(P, Name, L, Start, Elements) ->
% io:format("cat: ~p L: ~p~n", [P,L]),
Extract = lists:split(Start, L),
Group = element(2, Extract),
AGroup = lists:sublist(Group, Elements),
% io:format("AGroup: ~p~n", [AGroup]),
Pid = spawn(fun putInRoom2/0),
Pid ! {P, cat, {Name, AGroup}}.
putInRoom2() ->
receive
{P, cat, {_GroupName, []}} -> P ! done;
{P, cat, {GroupName, L}} ->
F = fun(G, [H|T]) ->
if
GroupName == 'Group A' ->
io:format("~w~n", [{'C003', GroupName, H}]);
GroupName == 'Group B' ->
io:format("~w~n", [{'C002', GroupName, H}]);
GroupName == 'Group C' ->
io:format("~w~n", [{'C015', GroupName, H}]);
GroupName == 'Group D' ->
io:format("~w~n", [{'C016', GroupName, H}])
end,
self() ! {P, cat, {G, T}}
end,
F(GroupName, L),
putInRoom2()
end.
len(L) ->
count(L, 0).
count([], Acc) -> Acc;
count([_|T], Acc) -> count(T, Acc + 1).