我从GenServer中的句柄信息功能调用elixir genserver来添加电话号码获取表单客户端。但是一旦调用handle_call,所有者进程就会崩溃[timeout]。请帮忙。
全局创建一个ETS,以便在调用任何下面描述的函数之前插入值。
def handle_info(message, state) do
{a,b} = message
phonenumber = b[:body]
add phonenumber
{:noreply, state}
end
def add(phonenumber) do
GenServer.call(__MODULE__, {:add, phonenumber})
end
def handle_call({:add, phonenumber}, from, state) do
:ets.insert(:access_table, {:details, phonenumber})
reply = {:ok, "Added #{phonenumber} to profile"}
new_state = [{username} | state]
{:reply, reply , new_state}
end
错误:
** When Server state == []
** Reason for termination ==
** {timeout,{gen_server,call,['Elixir.Bankrecord',{add,"346534543534"},5000]}}
** (EXIT from #PID<0.150.0>) exited in: :gen_server.call(Bankrecord, {:add, '346534543534'}, 5000)
** (EXIT) time out
答案 0 :(得分:7)
您无法在通话中给自己打电话,就像在handle_info中调用add
来执行对gen_server的回调一样。由于所有操作都在gen_server中按顺序发生,因此您最终会阻止自己。解决方案应该是在模块上使用简单的私有add
函数,并让handle_info
和handle_call({:add
委托给它。
def add(phonenumber) do
GenServer.call(__MODULE__, {:add, phonenumber})
end
def handle_info({_, message}, state) do
add_number message[:body]
{:noreply, state}
end
def handle_call({:add, phonenumber}, from, state) do
add_number phonenumber
{:reply, {:ok, "Added #{phonenumber} to profile"} , [{username} | state]}
end
defp add_number(phonenumber) do
:ets.insert(:access_table, {:details, phonenumber})
end