Erlang监控多个进程

时间:2017-05-29 21:14:51

标签: process erlang monitor

我需要监控一堆工作进程。目前,我能够通过1台显示器监控1个流程。如何将其扩展为监视N个工作进程。我是否也需要产生N个显示器?如果是这样,如果其中一个产生的监视器失败/崩溃会发生什么?

2 个答案:

答案 0 :(得分:1)

  

我是否还需要生成N个监视器?

没有

-module(mo).
-compile(export_all).

worker(Id) ->
    timer:sleep(1000 * rand:uniform(5)),
    io:format("Worker~w: I'm still alive~n", [Id]),
    worker(Id).

create_workers(N) ->
    Workers = [  % { {Pid, Ref}, Id }
        { spawn_monitor(?MODULE, worker, [Id]), Id }
        || Id <- lists:seq(1, N)
    ],
    monitor_workers(Workers).

monitor_workers(Workers) ->
    receive
        {'DOWN', Ref, process, Pid, Why} ->
            Worker = {Pid, Ref},
            case is_my_worker(Worker, Workers) of
                true  ->  
                    NewWorkers = replace_worker(Worker, Workers, Why),
                    io:format("Old Workers:~n~p~n", [Workers]),
                    io:format("New Workers:~n~p~n", [NewWorkers]),
                    monitor_workers(NewWorkers);
                false -> 
                    monitor_workers(Workers)
            end;
        _Other -> 
            monitor_workers(Workers)
    end.

is_my_worker(Worker, Workers) ->
    lists:keymember(Worker, 1, Workers).

replace_worker(Worker, Workers, Why) ->
    {{Pid, _}, Id} = lists:keyfind(Worker, 1, Workers),
    io:format("Worker~w (~w) went down: ~s~n", [Id, Pid, Why]),
    NewWorkers = lists:keydelete(Worker, 1, Workers),
    NewWorker = spawn_monitor(?MODULE, worker, [Id]),
    [{NewWorker, Id}|NewWorkers].

start() ->
    observer:start(),  %%In the Processes tab, you can right click on a worker and kill it.
    create_workers(4).

在shell中:

$ ./run
Erlang/OTP 19 [erts-8.2] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V8.2  (abort with ^G)


1> Worker3: I'm still alive
Worker1: I'm still alive
Worker2: I'm still alive
Worker4: I'm still alive
Worker3: I'm still alive
Worker1: I'm still alive
Worker4: I'm still alive
Worker2: I'm still alive
Worker3: I'm still alive
Worker1: I'm still alive
Worker4: I'm still alive
Worker3 (<0.87.0>) went down: killed
Old Workers:
[{{<0.85.0>,#Ref<0.0.4.292>},1},
 {{<0.86.0>,#Ref<0.0.4.293>},2},
 {{<0.87.0>,#Ref<0.0.4.294>},3},
 {{<0.88.0>,#Ref<0.0.4.295>},4}]
New Workers:
[{{<0.2386.0>,#Ref<0.0.1.416>},3},
 {{<0.85.0>,#Ref<0.0.4.292>},1},
 {{<0.86.0>,#Ref<0.0.4.293>},2},
 {{<0.88.0>,#Ref<0.0.4.295>},4}]
Worker2: I'm still alive
Worker1: I'm still alive
Worker2: I'm still alive
Worker1: I'm still alive
Worker1: I'm still alive
Worker4: I'm still alive
Worker3: I'm still alive
Worker2: I'm still alive
Worker1: I'm still alive
Worker3: I'm still alive
Worker4: I'm still alive
Worker1: I'm still alive
Worker4 (<0.88.0>) went down: killed
Old Workers:
[{{<0.2386.0>,#Ref<0.0.1.416>},3},
 {{<0.85.0>,#Ref<0.0.4.292>},1},
 {{<0.86.0>,#Ref<0.0.4.293>},2},
 {{<0.88.0>,#Ref<0.0.4.295>},4}]
New Workers:
[{{<0.5322.0>,#Ref<0.0.1.9248>},4},
 {{<0.2386.0>,#Ref<0.0.1.416>},3},
 {{<0.85.0>,#Ref<0.0.4.292>},1},
 {{<0.86.0>,#Ref<0.0.4.293>},2}]
Worker3: I'm still alive
Worker2: I'm still alive
Worker4: I'm still alive
Worker1: I'm still alive
Worker3: I'm still alive
Worker3: I'm still alive
Worker2: I'm still alive
Worker1 (<0.85.0>) went down: killed
Old Workers:
[{{<0.5322.0>,#Ref<0.0.1.9248>},4},
 {{<0.2386.0>,#Ref<0.0.1.416>},3},
 {{<0.85.0>,#Ref<0.0.4.292>},1},
 {{<0.86.0>,#Ref<0.0.4.293>},2}]
New Workers:
[{{<0.5710.0>,#Ref<0.0.1.10430>},1},
 {{<0.5322.0>,#Ref<0.0.1.9248>},4},
 {{<0.2386.0>,#Ref<0.0.1.416>},3},
 {{<0.86.0>,#Ref<0.0.4.293>},2}]
Worker2: I'm still alive
Worker3: I'm still alive
Worker4: I'm still alive
Worker3: I'm still alive

我认为下面的版本可能更有效:它使用lists:map()来搜索和替换崩溃的工作者,因此它只遍历工作者列表一次:

-module(mo).
-compile(export_all).

worker(Id) ->
    timer:sleep(1000 * rand:uniform(5)),
    io:format("Worker~w: I'm still alive~n", [Id]),
    worker(Id).

create_workers(N) ->
    Workers = [  % { {Pid, Ref}, Id }
        { spawn_monitor(?MODULE, worker, [Id]), Id }
        || Id <- lists:seq(1,N)
    ],
    monitor_workers(Workers).

monitor_workers(Workers) ->
    receive
        {'DOWN', Ref, process, Pid, Why} ->
            CrashedWorker = {Pid, Ref},
            NewWorkers = replace(CrashedWorker, Workers, Why),
            io:format("Old Workers:~n~p~n", [Workers]),
            io:format("New Workers:~n~p~n", [NewWorkers]),
            monitor_workers(NewWorkers);
        _Other -> 
            monitor_workers(Workers)
    end.

replace(CrashedWorker, Workers, Why) ->
    lists:map(fun(PidRefId) ->
                      { {Pid,_Ref}=Worker, Id} = PidRefId,
                      case Worker =:= CrashedWorker of
                          true ->  %replace worker
                              io:format("Worker~w (~w) went down: ~s~n", 
                                        [Id, Pid, Why]),
                              {spawn_monitor(?MODULE, worker, [Id]), Id}; %=> { {Pid,Ref}, Id }
                          false ->  %leave worker alone
                              PidRefId  
                      end
              end,
              Workers).

start() ->
    observer:start(),  %%In the Processes tab, you can right click on a worker and kill it.
    create_workers(4).
  

如果是这样,那么如果其中一个产生的监视器失败/崩溃会发生什么?

Erlang拥有不同国家的多个服务器群,而erlang已经获得了多个冗余电网,因此elrang将在容错,分布式系统中重启所有内容,永不失败。它都是内置的。你不必担心任何事情。 :)

实际上......在任何你可以想象失败的地方,然后必须备份,例如通过另一台计算机上的另一个监控过程。

答案 1 :(得分:0)

请勿生成然后进行监控,这会过去导致生产问题,而是使用spawn_monitor

您可以从主管开始和监控多个流程,如果您查看monitor上的文档,您会注意到每次受监控的流程死亡时,它都会发送如下消息:

{'DOWN', MonitorRef, Type, Object, Info}

监视正在监视刚刚死亡的进程的主管进程

然后您可以决定要做什么, MonitorRef 是您在开始监控流程时获得的参考,对象将拥有流程的Pid如果您为其命名,则为已注册的名称。

使用监视器创建一些示例代码是一个很好的练习,但是请尝试坚持使用OTP库和OTP Supervisor。