Erlang入门:进程实例操作与练习2

%% @author Rolong<[email protected]>

-module(process2).
-compile(export_all).
-include("common.hrl").

%% Usage:
%% 启动服务:process2:start().
%% 查看时间:myserver ! now.
%% 结束服务:myserver ! stop.

start()->
    Pid = spawn(?MODULE, server, []),
    %% 给服务注册一个名字:myserver
    erlang:register(myserver, Pid).

server() ->
    receive
        Msg ->
            ?MODULE:handle(Msg),
            server()
    end.

handle(now) ->
    ?I("Current time is: ~p", [erlang:localtime()]);

handle(stop) ->
    ?I("stopping ..."),
    exit(normal);

handle(Msg) ->
    ?I("received undefined message:~p", [Msg]).

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。