- 浏览: 44827 次
- 性别:
- 来自: 广州
文章分类
一、supervisor监督者
重启策略:
one_for_one 只影响当前child
one_for_all - 影响所有children
rest_for_one - 影响当前和"rest" order 后面的child
simple_one_for_one - 所有的child运行同一个模块
delete_child/2 and restart_child/2 are invalid for simple_one_for_one
child_spec() = {Id, StartFunc, Restart, Shutdown, Type, Modules}
Id -> 标示符.
StartFunc -> {M,F,A}
Restart :permanent | transient | temporary
permanent -> always be restarted
temporary -> never be restarted
transient -> 非正常死亡是会重启,exit
即exit reason 不是 normal, shutdown or {shutdown,Term}之一.
Shutdown = brutal_kill | int()>0 | infinity
Type = worker | supervisor
Modules = [Module] | dynamic 用于代码升级
创建并链接监督者进程:
start_link(Module, Args) ->
start_link(SupName, Module, Args) -> // 注册进程名SupName
SupName -> {local, Name::atom()} | {global, Name::atom()}
Args 当做回调模块 Module:init/1 的参数.
如果有child进程启动失败,那么监督者进程也会启动失败
启动子进程:
start_child(SupRef, ChildSpec) -> startchild_ret()
SupRef -> pid | name | {name, node} | {global, name} 表示监督者进程
当 simple_one_for_one , ChildSpec 表示一个参数List, apply(M, F, A++List)
当 others, ChildSpec 才表示子进程的规范,如果规范Id已经存在,那么会报错。
终止子进程:
terminate_child(SupRef, Id) -> Result
当 simple_one_for_one, Id表示子进程的pid
当 others 表示 规范Id.
删除规范Id (前提SpecId对应的进程已经终止)
delete_child(SupRef, Id) -> Result
重启子进程:
restart_child(SupRef, Id) -> Result
回调函数:
Module:init(Args) -> Result
Result -> {ok,{{RestartStrategy, MaxR, MaxT},[ChildSpec]}} | ignore
MaxR and MaxT 表示在MaxT时间重启的次数不能超过 MaxR
二、gen_server通用服务器
gen_server:start_link -----> Module:init/1
gen_server:call -----> Module:handle_call/3
gen_server:cast -----> Module:handle_cast/2
- -----> Module:handle_info/2
- -----> Module:terminate/2
- -----> Module:code_change/3
如果回调函数fail或返回bad value,那么gen_server将会终止.
a gen_server does not trap exit signals automatically, 必须要手动设置.
启动并链接gen_server:
start_link(Module, Args, Options) -> Result // 不注册别名
start_link(ServerName, Module, Args, Options) -> Result //
// ServerName -> {local, name} | {global, name}
同步调用:
call(ServerRef, Request) -> Reply
call(ServerRef, Request, Timeout) -> Reply
该调用等待 a reply arrives or a timeout occurs 才返回。
异步调用:
cast(ServerRef, Request) -> ok
就算gen_server不存在,也会立刻返回 ok.
初始化回调:Module:init(Args) -> Result
Result = {ok,State} | {ok,State,Timeout} | {stop,Reason} | ignore
Timeout 为0则立刻触发一个 timeout消息。 为infinity则不会触发timeout。(毫秒)
注:handle_info 处理timeout消息
同步回调:
Module:handle_call(Request, From, State) -> Result
Result = {reply,Reply,NewState} | {reply,Reply,NewState,Timeout}
| {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,Reply,NewState} | {stop,Reason,NewState}
From 表示发送者的进程pid或注册名
State is the internal state of the gen_server.
reply 表示 Reply 作为 gen_server:call的返回值
noreply 表示 Reply表示显示地调用 gen_server:reply/2.
stop 表示 gen_server 将会调用 terminate(Reason, NewState) 进而终止.
NewState 更新后的 internal state
异步回调:
Module:handle_cast(Request, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,NewState}
Timeout等消息回调:
Module:handle_info(Info, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,NewState}
终止回调:
Module:terminate(Reason, State)
Reason = normal | shutdown | {shutdown,term()} | term()
当回调返回stop项式或产生异常,会体现在Reason里.
代码更新回调:
Module:code_change(OldVsn, State, Extra) -> {ok, NewState} | {error, Reason}
三、 application应用程序
加载 app规范 (包括依赖App的, 但不是加载代码) :
load(AppDescr) -> ok | {error, Reason}
load(AppDescr, Distributed) -> ok | {error, Reason}
application controller控制者,
AppDescr 可以是应用的名字 或者 应用规范的项式
启动应用:
start(Application) -> ok | {error, Reason}
start(Application, Type) -> ok | {error, Reason}
The application controller then creates an application master for the application. The application master is the group leader of all the processes in the application.
The application master starts the application by calling the application callback function Module:start/2 as defined by the application specification key mod.
停止应用:
stop(Application) -> ok | {error, Reason}
获取配置变量 (通过.app中配置):
get_env(Par) -> undefined | {ok, Val}
get_env(Application, Par) -> undefined | {ok, Val}
应用.app文件:在ebin目录下.
重启策略:
one_for_one 只影响当前child
one_for_all - 影响所有children
rest_for_one - 影响当前和"rest" order 后面的child
simple_one_for_one - 所有的child运行同一个模块
delete_child/2 and restart_child/2 are invalid for simple_one_for_one
child_spec() = {Id, StartFunc, Restart, Shutdown, Type, Modules}
Id -> 标示符.
StartFunc -> {M,F,A}
Restart :permanent | transient | temporary
permanent -> always be restarted
temporary -> never be restarted
transient -> 非正常死亡是会重启,exit
即exit reason 不是 normal, shutdown or {shutdown,Term}之一.
Shutdown = brutal_kill | int()>0 | infinity
Type = worker | supervisor
Modules = [Module] | dynamic 用于代码升级
创建并链接监督者进程:
start_link(Module, Args) ->
start_link(SupName, Module, Args) -> // 注册进程名SupName
SupName -> {local, Name::atom()} | {global, Name::atom()}
Args 当做回调模块 Module:init/1 的参数.
如果有child进程启动失败,那么监督者进程也会启动失败
启动子进程:
start_child(SupRef, ChildSpec) -> startchild_ret()
SupRef -> pid | name | {name, node} | {global, name} 表示监督者进程
当 simple_one_for_one , ChildSpec 表示一个参数List, apply(M, F, A++List)
当 others, ChildSpec 才表示子进程的规范,如果规范Id已经存在,那么会报错。
终止子进程:
terminate_child(SupRef, Id) -> Result
当 simple_one_for_one, Id表示子进程的pid
当 others 表示 规范Id.
删除规范Id (前提SpecId对应的进程已经终止)
delete_child(SupRef, Id) -> Result
重启子进程:
restart_child(SupRef, Id) -> Result
回调函数:
Module:init(Args) -> Result
Result -> {ok,{{RestartStrategy, MaxR, MaxT},[ChildSpec]}} | ignore
MaxR and MaxT 表示在MaxT时间重启的次数不能超过 MaxR
二、gen_server通用服务器
gen_server:start_link -----> Module:init/1
gen_server:call -----> Module:handle_call/3
gen_server:cast -----> Module:handle_cast/2
- -----> Module:handle_info/2
- -----> Module:terminate/2
- -----> Module:code_change/3
如果回调函数fail或返回bad value,那么gen_server将会终止.
a gen_server does not trap exit signals automatically, 必须要手动设置.
启动并链接gen_server:
start_link(Module, Args, Options) -> Result // 不注册别名
start_link(ServerName, Module, Args, Options) -> Result //
// ServerName -> {local, name} | {global, name}
同步调用:
call(ServerRef, Request) -> Reply
call(ServerRef, Request, Timeout) -> Reply
该调用等待 a reply arrives or a timeout occurs 才返回。
异步调用:
cast(ServerRef, Request) -> ok
就算gen_server不存在,也会立刻返回 ok.
初始化回调:Module:init(Args) -> Result
Result = {ok,State} | {ok,State,Timeout} | {stop,Reason} | ignore
Timeout 为0则立刻触发一个 timeout消息。 为infinity则不会触发timeout。(毫秒)
注:handle_info 处理timeout消息
同步回调:
Module:handle_call(Request, From, State) -> Result
Result = {reply,Reply,NewState} | {reply,Reply,NewState,Timeout}
| {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,Reply,NewState} | {stop,Reason,NewState}
From 表示发送者的进程pid或注册名
State is the internal state of the gen_server.
reply 表示 Reply 作为 gen_server:call的返回值
noreply 表示 Reply表示显示地调用 gen_server:reply/2.
stop 表示 gen_server 将会调用 terminate(Reason, NewState) 进而终止.
NewState 更新后的 internal state
异步回调:
Module:handle_cast(Request, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,NewState}
Timeout等消息回调:
Module:handle_info(Info, State) -> Result
Result = {noreply,NewState} | {noreply,NewState,Timeout}
| {stop,Reason,NewState}
终止回调:
Module:terminate(Reason, State)
Reason = normal | shutdown | {shutdown,term()} | term()
当回调返回stop项式或产生异常,会体现在Reason里.
代码更新回调:
Module:code_change(OldVsn, State, Extra) -> {ok, NewState} | {error, Reason}
三、 application应用程序
加载 app规范 (包括依赖App的, 但不是加载代码) :
load(AppDescr) -> ok | {error, Reason}
load(AppDescr, Distributed) -> ok | {error, Reason}
application controller控制者,
AppDescr 可以是应用的名字 或者 应用规范的项式
启动应用:
start(Application) -> ok | {error, Reason}
start(Application, Type) -> ok | {error, Reason}
The application controller then creates an application master for the application. The application master is the group leader of all the processes in the application.
The application master starts the application by calling the application callback function Module:start/2 as defined by the application specification key mod.
停止应用:
stop(Application) -> ok | {error, Reason}
获取配置变量 (通过.app中配置):
get_env(Par) -> undefined | {ok, Val}
get_env(Application, Par) -> undefined | {ok, Val}
应用.app文件:在ebin目录下.
发表评论
-
erl_scan模块
2014-09-11 11:46 0string(String) -> Return str ... -
lists
2014-09-02 11:22 0all(Pred, List) -> boolean() ... -
file
2014-09-02 10:57 0open(File, Modes) -> {ok, Io ... -
erlang内存管理
2014-09-01 19:22 887垃圾回收器的本质实际上是改变存活数据结构构成图的连通性. 堆对 ... -
net
2014-08-28 19:17 316gen_tcp模块: 主要的选项: option() = { ... -
erlang内置函数
2014-08-27 20:32 466123 表示数字 '123' 表示原子 "12 ... -
shell交互
2014-08-27 17:33 401在erlang shell中可使用的函数 b() Print ... -
binary二进制
2014-08-27 17:24 917Bit语法把二进制数据放在 << 和 >&g ... -
ets表
2014-08-27 11:28 255access() = public | protected | ... -
EMakefile规则
2014-08-20 22:07 381格式: { [ "src/*&qu ... -
mnesia要点
2014-08-20 22:02 0关键词: DBMS relational data ... -
Erlang系统运行
2014-08-15 14:48 4551, 启动系统 erl erl 解释执行从命令行方式输入 ... -
erlang代码更新知识
2014-08-11 16:46 487代码版本:--简单理解模块版本 Erlang允许加载的代码有两 ... -
erlang备忘
2014-08-11 14:52 272erlang 项目结构 /src /include /ebi ... -
erlang在debian下vim环境设置
2014-08-08 20:19 3921, 安装vunble 2, 配置~/.vim.rc, 由v ...
相关推荐
Match:在JavaScript中实现类似于Erlang的模式匹配
Tamale: Lua的erlang风格模式匹配库
erlang入门电子书 erlang编程 Introducing Erlang,作者Simon.St.Laurent
erlang 安装包
Erlang及其应用Erlang及其应用Erlang及其应用
erlang25.0 windows版本
erlang otp25 win安装包
ErlangB和ErlangC计算工具(exe可执行文件+excel两个) ErlangB和ErlangC计算工具(exe可执行文件+excel两个)
%% 优先级选择节点,从左到右遍历子节点,若子节点的准入条件符合信息则执行该子节点-define(EBT_SELECTOR, ebt_priority_selector).%% 行为节点,具体的游戏逻辑放在这个节点里面-define(EBT_ACTION, ebt_action)....
erlang22最新下载包 erlang22.1.tar.gz erlang22最新下载包 erlang22最新下载包
Erlang并发编程,Erlang程序设计,Erlang中文手册。 学习erlang的好资料。 Erlang是一个结构化,动态类型编程语言,内建并行计算支持。最初是由爱立信专门为通信应用设计的,比如控制交换机或者变换协议等,因此...
erlang 中文基础教程erlang 中文基础教程
RabbitMQ version Minimum required Erlang/OTP Maximum supported Erlang/OTP Notes 3.13.0 26.0 26.2.x The 3.13 release series is compatible wtih Erlang 26. OpenSSL 3 support in Erlang is considered to ...
erlang安装包
erlang四大behaviour之四-supervisor
erlang资源,非常值得下载,二郎学习
erlang文献及资料汇总 入门资料: erlang中文手册(R11B 文档译文,最适合入门) erlang位运算与二进制解析 erlang二进制高效编程 erlang异常处理详解 开发经验: 面对软件错误构建可靠的分布式系统 编写分布式的 ...
Programming Erlang
rabbitmq,erlang安装包
Erlang特性: ● 并发性 - Erlang支持超大量级的并发进程,并且不需要操作系统具有并发机制。 ● 分布式 - 一个分布式Erlang系统是多个Erlang节点组成的网络(通常每个处理器被作为一个节点) ● 健壮性 - Erlang...