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

Max Bourinov bourinov@REDACTED
Thu Nov 10 08:19:27 CET 2011


Hi Ulf,

Thank you very much for your reply!

In my case I am getting messages from decoupled system over XMPP, so there
is nothing really dies - I need timeouts to detect the state of user's
process when it can be safely saved to the persisten storage - really
domain specific task... So, I really need timeouts.

About your code:
I am quite new to erlang, but for me it seems that you also sends tock
messages on a constant base?
Please correct me if I am wrong.

Max



On Thu, Nov 10, 2011 at 9:59 AM, Ulf Wiger
<ulf.wiger@REDACTED>wrote:

>
> 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/97a0e887/attachment.htm>


More information about the erlang-questions mailing list