2011-01-29 15 views

उत्तर

13

मैंने कुछ शोध किया, और नीचे मेरे पास है।

पहले, इस एक पर्यवेक्षक का एक नमूना कॉलबैक मॉड्यूल है:

-module(root_sup). 
-behaviour(supervisor). 
-export([start_link/0]). 
-export([init/1]). 

start_link() -> 
    {ok, Pid} = supervisor:start_link({local, ?MODULE}, 
      ?MODULE, []), 
    {ok, Pid}. 

init(_Args) -> 
    RestartStrategy = {simple_one_for_one, 10, 60}, 
    ChildSpec = {ch1, {ch1, start_link, []}, 
      permanent, brutal_kill, worker, [ch1]}, 
    Children = [ChildSpec], 
    {ok, {RestartStrategy, Children}}. 

और यह एक बच्चे जो suprervision पेड़ गतिशील रूप में जोड़ दिया जाएगा की एक कॉलबैक मॉड्यूल है:

-module(ch1). 

-behaviour(gen_server). 

% Callback functions which should be exported 
-export([init/1]). 
-export([handle_cast/2]). 

% user-defined interface functions 
-export([start_link/0]). 

start_link() -> 
    gen_server:start_link(?MODULE, [], []). 

init(_Args) -> 
    io:format("ch1 has started (~w)~n", [self()]), 
    % If the initialization is successful, the function 
    % should return {ok,State}, {ok,State,Timeout} .. 
    {ok, ch1State}. 

handle_cast(calc, State) -> 
    io:format("result 2+2=4~n"), 
    {noreply, State}; 
handle_cast(calcbad, State) -> 
    io:format("result 1/0~n"), 
    1/0, 
    {noreply, State}. 

यह यह है कि हम आमतौर पर पर्यवेक्षक कैसे शुरू करते हैं:

1> ch_sup:start_link(). 
{ok,<0.33.0>} 

अब चलिए अपना पहला बच्चा पी शुरू करें rocess:

2> {ok, Child1Pid} = supervisor:start_child(ch_sup, []). 
ch1 has started (<0.35.0>) 
{ok,<0.35.0>} 

आप गतिशील रूप से बाल प्रक्रिया शुरू कर सकते हैं; चलो एक और बच्चे को प्रारंभ करते हैं:

3> {ok, Child2Pid} = supervisor:start_child(ch_sup, []). 
ch1 has started (<0.37.0>) 
{ok,<0.37.0>} 

आप देख सकते हैं कि हमारे प्रक्रियाओं शुरू किया (ध्यान दें पिछले दो):

4> erlang:processes(). 
[<0.0.0>,<0.2.0>,<0.4.0>,<0.5.0>,<0.7.0>,<0.8.0>,<0.9.0>, 
<0.10.0>,<0.11.0>,<0.12.0>,<0.13.0>,<0.14.0>,<0.15.0>, 
<0.16.0>,<0.17.0>,<0.18.0>,<0.19.0>,<0.20.0>,<0.21.0>, 
<0.22.0>,<0.23.0>,<0.24.0>,<0.25.0>,<0.26.0>,<0.27.0>, 
<0.31.0>,<0.33.0>,<0.35.0>,<0.37.0>] 

आइए अब हमारे पहले बच्चे प्रक्रिया कुछ करते हैं:

5> gen_server:cast(Child1Pid, calc). 
result 2+2=4 
ok 

अब तक, बहुत अच्छा है। अब हम हमारी पहली संतान कुछ बुरी कोड का मूल्यांकन करने बनाती हूँ: रिपोर्ट में

6> gen_server:cast(Child1Pid, calcbad). 
result 1/0 
ok  
7> 
=ERROR REPORT==== 10-Feb-2011::01:32:15 === 
** Generic server <0.35.0> terminating 
** Last message in was {'$gen_cast',calcbad} 
** When Server state == ch1State 
** Reason for termination == 
** {'function not exported', 
     [{ch1,terminate, 
      [{badarith, 
       [{ch1,handle_cast,2}, 
        {gen_server,handle_msg,5}, 
        {proc_lib,init_p_do_apply,3}]}, 
      ch1State]}, 
     {gen_server,terminate,6}, 
     {proc_lib,init_p_do_apply,3}]} 
ch1 has started (<0.42.0>) 
7> 

, जैसा कि आप देख सकते हैं कि शून्य से भाग एक अपवाद के कारण होता है और प्रक्रिया को समाप्त कर दिया गया था। लेकिन पर्यवेक्षक इसका ख्याल रखता है और तुरंत एक और बच्चे की प्रक्रिया शुरू करता है (अंतिम पंक्ति को नोट करें)।

हमें यकीन है कि अन्य बच्चे प्रक्रिया हम पहले शुरू कर दिया अभी भी जीवित है बनाने के लिए जाँच कर सकते हैं (ध्यान दें <0.37.0>):

8> gen_server:cast(Child2Pid, calc). 
result 2+2=4 
9> 

:

7> erlang:processes().     
[<0.0.0>,<0.2.0>,<0.4.0>,<0.5.0>,<0.7.0>,<0.8.0>,<0.9.0>, 
<0.10.0>,<0.11.0>,<0.12.0>,<0.13.0>,<0.14.0>,<0.15.0>, 
<0.16.0>,<0.17.0>,<0.18.0>,<0.19.0>,<0.20.0>,<0.21.0>, 
<0.22.0>,<0.23.0>,<0.24.0>,<0.25.0>,<0.26.0>,<0.27.0>, 
<0.31.0>,<0.33.0>,<0.37.0>,<0.42.0>] 
8> 

हम भी यह हमारे लिए कुछ करना कर सकते हैं

निम्नलिखित Erlang मैनुअल पृष्ठ हैं आप पढ़ना चाहते हैं जाएगा:

+0

आपको बहुत बहुत धन्यवाद। और my_start_child/0 wher? – 0xAX

+0

@shk: ओह, यह 2:22 पूर्वाह्न है, बस इस निर्यात को पुनर्स्थापित करना भूल गया :-) इस फ़ंक्शन में 'पर्यवेक्षक: start_child (ch_sup, []) शामिल है।' मैं पोस्ट संपादित करूंगा। उल्लेख करने के लिए एक महत्वपूर्ण बात: आपके बच्चे को हमेशा पर्यवेक्षक से लिंक करना चाहिए, यही कारण है कि मैंने 'gen_server: start_link' का उपयोग बाल प्रारंभ समारोह में किया था, न केवल' gen_server: start'; अन्यथा पर्यवेक्षक को यह जानने का कोई तरीका नहीं होगा कि उसके बच्चों की प्रक्रियाएं अभी भी चल रही हैं या समाप्त हो चुकी हैं (आमतौर पर या असामान्य रूप से)। –

+1

"ch_sup" कहां है? मुझे लगता है कि यह "root_sup" होना चाहिए – why

7

Supervisor BehaviourOTP Design Principles में से Erlang दस्तावेज़ों का हिस्सा simple_one_for_one और गतिशील बच्चों का उपयोग करने का एक उदाहरण है। मैं पूरे डिजाइन सिद्धांतों के भाग की अनुशंसा करता हूं क्योंकि यह ओटीपी कैसे काम करता है में बहुत अंतर्दृष्टि प्रदान करता है।

संबंधित मुद्दे