【发布时间】:2016-12-11 02:19:59
【问题描述】:
我在 Erlang 中的函数接收后遇到问题,我已经用谷歌搜索并对此进行了一段时间的尝试,但似乎无法弄清楚并且需要一些帮助。我有一个进程运行以下进程并等待消息。
% Waits for messages, and puts them into the list.
% If the list has the same length as the Length variable
% which is the original provided list, then it means we are done
% and can print the results.
% If the MaxTime has passed it should just print the provided list.
wait_max_time(List, Length, MaxTime) ->
io:format("I am ~p and waiting...~n", [self()]),
receive
X ->
Results = List ++ [X],
case length(Results) =:= Length of
true ->
io:format("~p~n", [Results]),
exit(self());
false ->
wait(Results, Length)
end
after MaxTime -> io:format("List is ~p~n", [List])
end.
之后的一切似乎都可以正常工作,它总是在计算,我已经用打印语句确认了它。
但是对于需要很长时间的计算,我只想打印它得到的列表而不继续。但是在我的代码中从未运行过之后,我做错了什么?
【问题讨论】:
-
您的意思是在
false -> ...中调用wait_max_time而不是wait? -
附带问题:List Handling 中的 #1 规则是您永远不会追加到列表的末尾。对于非常短的列表也可以,但您应该养成构建列表的习惯,例如
[X | List]而不是List ++ [X]。 -
您提供的代码并没有真正说明问题。 SSCCE 将大大有助于为您提供一个好的答案。可能是您通过向此过程发送消息来减缓自己的 DoS 攻击。请注意,每次收到消息时,
after超时都会重置。
标签: parallel-processing erlang timeout wait