本文共 1401 字,大约阅读时间需要 4 分钟。
模块 proc_lib 中的函数可以用于实现一种特殊进程,遵照 OTP 设计原则,但不使用标准行为。它们也可以用于实现用户自定义的(非标准)行为。
1 2 3 4 5 6 | -module(proc_lib). %% This module is used to set some initial information %% in each created process. %% Then a process terminates the Reason is checked and %% a crash report is generated if the Reason was not expected. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | spawn(F) when is_function(F) -> Parent = get_my_name(), Ancestors = get_ancestors(), erlang:spawn(?MODULE, init_p, [Parent,Ancestors,F]). init_p(Parent, Ancestors, Fun) when is_function(Fun) -> put( '$ancestors' , [Parent|Ancestors]), {module,Mod} = erlang:fun_info(Fun, module), {name,Name} = erlang:fun_info(Fun, name), {arity,Arity} = erlang:fun_info(Fun, arity), put( '$initial_call' , {Mod,Name,Arity}), try Fun() catch Class:Reason -> exit_p(Class, Reason) end. |
转载地址:http://ylcia.baihongyu.com/