我正在对Logan / Merritt / Carlson的简单缓存,第6章,第149-169页,Erlang和OTP in Action进行略微修改。到目前为止,没有代码更改,只需重命名模块。
我开始申请:
application:start(gridz).
ok
我插入一个项目:
gridz_maker:insert(blip, blop).
我收到此错误:
** exception error: no match of right hand side value
{error,
{function_clause,
[{gridz_edit,init,
[{blop,86400}],
[{file,"src/gridz_edit.erl"},{line,51}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}
in function gridz_maker:insert/2 (src/gridz_maker.erl, line 15)
以下是代码:
insert(Key, Value) ->
case gridz_store:lookup(Key) of
{ok, Pid} -> gridz_edit:replace(Pid, Value);
{error, _} -> {ok, Pid} = gridz_edit:create(Value), %% line 15
gridz_store:insert(Key, Pid)
end.
我看第15行:
{error, _} -> {ok, Pid} = gridz_edit:create(Value),
我期待错误,因为这是一个新项目。 gridz:edit是gen_server(Logan等人的sc_element)这是create / 1的代码:
create(Value) ->
create(Value, ?DEFAULT_LEASE_TIME).
create(Value, LeaseTime) ->
gridz_sup:start_child(Value, LeaseTime).
这是gridz_sup的代码:start_child / 2:
start_child(Value, LeaseTime) ->
supervisor:start_child(?SERVER, [Value, LeaseTime]).
init([]) ->
Grid = {gridz_edit, {gridz_edit, start_link, []},
temporary, brutal_kill, worker, [gridz_edit]},
Children = [Grid],
RestartStrategy = {simple_one_for_one, 0, 1},
{ok, {RestartStrategy, Children}}.
如果我直接执行supervisor:start_child / 2,这就是我得到的:
{error,{function_clause,[{gridz_edit,init,
[{blop,50400}],
[{file,"src/gridz_edit.erl"},{line,51}]},
{gen_server,init_it,6,
[{file,"gen_server.erl"},{line,304}]},
{proc_lib,init_p_do_apply,3,
[{file,"proc_lib.erl"},{line,227}]}]}}
gridz_edit中的第51行是init函数:
init([Value, LeaseTime]) ->
Now = calendar:local_time(),
StartTime = calendar:datetime_to_gregorian_seconds(Now),
{ok,
#state{value = Value,
lease_time = LeaseTime,
start_time = StartTime},
time_left(StartTime, LeaseTime)}.
如果我直接执行它,它可以工作:
120> gridz_edit:init([blop, (60 * 60 * 24)]).
{ok,{state,blop,86400,63537666408},86400000}
所以现在我感到困惑。我错过了什么?为什么supervisor:start_child / 2会抛出错误?
谢谢,
LRP
答案 0 :(得分:1)
错误说你正在传递一个包含2个成员的元组{blop,86400}
,当你似乎期待一个包含2个成员的列表时:[Value, LeaseTime]
。在直接执行中,您也使用列表,因此它可以正常工作。你应该找出创建元组的位置,然后创建一个列表。