我對Logan/Merritt/Carlson的簡單緩存,第6章,第149-169頁,Erlang和OTP in Action做了些許修改。到目前爲止,沒有代碼改變,只是重命名模塊。Erlang:主管:start_child/2錯誤讓我感到困惑
我啓動應用程序:
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:編輯是一個gen_server(sc_element洛根等/人。)下面是創建/ 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}}.
如果我執行主管: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}]}]}}
51號線在gridz_edit是一個初始化函數:
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}
所以現在我感到困惑。我錯過了什麼?爲什麼主管:start_child/2拋出一個錯誤?
感謝,
LRP
啊,我的眼睛老化,印刷精美。你盯住它。我在gridz_edit中有一個元組:start_link/2,當我有一個列表。 –
謝謝。謝謝。 –
沒問題,樂於幫忙。 – kjw0188