- 浏览: 578093 次
- 性别:
- 来自: 广州杭州
文章分类
最新评论
-
bohc:
谢谢,搞了两天了,现在才算是找到问题所在,解决了。
文件在使用FileChannel.map后不能被删除(Windows上) -
zhang0000jun:
在jdk1.8中执行正好和楼主的结果相反,请指教
从Java视角理解CPU缓存(CPU Cache) -
在世界的中心呼喚愛:
forenroll 写道请问楼主的那个分析工具cachemis ...
从Java视角理解CPU缓存(CPU Cache) -
xgj1988:
我这里打出的结果是: 0 L1-dcache-load-mis ...
从Java视角理解CPU缓存(CPU Cache) -
thebye85:
请教下大神,为什么频繁的park会导致大量context sw ...
从Java视角理解CPU上下文切换(Context Switch)
1 Overview
OTP 设计原则 是一套教你如何运用进程,模块和目录等条件来组织Erlang代码的原则.
1.1 Supervision Trees 管理树
A basic concept in Erlang/OTP is the supervision tree. This is a process structuring model based on the idea of workers and supervisors.
Erlang/OTP中的一个基本概念就是 管理树. 这是一个以工作者和管理者思想为基础的进程结构模型.
- Workers are processes which perform computations, that is, they do the actual work.
工作者是完成计算的进程,也就是说,它们干实际的活。 - Supervisors are processes which monitor the behaviour of workers. A supervisor can restart a worker if something goes wrong.
管理者是监视工作者行为的进程.一个管理者可以 在出错的时候重启一个工作者. - The supervision tree is a hierarchical arrangement of code into supervisors and workers, making it possible to design and program fault-tolerant software.
管理树是一些管理者和工作者的分层排布代码.它使得设计和编写容错软件成为可能.
Supervision Tree 管理树
In the figure above, square boxes represents supervisors and circles represent workers.
上图中, 方块代表管理者, 圆代表工作者.
1.2 Behaviours 行为
In a supervision tree, many of the processes have similar structures, they follow similar patterns. For example, the supervisors are very similar in structure. The only difference between them is which child processes they supervise. Also, many of the workers are serves in a server-client relation, finite state machines, or event handlers such as error loggers.
在一棵管理树中, 许多进程都有相似的结构,它们遵循相似的模式.例如, 所有管理者的结构非常相似.它们之间唯一不同点就是具体管理哪些子进程.同样, 许多工作者都在服务-客户关系, 有限状态机或事件处理中提供服务.
Behaviours are formalizations of these common patterns. The idea is to divide the code for a process in a generic part (a behaviour module) and a specific part (a callback module).
行为形式化了这些共同模式.它的思想是把进程的代码分离成一普通部分(一个行为模块)和一特殊部分(一个回调模块).
The behaviour module is part of Erlang/OTP. To implement a process such as a supervisor, the user only has to implement the callback module which should export a pre-defined set of functions, the callback functions.
行为模块是Erlang/OTP的一部分。 实现像管理者这样的进程, 用户只须实现回调模块.回调模块要求输出一套预定义函数,这些函数叫做回调函数。
An example to illustrate how code can be divided into a generic and a specific part: Consider the following code (written in plain Erlang) for a simple server, which keeps track of a number of "channels". Other processes can allocate and free the channels by calling the functions alloc/0
and free/1
, respectively.
代码
- -module(ch1).
- -export([start/0]).
- -export([alloc/0, free/1]).
- -export([init/0]).
- start() ->
- spawn(ch1, init, []).
- alloc() ->
- ch1 ! {self(), alloc},
- receive
- {ch1, Res} ->
- Res
- end.
- free(Ch) ->
- ch1 ! {free, Ch},
- ok.
- init() ->
- register(ch1, self()),
- Chs = channels(),
- loop(Chs).
- loop(Chs) ->
- receive
- {From, alloc} ->
- {Ch, Chs2} = alloc(Chs),
- From ! {ch1, Ch},
- loop(Chs2);
- {free, Ch} ->
- Chs2 = free(Ch, Chs),
- loop(Chs2)
- end.
The code for the server can be rewritten into a generic part server.erl
:
服务的代码可以写入到一普通部分server.erl
:
代码
- -module(server).
- -export([start/1]).
- -export([call/2, cast/2]).
- -export([init/1]).
- start(Mod) ->
- spawn(server, init, [Mod]).
- call(Name, Req) ->
- Name ! {call, self(), Req},
- receive
- {Name, Res} ->
- Res
- end.
- cast(Name, Req) ->
- Name ! {cast, Req},
- ok.
- init(Mod) ->
- register(Mod, self()),
- State = Mod:init(),
- loop(Mod, State).
- loop(Mod, State) ->
- receive
- {call, From, Req} ->
- {Res, State2} = Mod:handle_call(Req, State),
- From ! {Mod, Res},
- loop(Mod, State2);
- {cast, Req} ->
- State2 = Mod:handle_cast(Req, State),
- loop(Mod, State2)
- end.
and a callback module ch2.erl
:
加上一个加调模块 ch2.erl
:
代码
- -module(ch2).
- -export([start/0]).
- -export([alloc/0, free/1]).
- -export([init/0, handle_call/2, handle_cast/2]).
- start() ->
- server:start(ch2).
- alloc() ->
- server:call(ch2, alloc).
- free(Ch) ->
- server:cast(ch2, {free, Ch}).
- init() ->
- channels().
- handle_call(alloc, Chs) ->
- alloc(Chs). % => {Ch,Chs2}
- handle_cast({free, Ch}, Chs) ->
- free(Ch, Chs). % => Chs2
Note the following:
注解:
- The code in
server
can be re-used to build many different servers.
server
代码可以重用到任何其它不同的服务中. - The name of the server, in this example the atom
ch2
, is hidden from the users of the client functions. This means the name can be changed without affecting them.
服务名称,在这儿是ch2原子.它对其它客户函数的使用来说是不可见的.这意味着名称的修改将不会影响到其它客户函数.
- The protcol (messages sent to and received from the server) is hidden as well. This is good programming practice and allows us to change the protocol without making changes to code using the interface functions.
协议(传送到或接收自服务器的消息)被隐藏很得很好. 这是一个良好的编程,且使我们可以改变协议的同时,不必改变接口函数的代码. - We can extend the functionality of
server
, without having to changech2
or any other callback module.
我们可以扩展server
的功能,而不必改变ch2
或其它任何回调模块.
(In ch1.erl
and ch2.erl
above, the implementation of channels/0
, alloc/1
and free/2
has been intentionally left out, as it is not relevant to the example. For completeness, one way to write these functions are given below. Note that this is an example only, a realistic implementation must be able to handle situations like running out of channels to allocate etc.)
代码
- channels() ->
- {_Allocated = [], _Free = lists:seq(1,100)}.
- alloc({Allocated, [H|T] = _Free}) ->
- {H, {[H|Allocated], T}}.
- free(Ch, {Alloc, Free} = Channels) ->
- case lists:member(Ch, Alloc) of
- true ->
- {lists:delete(Ch, Alloc), [Ch|Free]};
- false ->
- Channels
- end.
Code written without making use of behaviours may be more efficient, but the increased efficiency will be at the expense of generality. The ability to manage all applications in the system in a consistent manner is very important.
Using behaviours also makes it easier to read and understand code written by other programmers. Ad hoc programming structures, while possibly more efficient, are always more difficult to understand.
The module server
corresponds, greatly simplified, to the Erlang/OTP behaviour gen_server
.
The standard Erlang/OTP behaviours are:
实现客户-服务关系的服务端.
实现有限状态机.
实现事件处理功能.
在管理树中,实现管理者.
The compiler understands the module attribute -behaviour(Behaviour)
and issues warnings about missing callback functions. Example:
编译器能识别模块属生-behaviour(Behaviour)
,并会在缺少回调函数时发出警告.例如:
代码
- -module(chs3).
- -behaviour(gen_server).
- ...
- 3> c(chs3).
- ./chs3.erl:10: Warning: undefined call-back function handle_call/3
- {ok,chs3}
1.3 Applications 应用
Erlang/OTP comes with a number of components, each implementing some specific functionality. Components are with Erlang/OTP terminology called applications. Examples of Erlang/OTP applications are Mnesia, which has everything needed for programming database services, and Debugger which is used to debug Erlang programs. The minimal system based on Erlang/OTP consists of the applications Kernel and STDLIB.
Erlang/OTP带有一系列组件,它们分别实现了特定的功能。组件在Erlang/OTP术语中叫做应用.Erlang/OTP应用有众多例子。命例如: Mnesia,它有所有数据库服务编程所需的功能; 调试器,它用来调试Erlang程序;最小系统,它基于Erlang/OTP,由Kernel 与STDLIB应用组成.
The application concept applies both to program structure (processes) and directory structure (modules).
应用概念既适用于程序结构(进程),也适用于目录结构(模块).
The simplest kind of application does not have any processes, but consists of a collection of functional modules. Such an application is called a library application. An example of a library application is STDLIB.
最简单的应用没有任何进程,而是由一套功能模块组成. 这种应用叫做库应用.库应用的一个典型例子就是STDLIB.
An application with processes is easiest implemented as a supervision tree using the standard behaviours.
最容易实现且带有进程的应用,就是用标准行为实现的管理树.
How to program applications is described in Applications.
怎么编写应用,在 Applications中有描述.
1.4 Releases 发行
A release is a complete system made out from a subset of the Erlang/OTP applications and a set of user-specific applications.
一个release 是一个由Erlang/OTP 子集与一些用户特定的应用组成的完整系统
How to program releases is described in Releases.
怎么编写releases请参考 Releases.
How to install a release in a target environment is described in the chapter about Target Systems in System Principles.
怎么在目标环境中安装一个release,Target Systems in System Principles相关的章节有描述.
1.5 Release Handling
Release handling is upgrading and downgrading between different versions of a release, in a (possibly) running system. How to do this is described in Release Handling.
Release handling 是在一个运行系统中,不同版本release之间的升级和降级 ,具体怎么做,Release Handling.有描述.
发表评论
-
ubuntu安装otp R11B 的一些记录
2007-11-16 12:30 2838新的ubuntu系统会缺少一些工具 和lib. 用apt-ge ... -
emulator调试日志: driver篇
2007-10-08 16:35 2334--------- driver篇 ------------- ... -
修正Programming Erlang中linked driver实例的小问题
2007-10-08 14:50 2495也许很多人碰上过, 用example1_lid:sta ... -
emulator调试日志: port篇
2007-10-06 16:14 2421------------------ port 篇 ----- ... -
supervisor一小技巧
2007-09-04 13:20 1866simple_one_for_one可以让supervisor ... -
gen_server
2007-08-29 21:52 1953State用来存数据, 任何erlang term都行 ge ... -
application
2007-08-29 02:01 1786用pman 可以看出application controlle ... -
epmd源码学习
2007-07-26 10:14 2057注: 此处节点是指分布式中分布在各终端的点, 而结点是指存在数 ... -
Tracing和dbg
2007-07-15 21:49 2583代码不必用特殊的标记(比如debug_info)来编译,也可以 ... -
ets,dets与大数据存储
2007-07-15 12:49 4985ets与dets都是用来存大数据的机制 ets是Erl ... -
用telnet来与ejabberd交互
2007-07-11 15:41 3255看了一篇文章,觉得用telnet来调试ejabberd也是一种 ... -
ejabberd管理页面和客户端
2007-07-11 00:23 9794转战到97机器。在ejabber.config加上这么一行. ... -
ejabberd在linux平台的安装与配置
2007-07-05 21:17 11979这些天捣鼓了下ejabberd,准备研究它的代码,做为榜样~ ... -
mnesia相关笔记
2007-06-29 12:17 2370当前版本OTP 5.5的mensia建表的表名可以和记录名不一 ... -
OTP设计原则:应用
2007-06-27 00:32 19747 Applications This chapter sh ... -
erlang网络编程的几个性能调优和注意点
2007-06-26 09:56 17895前些天给echo_server写了 ... -
erlc
2007-06-24 15:08 3881erlc 命令 erlc 概要 编译器 描述 Th ... -
echo_server
2007-06-23 14:45 2476代码 -module(echo_server ... -
OTP设计原则:Supervisor行为
2007-06-22 12:15 27685 Supervisor Behaviour This s ... -
OTP设计原则:Gen_Event 行为
2007-06-22 11:59 20494 Gen_Event 行为 这一章应该与gen_event ...
相关推荐
读书笔记:ErlangOTP设计原则中文文档
OTP设计原则着重于实现高度并发、容错性和高效能。下面将详细讨论Erlang OTP中的关键概念和相关知识点。 1. 监督树(Supervision Tree): 监督树是OTP设计的核心概念,它是一种组织Erlang进程的方式。每个节点都...
mta-otp-docker:Docker容器化MTA OTP前端
VUE OTP输入 Vue的OTP输入组件 演示: : 安装 npm i vue-otp-2 使用范例 在main.js中 import Vue from 'vue' import VueOtp2 from 'vue-otp-2' ; Vue . use ( VueOtp2 ) 在App.vue中 道具 名称 类型 默认 描述 ...
- Phoenix框架与OTP:Phoenix web框架充分利用了OTP,例如使用GenServer管理Web服务器的状态,利用PubSub进行实时通信。 5. 并发与并行计算: - 并发模型:理解Erlang/OTP中的进程并发是如何实现的,以及如何有效...
#otp-generator “ otp-generator”是简单的一次性密码生成器,可以用作密码生成器。 指数 安装 npm install otp-generator --save 用法 var otpGenerator = require ( 'otp-generator' ) otpGenerator . ...
OTP是基于Erlang的一套设计原则和库,它提供了标准的组件和模式,如GenServer、GenEvent和Supervisor等,用于构建可靠的分布式系统。OTP的核心理念是模块化、可复用和容错性,通过这些组件,开发者可以快速构建出...
react-native-otp-fieldReact本机OTP字段 :keycap_1: :keycap_2: :keycap_3: :keycap_4: :keycap_5:安装npm i react-native-otp-fieldRN <0.63 npm i react-native-otp-field@0.0.7一个可在android和iOS上运行的...
OTP控制器版本1.2的项目 特征 通过I2C接口与主机处理器通信 在开始时(引导阶段)或在测试模式下有NVM加载请求时,将OTP值加载到寄存器文件中 在测试模式下编程OTP 文件分发(v1.2) top ------ i2c verilog文件...
OTP管理器(Laravel库) OTP Manager是Laravel OTP库。 通过短信,电子邮件或其他方式通过一次密码验证用户。 您可以选择各种类型的身份验证方法,例如cookie或请求标头。 一些功能: 您可以创建OTP链接并通过电子...
安装要安装最新的稳定版本: npm install --save react-otp-input基本用法: import React , { Component } from 'react' ;import OtpInput from 'react-otp-input' ;export default class App extends Component { ...
OTP视图 准备使用一次性密码组件。 用法 XML格式 < com .hololo.library.otpview.OTPView android : layout_width = " match_parent " android : layout_height = " wrap_content " android : gravity = " ...
为了做到这一点,他们需要熟悉Erlang编程语言,理解 OTP的设计原则,包括进程间的通信、错误处理和容错机制。同时,了解RabbitMQ的工作原理和配置也是必要的,以便有效地集成和利用其消息传递功能。 总结来说,"otp...
Erlang and OTP in Action Martin Logan, Eric Merritt, and Richard Carlsson MEAP Began: August 2008 Softbound print: May 2010 (est.) | 500 pages ISBN: 1933988789 Part One: Getting Past Pure Erlang; ...
uuu.php: 成功== true){otp:echo“ [+] Masukin Kode OTP Kamu Disini:”; $ otp = trim(fgets(STDIN)); $ data1 ='{“ scopes”:“ gojek transaction gojek只读”,“ grant_type”:“密码”,“ login_...
Android OTP提取器许多OTP应用程序不支持导出或备份其OTP机密。 切换应用程序将需要您重新生成所有令牌,如果您拥有很多令牌,这可能很乏味。 该应用程序可以从流行的Android OTP应用程序中提取令牌,并以标准格式...
Firebase OTP (One-Time Password) SMS 是一种常见的身份验证机制,特别是在移动应用中,它用于确保用户的安全登录。在这个场景中,我们使用JavaScript与Firebase的电话认证功能集成,为用户提供通过电话号码接收OTP...
在本压缩包文件“otp.rar”中,包含的“OTP_otp显示程序”是专门针对“DX0906VX”显示屏设计的。DX0906VX是一款可能的LCD或者OLED显示屏,这种类型的显示屏通常需要根据环境光条件和个人偏好进行定制设置,以达到...
Flutter-OTP验证基于Flutter的OTP身份验证组件,用于使用Firebase身份验证通过OTP(一次性密码)验证您的手机号码。目录颤振支持版本-Flutter 1.17(稳定) 我们已经在上述版本中测试了我们的程序,但是您也可以在...
OTP认证符合以下条件的Sapplice Appplicazione che lavora e salva i dati localmente: 通过OTP APP生成Genericione dei QR di configurazione Generazione codice OTP Verifica correto allineamento codici OTP L...