【问题标题】:Determine if all erlang processes spawned are idle确定产生的所有 erlang 进程是否都是空闲的
【发布时间】:2013-10-21 23:15:38
【问题描述】:

我想知道我所有的衍生进程是否处于空闲状态或不做任何事情。所以我有这个特殊的例子,它产生了 1000 个 proc/3 的 erlang 进程

proc(A,B,C) ->
    receive
        {do} ->
            NewA = % doing something
            NewB = % doing something
            NewC = % doing something
            % doing something
            % ...
            io:format("Process: ~w is done doing something.", [self()]),
            proc(NewA,NewB,NewC)
    end

[,,,...]

现在这些进程中的每一个都会收到一条消息{do},告诉它们与其他进程同时执行其下的语句。我如何知道是否所有进程都已执行完毕,或者只是它们现在处于空闲状态?

【问题讨论】:

  • 你能在进程完成工作并空闲时结束它吗?如果可以,您可以使用监视器。
  • 不,进程必须保持活动状态。

标签: erlang


【解决方案1】:

我遇到了这个问题,我通过将消息发送回负责发送 do 消息的进程来解决它。就我而言,我只需要计算答案的数量。

proc(A,B,C,From) ->
    receive
        {do} ->
            NewA = % doing something
            NewB = % doing something
            NewC = % doing something
            % doing something
            % ...
            io:format("Process: ~w is done doing something.", [self()]),
            From ! {job_done,self()},
            proc(NewA,NewB,NewC,From)
    end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    相关资源
    最近更新 更多