<div dir="ltr"><div>Sort of; you don't need a monitor because they're linked (so any process deaths will cause a death of the wrapper process, the eredis_sub process, and the eredis_smart_sub process). Make those calls from within your own process (maintaining the pid(s) you need in the process state), and supervise ~that~. If either your process, or either of the two that are spawned in the example code snippet on that page, die, the link will cause the other two to also die, your wrapper process to restart, and those two to reinitialize. Make all your calls through your wrapper process. That is, something like - <br><br>*my_sup.erl </div><div><br></div><div>init(_) -><br> MyProcSpec = ...,<br> {ok, { {one_for_one, 10, 10}, [MyProcSpec]} . </div><div><br></div><div><br></div><div><br>*my_proc.erl (I'm assuming this is a gen_server)<br><br>init(_) -><br> {ok, EredisSubClient} = eredis_sub:start_link(),<div> {ok, SubClient} = eredis_smart_sub:start_link(EredisSubClient),<br> {ok, [{eredis_sub, EredisSubClient}, {eredis_smart_sub, SubClient}]}.<br><br>handle_cast(A, State) -></div><div> proplists:get_value(eredis_smart_sub, State) ! A,</div><div> ...<br> {noreply, State}.<br><br>handle_call(A, _, State) -> </div> proplists:get_value(eredis_smart_sub, State) ! A,</div><div> ...</div><div> {reply, ..., State}.<br><br><br>etc. </div><div><br></div><div>You can also register those pids in my_proc:init, and reference them externally using whereis(WellKnownName), as I mentioned before, but you'll likely want to unregister them as part of the terminate (and in either case, you need to ensure that this process is up first in your supervisor hierarchy, before anything that relies on it, likely using rest_for_one, as mentioned before, so you don't end up sending messages to a pid that corresponds to nothing). </div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Feb 26, 2015 at 6:29 PM, Chase James <span dir="ltr"><<a href="mailto:chase.james@cirrusmio.com" target="_blank">chase.james@cirrusmio.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">To bring the discussion closer to my problem domain, I'm specifically<br>
trying to bring the eredis_smart_sub app<br>
(<a href="https://github.com/nacmartin/eredis_smart_sub" target="_blank">https://github.com/nacmartin/eredis_smart_sub</a>) under supervision, but<br>
it requires the Pid of eredis to be passed in to its start_link<br>
function.<br>
<br>
Because of the way its designed, is it safe to say you can't put it<br>
under supervision directly without a wrapper module that uses the<br>
registered eredis Pid and creates like a monitor or something for the<br>
eredis_smart_sub gen_server?<br>
<br>
Thanks for your time and advice!<br>
<div><div class="h5"><br>
On Wed, Feb 25, 2015 at 11:32 PM, Christopher Phillips<br>
<<a href="mailto:lostcolony@gmail.com">lostcolony@gmail.com</a>> wrote:<br>
> There are a couple of ways to achieve this, though not directly the way<br>
> you describe. You would not pass in the Pid into the supervision spec,<br>
> because the Pid can change any time a process restarts. Instead, you need to<br>
> register the Pid to a name, and use the name.<br>
><br>
> As a quick aside, it sounds like the second process is dependent on the<br>
> first process being up. With that in mind, you'll likely want a rest_for_one<br>
> supervision strategy, with the first process being the first in the child<br>
> spec list.<br>
><br>
> To your question, most gen_server examples actually are set up to do this<br>
> for you, and you don't generally need the Pid, but if you're not using a<br>
> gen_server you can manually use erlang:register/2, and erlang:whereis/1 to<br>
> register the Pid to an atom, and to look up the Pid, as appropriate.<br>
><br>
><br>
> That is, I can do something like the following for my first process<br>
> (ellipsis are indicative of 'assume the proper arguments; not relevant to<br>
> the answer at hand') -<br>
><br>
> start_link() -><br>
> gen_server:start_link({local, NameToRegisterItAs}, ...).<br>
><br>
> or if not using a gen_server,<br>
><br>
> start_link() -><br>
> Pid = spawn_link(...),<br>
> true = register(NameToRegisterItAs, Pid),<br>
> Pid.<br>
><br>
><br>
><br>
> and then elsewhere (my second process) where I need that Pid, I can use<br>
><br>
> whereis(NameToRegisterItAs)<br>
><br>
> to get the Pid. As long as you set up your supervisor correctly, and<br>
> assuming a single node (else you may need to globally register it rather<br>
> than locally) the registered Pid should always be the one the process is<br>
> running under.<br>
><br>
><br>
><br>
>><br>
>> ------------------------------<br>
>><br>
>> Message: 7<br>
>> Date: Wed, 25 Feb 2015 14:03:44 -0500<br>
>> From: nx <<a href="mailto:nx@nu-ex.com">nx@nu-ex.com</a>><br>
>> To: erlang-questions <<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a>><br>
>> Subject: [erlang-questions] Supervising a Process that is Dependent on<br>
>> Another Process<br>
>> Message-ID:<br>
>><br>
>> <CAP8Yv7KxYS+qXA04k7qWjZMjZMpD9uaMsAOwL2W4yA+6=<a href="mailto:VZMyw@mail.gmail.com">VZMyw@mail.gmail.com</a>><br>
>> Content-Type: text/plain; charset=UTF-8<br>
>><br>
>><br>
>> Is it possible to supervise a process that requires the Pid of another<br>
>> process in its start_link function?<br>
>><br>
>> I'm not clear on how I can get the supervisor to 1) start a process 2)<br>
>> start another process with the Pid of the first process, then 3)<br>
>> restart both of these if the first process fails and automatically<br>
>> pass in the Pid of the restarted first process to the second process.<br>
>><br>
>> I may be expecting too much from the supervisor. Any suggestions?<br>
>><br>
>> Thanks!<br>
>><br>
>><br>
><br>
</div></div>> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
</blockquote></div><br></div>