[erlang-questions] How to wake up hibernated process without sending message from external process

Ulf Wiger <>
Mon Oct 25 14:54:08 CEST 2010


I imagine that erlang:start_timer/3 ought to work well.
It is pretty scalable, in that the runtime system can handle many thousands -
most likely much more than that - of timers without noticeable degradation.

There are two different timer BIFs: start_timer/3 and send_after/3.
The main difference is that start_timer/3 includes the Ref returned by the 
function in the message itself, e.g.

1> TRef = erlang:start_timer(1000, self(), "hi").
#Ref<0.0.0.344>
2> receive {timeout,TRef,Msg} -> io:fwrite("Timer says ~p.~n", [Msg]) end.
Timer says "hi".
ok

...whereas send_after/3 simply sends whatever message you specify:

3> erlang:send_after(1000,self(),"ho").
#Ref<0.0.0.352>
4> flush().
Shell got "ho"
ok

Personally, I always use erlang:start_timer/3.

BR,
Ulf W

On 25 Oct 2010, at 14:43, Zvi wrote:

> Hi,
> 
> I have many hibernated erlang processes, which need to be periodically
> waken up.
> One way to do this, is to send wakeup messages from external process.
> Is there a scalable way for the hibernated process to send messages to
> itself via some kind of periodic timer?
> 
> Also note, that "after" doesn't work for hibernated processes. In
> order to "after" clause to work, the process need to be
> "unhibernated".
> 
> Thanks,
> Zvi
> 
> ================================
> -module(mymodule).
> -export([start/0, loop/1]).
> 
> start() ->
>    proc_lib:hibernate(?MODULE, loop, [0]);
> 
> loop(State) ->
>    receive
>        {do_something, Msg} ->
>              State2 = do_something(State, Msg),
>              proc_lib:hibernate(?MODULE, loop, [State2]);
>        wakeup ->
>              loop(State)
>    after Timeout ->
>              do_cleanup()
>    end.
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 

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





More information about the erlang-questions mailing list