[erlang-questions] How to put together gproc, hundreds of thousand of processes, timeouts?

Ulf Wiger <>
Thu Nov 10 07:59:22 CET 2011


On 10 Nov 2011, at 07:36, Max Bourinov wrote:

> Approach 2:
> In each user process instead of using timer I do the following:
> 
>     Self = self(),
>     Fun = fun(ThisFun) -> Self ! {tik}, receive after 1000 -> nil end, ThisFun(ThisFun) end,
>     spawn(fun() -> Fun(Fun) end),
> 
> Basically this is the same but here I have as twice as much of processes and it runs without ETS. (of course gproc uses ETS for its needs).

This should work pretty well.

If memory becomes an issue, you can save some by using hibernate/3:

Self = self(),
spawn(fun() -> set_interval_timer(timer:minutes(1), Self) end).

set_interval_timer(Interval, Pid) ->
    MRef = erlang:monitor(process, Pid),
    interval_wait(Interval, Pid).

interval_wait(Interval, Pid) ->
    erlang:send_after(Interval, self(), timeout),
    erlang:hibernate(?MODULE, interval_msg, [Interval, Pid]).

interval_msg(Interval, Pid) ->
    receive
        {'DOWN', _, process, Pid, _} -> exit(normal);
        timeout ->
            Pid ! {self(), tick},
            interval_wait(Interval, Pid)
    end.

It's good form to have the interval timer process detect the death of its parent, rather than keep on sending useless tick messages forever.

BR,
Ulf W

Ulf Wiger, CTO, Erlang Solutions, Ltd.
http://erlang-solutions.com



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20111110/5683b90d/attachment.html>


More information about the erlang-questions mailing list