您的位置:首页 > 其它

Erlang使用相关笔记

2015-06-16 23:24 423 查看
#从源码编译安装Erlang
1. wget http://www.erlang.org/download/otp_src_r16b.tar.gz -p /usr/local/src
2. tar zxvf otp_src_r16b.tar.gz -c /usr/local/src
3. cd otp_src_r16b
4. ./configure --prefix=/usr/local/erlang
5. make
6. make install
7. 将"/usr/local/erlang/bin"加入PATH

#ets相关操作 [1]
ets:all() %列出所有的ETS Table
ets:i()   %给出一个ETS Table的清单 包含表的类型,数据量,使用内存,所有者信息
ets:i(tb) %输出ets中tb表的数据

#erlc编译文件到指定目录
erlc -o ../ebin hello_world_app.erl

#导出所有函数
-compile(export_all).

#使用erlang的dbg调试程序 [1] [2] [3]
dbg:tracer().
dbg:p(all,c).
dbg:tpl(test_mod, test_fun, dbg:fun2ms(fun(_) -> exception_trace() end)).
dbg:stop_clear().

%% dbg:tpl(test_mod, '_', dbg:fun2ms(fun(_A) -> [_A,mydebug] end)).

#输出当前函数的调用栈
ZZ = try throw(a)
catch
throw:X ->
{X, erlang:get_stacktrace()}
end,
io:format("get_stacktrace:~p~n", [ZZ]),

#清空绑定变量
f().     %% 清空所有绑定变量,在测试的时候非常有用
f(A).    %% 清空指定的绑定变量A

#erlang中的queue实例
Q = queue:new(),
io:format("====is_queue(Q):~p====~n",[queue:is_queue(Q)]),
Q1 = queue:in(a, Q),
Q1_len = queue:len(Q1),
io:format("====Q1_len:~p====~n",[Q1_len]),
Q2 = queue:in(b, Q1),
Q3 = queue:in(c, Q2),
Q3_len = queue:len(Q3),
io:format("====Q3_len:~p====~n",[Q3_len]),
io:format("====Q3_to_list:~p====~n",[queue:to_list(Q3)]).
输出结果:
====is_queue(Q):true====
====Q1_len:1====
====Q3_len:3====
====Q3_to_list:[a,b,c]====

#OTP中的handle_call的参数说明
handle_call(_Request, _From, State)
From是发送请求的客户端进程的PID,State则是客户端的当前状态。

#rebar.config中包含本地的库
{rsync, "file:///home/alex/projects/cowboy"}

=========================
1> erlang:universaltime().		%% erlang:universaltime/0 这个方法是返回当前系统时间
{{2015,6,17},{1,15,12}}
2>

MaxConns = proplists:get_value(max_connections, TransOpts, 1024).		%% 从 List中查找 Key ,并返回它的值,如果不存在,则使用 Default 值。

MonitorRef = erlang:monitor(process, ConnPid),
  erlang doc:http://www.erlang.org/doc/man/erlang.html#monitor-2
  这边,我还查看了下 《erlang 编程指南》 148页,关于这个函数的用法如下:
    为了单向监控进程,可以把内置函数 erlang:monitor/2 添加到 Erlang中,如下调用:
      erlang:monitor(process, Proc)
    这样就生成了一个调用进程到另一个标记为 Proc 进程的监控进程,Proc 可以是进程标识符也可以是注册的名字。当带有进程标识符的进程终止的时候,消息:
      {'DOWN', MonitorRef, Type, Object, Info} 会发送到监控进程。这个消息包含一个对监控进程的引用。

url解析完后执行cowboy_handler:handler_init/4
如果返回{ok,Req,State}则请求结束并开始返回结果
返回结果前会执行handler()函数,如果这个函数也是返回一个{ok, Req, State}的结果,那cowboy将返回一个"200 OK"的HTTP响应。其他情况返回500的HTTP响应。

如果是rest,则会返回一个{update, protocol, Mod}的结果,比如Mod的值为"cowboy_rest"
接下来经过一个upgrade_protocol/2中间函数后会执行Module:upgrade/4函数。比如rest中应该是cowboy_rest:upgrade/4
upgrade/4中执行Handler:rest_init/2,如果返回{ok, Req, State}形式的结果则会正常返回值。
service_available/2
expect/6
call/3
...
Handler:rest_terminate/2这个函数最后会调用,应该返回一个{ok, Req, State}的结果cowboy才会返回"200 OK"的HTTP响应。

----------------------
期代的结果是执行terminate_request/5函数,或者执行一个和它返回值相同的函数

erlang:function_exported(Module, Function, Arity) -> boolean()

Types:

Module = module()
Function = atom()
Arity = arity()
Returns true if the module Module is loaded and contains an exported function Function/Arity; otherwise false.
如果Module中存在一个导出的函数"Function/Arity"则返回true,其他的返回false。
Returns false for any BIF (functions implemented in C rather than in Erlang).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: