[erlang-questions] perform a function on a fixed time interval

Mazen Harake <>
Tue Aug 24 11:54:24 CEST 2010


  Be cautious,

Using send messages in an interval is dangerous if compute() can take 
more then the interval. It can build up message queues on a congested 
system; especially if what compute() does has side effects.

Just a word of advise.

On 24/08/2010 13:40, Roberto Ostinelli wrote:
> 2010/8/24 Hynek Vychodil<>:
>> use timer:send_interval/2,3
> sure, have this version too:
>
> ----------------------------------------------
>
> -module(test).
> -compile(export_all).
>
>
> start() ->
> 	% start main loop
> 	Pid = spawn(?MODULE, main_loop, []),
> 	% start timer
> 	timer:send_interval(1000, Pid, compute),
> 	% start sender loop
> 	send_loop(Pid, 20),
> 	Pid ! shutdown.
>
> % send 20 messages every 500 ms
> send_loop(_Pid, 0) ->  ok;
> send_loop(Pid, Count) ->
> 	receive
> 	after 500 ->
> 		Pid ! test,
> 		send_loop(Pid, Count - 1)
> 	end.
>
> main_loop() ->
> 	receive
> 		shutdown ->
> 			shutdown;
> 		test ->
> 			io:format("received test message~n"),
> 			main_loop();
> 		compute ->
> 			compute(),
> 			main_loop()
> 	end.
> 	
> compute() ->
> 	io:format("ping~n").
>
> ----------------------------------------------
>
> doesn't change much.
>
> fact is: is this really the only way?
>
> cheers,
>
> r.
>
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>



More information about the erlang-questions mailing list