Data structures 过程

Data structures 过程,data-structures,erlang,ets,Data Structures,Erlang,Ets,persistent_元素是全局的,它仅与节点一起消亡。它可以由任何进程访问,无需控制 ETS由一个进程拥有,它可以由不同的进程共享,它与所有者一起消亡,所有权可以授予另一个进程,它提供一些访问控制(公共、私有、受保护)和不同的组织类型和访问 shell中的一个简短会话显示了(大部分)这些差异 12> persistent_term:put(global,value1). ok 13> put(local,value2). % with process_dictionary, put

persistent_元素是全局的,它仅与节点一起消亡。它可以由任何进程访问,无需控制

ETS由一个进程拥有,它可以由不同的进程共享,它与所有者一起消亡,所有权可以授予另一个进程,它提供一些访问控制(公共、私有、受保护)和不同的组织类型和访问

shell中的一个简短会话显示了(大部分)这些差异

12> persistent_term:put(global,value1).
ok
13> put(local,value2). % with process_dictionary, put returns the previous value associated to the key
undefined
14> ets:new(my_ets,[named_table,public]). % create a public table
my_ets
15> ets:insert(my_ets,{shared,value3,other_values}).
true
16> persistent_term:get(global).  % check that everything is stored                  
value1
17> get(local).                 
value2
18> ets:lookup(my_ets,shared).
[{shared,value3,other_values}]
19> ets:lookup_element(my_ets,shared,2). % a small example of ETS extended capabilities
value3
20> F1 = fun(From) -> From ! persistent_term:get(global) end. % prepare the same functions to be executed from external process
#Fun<erl_eval.44.97283095>
21> 
21> F2 = fun(From) -> From ! get(local) end.                  
#Fun<erl_eval.44.97283095>
22> F3 = fun(From) -> From ! ets:lookup(my_ets,shared) end.
#Fun<erl_eval.44.97283095>
23> Me = self().
<0.19320.1>
24> spawn(fun() -> F1(Me) end).
<0.12906.2>
25> flush(). % persistent_term are global
Shell got value1
ok
26> spawn(fun() -> F2(Me) end).
<0.13701.2>
27> flush(). % prrocess_dictionary is local                  
Shell got undefined
ok
28> spawn(fun() -> F3(Me) end).
<0.13968.2>
29> flush().  % ETS can be shared                 
Shell got [{shared,value3,other_values}]
ok
30> 1/0. % create an exception so the shell dies and is restarted by its supervisor
** exception error: an error occurred when evaluating an arithmetic expression
     in operator  '/'/2
        called as 1 / 0
31> Me = self().  % the shell's Pid changed             
** exception error: no match of right hand side value <0.14499.2>
32> persistent_term:get(global).    % persistent are still there                          
value1
33> get(local).  % Oooops! the process dictionary is still there too. Warning, this is a side effect of the shell implementation                                             
value2
34> ets:lookup(my_ets,shared).  % my_ets does not exist anymore                      
** exception error: bad argument
     in function  ets:lookup/2
        called as ets:lookup(my_ets,shared)
35> 
12>持久性术语:put(全局,值1)。
好啊
13> 放置(本地,值2)。%使用process_dictionary,put返回与键关联的上一个值
未定义
14> ets:新(我的ets,[命名的表格,公共])。%创建一个公共表
我的孩子
15> ets:插入(my_ets,{shared,value3,other_values})。
真的
16> 持久_项:获取(全局)。%检查所有物品是否已存储
价值1
17> 获取(本地)。
价值2
18> ets:查找(我的ets,共享)。
[{共享,值3,其他值}]
19> ets:查找元素(我的ets,共享,2)。%ETS扩展功能的一个小例子
价值3
20> F1=乐趣(来自)->来自!持久性_术语:获取(全局)结束。%准备从外部流程执行的相同功能
#乐趣
21> 
21>F2=乐趣(来自)->来自!获得(本地)结束。
#乐趣
22>F3=乐趣(来自)->来自!ets:查找(我的集合,共享)结束。
#乐趣
23>我=自我()。
24>繁殖(乐趣()->F1(Me)结束)。
25>冲洗()。%持久性术语是全局的
壳牌获得了价值1
好啊
26>繁殖(乐趣()->F2(自我)结束)。
27>冲洗()。%PRPROCESS_字典是本地的
Shell没有定义
好啊
28>繁殖(乐趣()->F3(自我)结束)。
29>刷新()。%ETS可以共享
Shell获得[{共享,值3,其他值}]
好啊
30> 1/0. % 创建一个异常,使shell死亡并由其主管重新启动
**异常错误:计算算术表达式时出错
在运算符“/”中/2
称为1/0
31>Me=self()。%外壳的Pid改变了
**异常错误:右侧值不匹配
32>持久_项:获取(全局)。%他们仍然在那里
价值1
33>获取(本地)。%哎呀!流程字典也仍然存在。警告,这是shell实现的副作用
价值2
34>ets:查找(我的ets,共享)。%我的东西已经不存在了
**异常错误:参数错误
在函数ets中:查找/2
称为ets:查找(我的ets,共享)
35> 
12> persistent_term:put(global,value1).
ok
13> put(local,value2). % with process_dictionary, put returns the previous value associated to the key
undefined
14> ets:new(my_ets,[named_table,public]). % create a public table
my_ets
15> ets:insert(my_ets,{shared,value3,other_values}).
true
16> persistent_term:get(global).  % check that everything is stored                  
value1
17> get(local).                 
value2
18> ets:lookup(my_ets,shared).
[{shared,value3,other_values}]
19> ets:lookup_element(my_ets,shared,2). % a small example of ETS extended capabilities
value3
20> F1 = fun(From) -> From ! persistent_term:get(global) end. % prepare the same functions to be executed from external process
#Fun<erl_eval.44.97283095>
21> 
21> F2 = fun(From) -> From ! get(local) end.                  
#Fun<erl_eval.44.97283095>
22> F3 = fun(From) -> From ! ets:lookup(my_ets,shared) end.
#Fun<erl_eval.44.97283095>
23> Me = self().
<0.19320.1>
24> spawn(fun() -> F1(Me) end).
<0.12906.2>
25> flush(). % persistent_term are global
Shell got value1
ok
26> spawn(fun() -> F2(Me) end).
<0.13701.2>
27> flush(). % prrocess_dictionary is local                  
Shell got undefined
ok
28> spawn(fun() -> F3(Me) end).
<0.13968.2>
29> flush().  % ETS can be shared                 
Shell got [{shared,value3,other_values}]
ok
30> 1/0. % create an exception so the shell dies and is restarted by its supervisor
** exception error: an error occurred when evaluating an arithmetic expression
     in operator  '/'/2
        called as 1 / 0
31> Me = self().  % the shell's Pid changed             
** exception error: no match of right hand side value <0.14499.2>
32> persistent_term:get(global).    % persistent are still there                          
value1
33> get(local).  % Oooops! the process dictionary is still there too. Warning, this is a side effect of the shell implementation                                             
value2
34> ets:lookup(my_ets,shared).  % my_ets does not exist anymore                      
** exception error: bad argument
     in function  ets:lookup/2
        called as ets:lookup(my_ets,shared)
35>