免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2884 | 回复: 5
打印 上一主题 下一主题

[函数] gen_server:enter_loop [复制链接]

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-12 22:34 |只看该作者 |倒序浏览
enter_loop(Module, Options, State)
enter_loop(Module, Options, State, ServerName)
enter_loop(Module, Options, State, Timeout)
enter_loop(Module, Options, State, ServerName, Timeout)

Types:

Module = atom()
Options = [Option]
Option = {debug,Dbgs}
  Dbgs = [Dbg]
   Dbg = trace | log | statistics
    | {log_to_file,FileName} | {install,{Func,FuncState}}
State = term()
ServerName = {local,Name} | {global,GlobalName} | {via,Module,ViaName}
Name = atom()
GlobalName = ViaName = term()
Timeout = int() | infinity
Makes an existing process into a gen_server. Does not return, instead the calling process will enter the gen_server receive loop and become a gen_server process. The process must have been started using one of the start functions in proc_lib, see proc_lib(3). The user is responsible for any initialization of the process, including registering a name for it.

This function is useful when a more complex initialization procedure is needed than the gen_server behaviour provides.

Module, Options and ServerName have the same meanings as when calling gen_server:start[_link]/3,4. However, if ServerName is specified, the process must have been registered accordingly before this function is called.

State and Timeout have the same meanings as in the return value of Module:init/1. Also, the callback module Module does not need to export an init/1 function.

Failure: If the calling process was not started by a proc_lib start function, or if it is not registered according to ServerName.

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
2 [报告]
发表于 2014-11-12 22:35 |只看该作者
没看懂

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
3 [报告]
发表于 2014-11-12 22:43 |只看该作者
  1. enter_loop(Mod, Options, State) ->
  2.     enter_loop(Mod, Options, State, self(), infinity).

  3. enter_loop(Mod, Options, State, ServerName = {Scope, _})
  4.   when Scope == local; Scope == global ->
  5.     enter_loop(Mod, Options, State, ServerName, infinity);

  6. enter_loop(Mod, Options, State, ServerName = {via, _, _}) ->
  7.     enter_loop(Mod, Options, State, ServerName, infinity);

  8. enter_loop(Mod, Options, State, Timeout) ->
  9.     enter_loop(Mod, Options, State, self(), Timeout).

  10. enter_loop(Mod, Options, State, ServerName, Timeout) ->
  11.     Name = get_proc_name(ServerName),
  12.     Parent = get_parent(),
  13.     Debug = debug_options(Name, Options),
  14.     loop(Parent, Name, State, Mod, Timeout, Debug).
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
4 [报告]
发表于 2014-11-12 22:44 |只看该作者
  1. %%% ---------------------------------------------------
  2. %%% Initiate the new process.
  3. %%% Register the name using the Rfunc function
  4. %%% Calls the Mod:init/Args function.
  5. %%% Finally an acknowledge is sent to Parent and the main
  6. %%% loop is entered.
  7. %%% ---------------------------------------------------
  8. init_it(Starter, self, Name, Mod, Args, Options) ->
  9.     init_it(Starter, self(), Name, Mod, Args, Options);
  10. init_it(Starter, Parent, Name0, Mod, Args, Options) ->
  11.     Name = name(Name0),
  12.     Debug = debug_options(Name, Options),
  13.     case catch Mod:init(Args) of
  14.         {ok, State} ->
  15.             proc_lib:init_ack(Starter, {ok, self()}),             
  16.             loop(Parent, Name, State, Mod, infinity, Debug);
  17.         {ok, State, Timeout} ->
  18.             proc_lib:init_ack(Starter, {ok, self()}),             
  19.             loop(Parent, Name, State, Mod, Timeout, Debug);
  20.         {stop, Reason} ->
  21.             %% For consistency, we must make sure that the
  22.             %% registered name (if any) is unregistered before
  23.             %% the parent process is notified about the failure.
  24.             %% (Otherwise, the parent process could get
  25.             %% an 'already_started' error if it immediately
  26.             %% tried starting the process again.)
  27.             unregister_name(Name0),
  28.             proc_lib:init_ack(Starter, {error, Reason}),
  29.             exit(Reason);
  30.         ignore ->
  31.             unregister_name(Name0),
  32.             proc_lib:init_ack(Starter, ignore),
  33.             exit(normal);
  34.         {'EXIT', Reason} ->
  35.             unregister_name(Name0),
  36.             proc_lib:init_ack(Starter, {error, Reason}),
  37.             exit(Reason);
  38.         Else ->
  39.             Error = {bad_return_value, Else},
  40.             proc_lib:init_ack(Starter, {error, Error}),
  41.             exit(Error)
  42.     end.
复制代码

论坛徽章:
27
水瓶座
日期:2014-08-22 21:06:34程序设计版块每日发帖之星
日期:2015-11-25 06:20:0015-16赛季CBA联赛之新疆
日期:2015-12-19 19:05:48IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:31IT运维版块每日发帖之星
日期:2015-12-25 06:20:3315-16赛季CBA联赛之上海
日期:2016-04-15 19:51:31程序设计版块每日发帖之星
日期:2016-04-17 06:23:29程序设计版块每日发帖之星
日期:2016-04-23 06:20:00程序设计版块每日发帖之星
日期:2016-05-26 06:20:00每日论坛发贴之星
日期:2016-05-26 06:20:0015-16赛季CBA联赛之辽宁
日期:2017-02-16 23:59:47
5 [报告]
发表于 2014-11-12 22:44 |只看该作者
啊,是这么来的

论坛徽章:
0
6 [报告]
发表于 2014-11-17 08:42 |只看该作者
最近没有人在这里发帖子了。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP