我在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的bug?
谢谢。
发布于 2012-10-27 23:35:34
shell进程已经创建了ETS表,并且是该表的所有者。当所有者进程终止时,ETS表将自动删除。
因此,当您在6获得异常时,shell进程将终止,因此ETS表将被删除。
将其设为private还意味着没有其他进程可以访问它(因此,即使表是持久化的,新的shell也不能访问它),但在这种情况下,情况会更糟,因为表已经被删除。
发布于 2012-10-28 05:45:32
(太大了,不能作为对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)https://stackoverflow.com/questions/13101453
复制相似问题