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

Roberto Ostinelli roberto@REDACTED
Tue Aug 24 12:40:52 CEST 2010


2010/8/24 Hynek Vychodil <hynek@REDACTED>:
> 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.


More information about the erlang-questions mailing list