无法在接收块中使用Erlang / ets [英] Unable to use Erlang/ets in receive block

查看:115
本文介绍了无法在接收块中使用Erlang / ets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Erlang / ets通过模式匹配接收到的数据来存储/更新各种信息。这是代码

I am trying to use Erlang/ets to store/update various informations by pattern matching received data. Here is the code

start() -> 
    S = ets:new(test,[]),
    register(proc,spawn(fun() -> receive_data(S) end)).

receive_data(S) ->
    receive
        {see,A} -> ets:insert(S,{cycle,A}) ;
        [[f,c],Fcd,Fca,_,_] -> ets:insert(S,{flag_c,Fcd,Fca});
        [[b],Bd,Ba,_,_] -> ets:insert(S,{ball,Bd,Ba})



    end,
    receive_data(S).

这里A是循环数,[f,c]是中心标志,[b]是球和Fcd,Fca,Bd,Ba是来自玩家的旗帜和球的方向和角度。

Here A is cycle number, [f,c] is center flag , [b] is ball and Fcd,Fca, Bd, Ba are directions and angle of flag and ball from player.

发件人进程正在发送这些信息。这里,模式匹配正常工作,我通过打印A,Fcd,Fca..etc的值检查。我相信使用Erlang / ets有什么问题。

Sender process is sending these informations. Here, pattern matching is working correctly which I checked by printing values of A, Fcd,Fca..etc. I believe there is something wrong with the use of Erlang/ets.

当我运行这个代码时,我会收到这样的错误

When I run this code I get error like this

Error in process <0.48.0> with exit value: {badarg,[{ets,insert,[16400,{cycle,7}]},{single,receive_data,1}]

有人可以告诉我这段代码有什么问题以及如何解决这个问题?

推荐答案

问题是ets-table的所有者是运行 start / 1 函数的进程,而ets的默认行为是只允许所有者写入和其他进程阅读,又名保护。两个解决方案:

The problem is that the owner of the ets-table is the process running the start/1 function and the default behavior for ets is to only allow the owner to write and other processes to read, aka protected. Two solutions:


  1. 创建ets表为public

  1. Create the ets table as public

S = ets:new(test,[public]). 


  • 将所有者设置为新创建的流程

  • Set the owner to your newly created process

    Pid = spawn(fun() -> receive_data(S) end, 
    ets:give_away(test, Pid, gift)
    register(proc,Pid)
    


  • give_away / 3

    这篇关于无法在接收块中使用Erlang / ets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆