【问题标题】:Erlang receive afterErlang 接收后
【发布时间】: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


【解决方案1】:

正如 Dogbert 在评论中指出的那样,我完全忘记了在 false 下更改函数的名称,该函数调用了另一个没有超时的函数。

问题已解决,谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 2014-09-09
    • 2012-03-15
    • 2014-02-18
    • 1970-01-01
    • 2023-03-31
    • 2012-10-18
    • 2012-06-13
    相关资源
    最近更新 更多