gpt4 book ai didi

elixir - “handle_call”超时

转载 作者:行者123 更新时间:2023-12-04 07:53:28 30 4
gpt4 key购买 nike

我正在从 GenServer 中的句柄信息功能调用 elixir genserver 以添加电话号码获取表单客户端。但是一旦调用了handle_call,所有者进程就会崩溃[超时]。请帮忙。

全局创建一个 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

最佳答案

你不能在通话中给自己打电话,就像在你的 handle_info 调用 add它在您的 gen_server 上执行回调。由于所有操作都在 gen_server 中按顺序发生,因此您最终会阻止自己。解决方案应该是使用简单的私有(private) add模块上的功能并具有handle_infohandle_call({:add委托(delegate)给它。

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

关于elixir - “handle_call”超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25194994/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com