当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


erlang monitor(Type :: process, Item :: monitor_process_identifier(), Opts :: [monitor_option()])用法及代码示例


monitor(Type :: process,
        Item :: monitor_process_identifier(),
        Opts :: [monitor_option()]) ->
           MonitorRef
OTP 24.0
monitor(Type :: port,
        Item :: monitor_port_identifier(),
        Opts :: [monitor_option()]) ->
           MonitorRef
OTP 24.0
monitor(Type :: time_offset,
        Item :: clock_service,
        Opts :: [monitor_option()]) ->
           MonitorRef
OTP 24.0
类型:
MonitorRef = reference()
registered_name() =atom()
registered_process_identifier() =
registered_name() | {registered_name(),node()}

monitor_process_identifier() =
pid()| registered_process_identifier()

monitor_port_identifier() =port()| registered_name()

提供用于修改监控函数的选项列表monitor/2。这TypeItem参数与传递给时具有相同的含义monitor/2。目前可用的选项:


{alias, UnaliasOpt}

返回的监视器引用也将成为调用进程的别名。也就是说,返回的引用可用于向调用进程发送消息。也可以看看alias()。这UnaliasOpt确定应如何停用别名。


explicit_unalias

仅显式调用unalias/1将停用别名。


demonitor

当监视器被移除时,别名将自动停用。这可以通过显式调用demonitor/1或者当它与以下内容同时自动删除时'DOWN'由于监视器的原因,消息被传递。别名仍然可以通过调用来停用unalias/1.


reply_demonitor

当监视器被移除(参见上面的 demonitor 选项)或收到通过别名发送的回复消息时,别名将自动停用。当通过别名收到回复消息时,监视器也将自动删除。当客户端监视服务器并通过别名获取回复时,这在客户端/服务器场景中非常有用。一旦收到响应,无论响应是回复还是 'DOWN' 消息,别名和监视器都将自动删除。别名仍然可以通过调用 unalias/1 来停用。请注意,如果使用 unalias/1 BIF 删除别名,监视器仍将保持活动状态。

例子:

server() ->
    receive
        {request, AliasReqId, Request} ->
            Result = perform_request(Request),
            AliasReqId ! {reply, AliasReqId, Result}
    end,
    server().

client(ServerPid, Request) ->
    AliasMonReqId = monitor(process, ServerPid, [{alias, reply_demonitor}]),
    ServerPid ! {request, AliasMonReqId, Request},
    %% Alias as well as monitor will be automatically deactivated if we
    %% receive a reply or a 'DOWN' message since we used 'reply_demonitor'
    %% as unalias option...
    receive
        {reply, AliasMonReqId, Result} ->
            Result;
        {'DOWN', AliasMonReqId, process, ServerPid, ExitReason} ->
            error(ExitReason)
    end.

请注意,此示例中的服务器和客户端都必须至少在 OTP 24 系统上执行才能使其正常工作。

有关进程别名的更多信息,请参阅进程别名的部分Erlang参考手册.


{tag, UserDefinedTag}

替换默认值TagUserDefinedTag在里面监听消息当监视器被触发时传递。例如,当监控一个进程时,'DOWN'向下消息中的标签将被替换为UserDefinedTag.

一个例子说明如何{tag, UserDefinedTag}可以使用选项来启用新的选择性接收优化,在 OTP 24 中引入,当向不同服务器发出多个请求时:

server() ->
    receive
        {request, From, ReqId, Request} ->
            Result = perform_request(Request),
            From ! {reply, self(), ReqId, Result}
    end,
    server().

client(ServerPids, Request) when is_list(ServerPids) ->
    ReqId = make_ref(),
    lists:foreach(fun (ServerPid) ->
                          _ = monitor(process, ServerPid,
                                      [{tag, {'DOWN', ReqId}}]),
                          ServerPid ! {request, self(), ReqId, Request}
                  end,
                  ServerPids),
    receive_replies(ReqId, length(ServerPids), []).

receive_replies(_ReqId, 0, Acc) ->
    Acc;
receive_replies(ReqId, N, Acc) ->
    %% The compiler will detect that we match on the 'ReqId'
    %% reference in all clauses, and will enable the selective
    %% receive optimization which makes the receive able to
    %% skip past all messages present in the message queue at
    %% the time when the 'ReqId' reference was created...
    Res = receive
              {reply, ServerPid, ReqId, Result} ->
                  %% Here we typically would have deactivated the
                  %% monitor by a call to demonitor(Mon, [flush]) but
                  %% we ignore this in this example for simplicity...
                  {ok, ServerPid, Result};
              {{'DOWN', ReqId}, _Mon, process, ServerPid, ExitReason} ->
                  {error, ServerPid, ExitReason}
          end,
    receive_replies(ReqId, N-1, [Res | Acc]).

为了使该示例按预期工作,客户端必须至少在 OTP 24 系统上执行,但服务器可以在较旧的系统上执行。

相关用法


注:本文由纯净天空筛选整理自erlang.org大神的英文原创作品 monitor(Type :: process, Item :: monitor_process_identifier(), Opts :: [monitor_option()]) -> MonitorRef。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。