繁体   English   中英

erlang ets选择奇怪的行为

[英]erlang ets select strange behaviour

我在erlang中有一个奇怪的行为:ets:select。

我实现了一个正确的select语句(下面的4和5),然后我在我的语句中出错(下面的6),然后我再次尝试与4和5中相同的语句,并且它不再起作用。

怎么了 ? 任何想法 ?

Erlang R14B01 (erts-5.8.2) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

Eshell V5.8.2  (abort with ^G)
1> Tab = ets:new(x, [private]).
16400
2> ets:insert(Tab, {c, "rhino"}).
true
3> ets:insert(Tab, {a, "lion"}). 
true
4> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]).      
["rhino","lion"]    
5> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]).      
["rhino","lion"]
6> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2', '$3']}]).
** exception error: bad argument
 in function  ets:select/2
    called as ets:select(16400,[{{'$1','$2'},[],['$1','$2','$3']}])
7> ets:select(Tab,[{{'$1','$2'},[],['$1', '$2']}]).      
** exception error: bad argument
 in function  ets:select/2
    called as ets:select(16400,[{{'$1','$2'},[],['$1','$2']}])

我的ets表被破坏了吗? 它会成为ets的错误吗?

谢谢。

shell进程创建了ETS表,并且是它的所有者 当所有者进程终止时,将自动删除ETS表。

因此,当您在6处获得异常时,shell进程将终止,因此将删除ETS表。

将其设为private也意味着没有其他进程可以访问它(因此即使表持久化,新shell也无法访问它),但在这种情况下,由于表已被删除,情况更糟。

(太大了,不能作为对thanosQR正确答案的评论)

如果您希望表在shell中存在异常,则可以将其提供给另一个进程。 例如:

1> Pid = spawn(fun () -> receive foo -> ok end end).    % sit and wait for 'foo' message
<0.62.0>
2> Tab = ets:new(x, [public]).                          % Tab must be public if you plan to give it away and still have access
24593
3> ets:give_away(Tab, Pid, []).
true
4> ets:insert(Tab, {a,1}).
true
5> ets:tab2list(Tab).
[{a,1}]
6> 3=4.
** exception error: no match of right hand side value 4
7> ets:tab2list(Tab).                                   % Tab survives exception
[{a,1}]
8> Pid ! foo.                                           % cause owning process to exit
foo
9> ets:tab2list(Tab).                                   % Tab is now gone
** exception error: bad argument
     in function  ets:match_object/2
        called as ets:match_object(24593,'_')
     in call from ets:tab2list/1 (ets.erl, line 323)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM