我正在尝试创建一个可以访问 ETS 模块的简单 Erlang 进程。
我的源代码包括:
进程创建:
start_message_channel() -> Table = ets:new(messages, [ordered_set, named_table]), Channel = spawn(?MODULE, channel, []), {Channel, {table, Table}}.
流程逻辑:
channel() -> receive {Sender, {send_message, {Message, Table}}} -> ets:insert(Table, {message, Message}), Sender ! {self(), {status, success}}; {Sender, {receive_message, Table}} -> {message, Message} = ets:first(Table), Sender ! {self(), {status, {success, Message}}}; _ -> throw(incorrect_protocol_exception) end.
与进程的通信
send_message_to_message_channel({Channel, {table, Table}}, Message) -> Channel ! {self(), {send_message, {Message, Table}}}, receive {Channel, {status, success}} -> io:format("Message sent!~n"); {Channel, {status, failure}} -> io:format("Message failed to send!~n"); _ -> throw(incorrect_protocol_exception) end. receive_message_from_message_channel({Channel, {table, Table}}) -> Channel ! {self(), {receive_message, Table}}, receive {Channel, {status, {success, Message}}} -> io:format(Message); {Channel, {status, failure}} -> io:format("Message failed to receive!~n"); _ -> throw(incorrect_protocol_exception) end.
在 Erlang 终端中执行函数调用时,出现错误:
1> cd("C:/Users/dauma").
C:/Users/dauma
ok
2> c(message_channel).
{ok,message_channel}
3> Object = message_channel:start_message_channel().
{<0.59.0>,{table,messages}}
4> message_channel:send_message_to_message_channel(Object, "Hello World!").
=ERROR REPORT==== 19-May-2016::11:09:27 ===
Error in process <0.59.0> with exit value:
{badarg,[{ets,insert,[messages,"Hello World!"],[]},
{message_channel,channel,0,
[{file,"message_channel.erl"},{line,35}]}]}
谁能告诉我,问题可能出在哪里?