[erlang-questions] Print a message when a process dies
Harit Himanshu
harit.subscriptions@REDACTED
Tue Jan 13 17:25:23 CET 2015
Thanks Michael for pointing to spawn_monitor/3, that is what I was looking
for
+ Harit
On Mon, Jan 12, 2015 at 10:27 PM, Michael Loftis <mloftis@REDACTED> wrote:
> You could also do a spawn_monitor (or possibly spawn_link depending on
> how you want to handle things) to avoid racing a potentially crashing
> process.
>
> On Mon, Jan 12, 2015 at 7:52 PM, Harit Himanshu
> <harit.subscriptions@REDACTED> wrote:
> > Thank you very much Felix. really appreciate your time and effort
> >
> > On Mon, Jan 12, 2015 at 3:50 PM, Felix Gallo <felixgallo@REDACTED>
> wrote:
> >>
> >> a) spawn() never fails. You'll get a Pid back, or the BEAM will crash
> in
> >> which case mere erlang programs are the least of your problems. The Pid
> >> will then either apply M:F to A, if M:F exists and is of arity
> befitting A,
> >> or apply error_handler:undefined_function to [M,F,A]), which in turns
> prints
> >> out that message you saw unless you've redefined the error handler,
> which
> >> you should definitely not do.
> >>
> >> b) monitor() also will never fail. If monitor() is called on a
> >> non-existent Pid, you will get a DOWN message immediately. monitor()
> will
> >> still return a Ref. (note: this applies to erlang released within the
> last
> >> few years; before, you would get a badarg exception).
> >>
> >> os:timestamp() will also never fail.
> >>
> >> In general your approach should be to assume that everything will work
> out
> >> fine and only program, at first, the 'happy path' for your code. Then
> fix
> >> them up as you find valid inputs which cause them to crash.
> >>
> >> http://erldocs.com/ and http://www.erlang.org/doc/ are really good, and
> >> can help you answer a lot of these questions; the functions you list are
> >> well documented, for example. If you're using a Mac, I can recommend
> >> getting 'Dash' (a documentation-at-your-hotkey app with a search bar) as
> >> well.
> >>
> >> F.
> >>
> >> On Mon, Jan 12, 2015 at 3:14 PM, Harit Himanshu
> >> <harit.subscriptions@REDACTED> wrote:
> >>>
> >>> I need to make my own function my_spawn(M,F,A) which work similar to
> >>> spawn(M,F,A) but if a spawned process dies, I need to print a message
> >>> telling how long a process lived.
> >>>
> >>>
> >>> My attempt looks like
> >>>
> >>> -export([my_spawn/3]).
> >>>
> >>> my_spawn(Mod, Func, Args) ->
> >>> Pid = spawn(Mod, Func, Args),
> >>> Ref = monitor(process, Pid),
> >>> Start = os:timestamp(),
> >>> receive
> >>> {'DOWN', Ref, process, Pid, _Why} ->
> >>> io:format("process died after ~p microseconds.~n",
> >>> [timer:now_diff(os:timestamp(), Start)])
> >>> end,
> >>> Pid.
> >>>
> >>>
> >>> so when I run this, I see
> >>>
> >>> 1> Pid = error_handling:my_spawn(area_server1, area, []).
> >>> process died after 363 microseconds.
> >>>
> >>> =ERROR REPORT==== 12-Jan-2015::15:06:53 ===
> >>> Error in process <0.33.0> with exit value:
> >>> {undef,[{area_server1,area,[],[]}]}
> >>>
> >>> <0.33.0>
> >>> 2>
> >>>
> >>> Problems?
> >>> - I am learning both Erlang and concurrency for the first time, so I
> see
> >>> following potential problems with this approach
> >>>
> >>> a.) What if spawned process failed in the first place, so I get no Pid
> >>> b.) What if command fails executing either "Ref = .." or "Start = .."?
> >>>
> >>> In such scenarios the program will not work effectively, what are the
> >>> recommended ways to solve such thing?
> >>>
> >>> Also, do you see any potential race conditions here? I take a guess
> >>> saying no because even if multiple process trying to execute the same
> code,
> >>> they will return different Pid anyway, is that understanding correct?
> >>>
> >>> Thanks a lot
> >>> + Harit
> >>>
> >>> _______________________________________________
> >>> erlang-questions mailing list
> >>> erlang-questions@REDACTED
> >>> http://erlang.org/mailman/listinfo/erlang-questions
> >>>
> >>
> >
> >
> > _______________________________________________
> > erlang-questions mailing list
> > erlang-questions@REDACTED
> > http://erlang.org/mailman/listinfo/erlang-questions
> >
>
>
>
> --
>
> "Genius might be described as a supreme capacity for getting its possessors
> into trouble of all kinds."
> -- Samuel Butler
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/f07f8739/attachment.htm>
More information about the erlang-questions
mailing list