[erlang-questions] Kill a process after T seconds

Jesper Louis Andersen jesper.louis.andersen@REDACTED
Tue Jan 13 20:02:08 CET 2015


Hi Harit,

I don't think there is any way you could do that better, without knowing
what semantics you are striving for in the first place. The code looks fine
to me, and depending in which context it runs, it could be perfect or have
a problem. It is really hard to tell without any more information.

For a larger system, you may gain some by using either the 'lager'
application (github.com/basho/lager) or the 'error_logger' to report
progress. That way, you can later route logging messages in a favorable
fashion.

Another thing you could look into is to use 'proc_lib' to turn the
framework you have built into an OTP-compliant framework. But that may be a
bit out on the horizon, depending on how much time you have spent with
Erlang to this date.

On Tue Jan 13 2015 at 7:56:03 PM Harit Himanshu <
harit.subscriptions@REDACTED> wrote:

> The recent problem I worked on is
>
> Write a function my_spawn(Mod, Func, Args, Time) that behaves like
> spawn(Mod, Func, Args) but with one difference. If the spawned process
> lives for more than Time seconds, it should be killed.
>
> My attempt looks like
>
> my_spawn(Mod, Func, Args, Time) ->
>   Pid = spawn(Mod, Func, Args),
>   io:format("created new process: ~p.~n", [Pid]),
>   timer:kill_after(Time * 1000),
>   Pid.
>
> and when I run I see
>
> 1>  c(error_handling).
>
> {ok,error_handling}
>
> 2> Pid1 = error_handling:my_spawn(area_server0, loop, [], 30).
>
> created new process: <0.39.0>.
>
> <0.39.0>
>
> 3> Pid1 ! {self(), {square, 10}}.
>
> {<0.32.0>,{square,10}}
>
> 4> receive X->X end.
>
> 100
>
> 5> Pid1 ! {self(), {square, 20}}.
>
> {<0.32.0>,{square,20}}
>
> 6> receive Y->Y end.
>
> 400
>
> ** exception error: killed
>
> 7>
>
> Looking for suggestions on the code or ways to write it better
>
> Thanks
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150113/c0381042/attachment.htm>


More information about the erlang-questions mailing list