【问题标题】:Erlang/ets: reset ets table after getting a "bad argument"?Erlang/ets:得到“错误参数”后重置 ets 表?
【发布时间】:2010-12-30 05:10:57
【问题描述】:

我一直在学习如何使用 ets,但困扰我的一件事是,偶尔*,ets:match 会抛出一个 bad argument……而且,从他们开始,所有后续调用(甚至是以前有效的调用) ) 也抛出一个bad argument:

> ets:match(Tid, { [$r | '$1'] }, 1)。 % 这场比赛有效... % 然后,在某些时候,这出现了: ** 异常错误:参数错误 在函数 ets:match/3 称为 ets:match(24589,{[114|'$1']},1) % 从那时起,匹配停止工作: > ets:match(Tid, { [$r | '$1'] }, 1)。 ** 异常错误:参数错误 在函数 ets:match/3 称为 ets:match(24589,{[114|'$1']},1)

有什么方法可以“重置”ets 系统,以便我可以再次查询它(即从 shell)?

*:我无法重现这个问题……但是当我尝试做“其他事情”时,它经常发生。

【问题讨论】:

    标签: erlang ets


    【解决方案1】:

    虽然我不是 100% 确定,this thread 似乎回答了你的问题。您似乎在 shell 中观察到这种行为。如果是这样,两个事实以一种令人困惑的方式相互作用:

    1. ets 表在其拥有的进程死亡后立即被删除。
    2. 当 erlang shell 收到异常并静默重新启动时,它就会死掉。

    所以,当你遇到第一个异常时,当前的 shell 进程会死掉,导致 ets 表被删除,然后为你启动一个新的 shell 进程。现在,当您尝试另一个 ets:match 时,它会失败,因为该表不再存在。

    【讨论】:

      【解决方案2】:

      戴尔已经告诉过你会发生什么。您可以不时在 shell 中调用 self() 来确认这一点。

      作为一种快速解决方法,您可以生成另一个进程来为您创建一个公共表。那么那张桌子就不会和你的壳一起死了。

      1> self().
      <0.32.0>    % shell's Pid
      
      2> spawn(fun() -> ets:new(my_table, [named_table, public]), receive X -> ok end end).
      <0.35.0>    % the spawned process's Pid
      
      3> ets:insert(my_table, {a, b}).
      true
      

      现在创建一个异常并检查该表是否确实存在。

      4> 1/0.
      ** exception error: bad argument in an arithmetic expression
           in operator  '/'/2
              called as 1 / 0
      5> self().
      <0.38.0>   % shell's reborn, with a different Pid
      
      6> ets:insert(my_table, {c, d}).
      true
      7> ets:tab2list(my_table).
      [{c,d},{a,b}]    % table did survive the shell restart
      

      要删除表,只需向您生成的进程发送一些内容:

      8> pid(0,35,0) ! bye_bye.
      bye_bye
      9> ets:info(my_table).   
      undefined
      

      【讨论】:

        猜你喜欢
        • 2016-10-23
        • 2016-09-16
        • 2016-06-03
        • 2012-10-14
        • 2016-10-27
        • 2016-09-10
        • 2021-01-19
        • 2015-10-27
        • 2010-12-19
        相关资源
        最近更新 更多