[erlang-questions] Supervising a Process that is Dependent on Another Process

Christopher Phillips <>
Thu Feb 26 05:32:41 CET 2015


  There are a couple of ways to achieve this, though not directly the way
you describe. You would not pass in the Pid into the supervision spec,
because the Pid can change any time a process restarts. Instead, you need
to register the Pid to a name, and use the name.

  As a quick aside, it sounds like the second process is dependent on the
first process being up. With that in mind, you'll likely want a
rest_for_one supervision strategy, with the first process being the first
in the child spec list.

  To your question, most gen_server examples actually are set up to do this
for you, and you don't generally need the Pid, but if you're not using a
gen_server you can manually use erlang:register/2, and erlang:whereis/1 to
register the Pid to an atom, and to look up the Pid, as appropriate.


  That is, I can do something like the following for my first process
(ellipsis are indicative of 'assume the proper arguments; not relevant to
the answer at hand') -

start_link() ->
  gen_server:start_link({local, NameToRegisterItAs}, ...).

or if not using a gen_server,

start_link() ->
  Pid = spawn_link(...),
  true = register(NameToRegisterItAs, Pid),
  Pid.



and then elsewhere (my second process) where I need that Pid, I can use

whereis(NameToRegisterItAs)

to get the Pid. As long as you set up your supervisor correctly, and
assuming a single node (else you may need to globally register it rather
than locally) the registered Pid should always be the one the process is
running under.




> ------------------------------
>
> Message: 7
> Date: Wed, 25 Feb 2015 14:03:44 -0500
> From: nx <>
> To: erlang-questions <>
> Subject: [erlang-questions] Supervising a Process that is Dependent on
>         Another Process
> Message-ID:
>         <CAP8Yv7KxYS+qXA04k7qWjZMjZMpD9uaMsAOwL2W4yA+6=
> >
> Content-Type: text/plain; charset=UTF-8
>
> Is it possible to supervise a process that requires the Pid of another
> process in its start_link function?
>
> I'm not clear on how I can get the supervisor to 1) start a process 2)
> start another process with the Pid of the first process, then 3)
> restart both of these if the first process fails and automatically
> pass in the Pid of the restarted first process to the second process.
>
> I may be expecting too much from the supervisor. Any suggestions?
>
> Thanks!
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150225/a40c62dc/attachment.html>


More information about the erlang-questions mailing list