簡體   English   中英

分別終止simple_one_for_one個孩子

[英]terminating simple_one_for_one children individually

我有一棵監督樹,其中有gen_servers個臨時子,壽命很短。 每個孩子在清理和終止時都會收到一條消息。

受我從tinymq項目中讀取的代碼的啟發,有一個控制器進程可以保留這些子進程的Pid。

在這種情況下,他們使用max_age設置和一些我不太了解的代碼來使頻道過期。

以我為例,在進行一些清理后,我嘗試使用supervisor:terminate_child(Sup, Pid) ,如下所示:

子級本身在控制器上執行RPC:

fs_outbound_controller:deallocate_me(UUID, self());

控制器:

deallocate_me(UUID, Pid) ->
    gen_server:cast(?SERVER, {deallocate_me, UUID, Pid}).

handle_cast({deallocate_me, UUID, Pid}, #state{dict = Uuid2Pid} = State) ->
    NewDict = dict:erase(UUID, Uuid2Pid),
    supervisor:terminate_child(fs_outbound_extn_sup, Pid),
    error_logger:info_msg("Successfully deallocated ~p", [UUID]),
    {noreply, State#state{dict=NewDict}}.

我觀察到的問題是,錯誤記錄器報告有關gen_server終止返回值的崩潰?

** Reason for termination == 
** {bad_return_value,ok}

感謝您的幫助。

編輯

我做了其他事情,將對deallocate_me的調用從RPC移到了從子級到控制器的消息。 認為孩子可能對控制器執行RPC調用,而后者又終止了該孩子,這導致了一些返回問題。 處理程序保持不變

handle_info({deallocate_me,UUID,Pid},#state {dict = Uuid2Pid} = State)-> NewDict = dict:erase(UUID,Uuid2Pid),超級用戶:terminate_child(fs_outbound_extn_sup,Pid),error_logger:info_msg(“已成功釋放〜 p”,[UUID]),{noreply,State#state {dict = NewDict}}。

但是現在我仍然得到:

** Reason for termination == 
** {bad_return_value,{deallocate_me,"49d9f7cb-62d3-4c3f-abf1-a19848967a9a",
                                    <0.50.0>}}

在我看來

fs_outbound_controller:deallocate_me(UUID, self());

被稱為handle_call/3handle_cast/2handle_info/2的最后一條語句。 正如您可能知道的那樣{stop, ...}這些代碼期望{reply, ...}{noreply, ...}{stop, ...}類的東西。

記住gen_server:cast/2總是返回okServerPid ! Message ServerPid ! Message總是返回Message本身。 這就是為什么第一次告訴您{bad_return_value, ok} ,第二次告訴您{bad_return_value, MessageSent}

在您的情況下,我會堅持使用gen_server:cast方法,並在錯誤的調用之后直接輸入正確的返回值:

fs_outbound_controller:deallocate_me(UUID, self()),
{noreply, State}; %% strongly depends on what your logic there is

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM