19962023Ericsson AB. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
timerSebastian StrolloBjarne Däcker1Bjarne Däcker1998-09-09Dtimer.xmltimerTimer functions.
This module provides useful functions related to time. Unless otherwise
stated, time is always measured in milliseconds. All
timer functions return immediately, regardless of work done by another
process.
Successful evaluations of the timer functions give return values
containing a timer reference, denoted TRef. By using
cancel/1,
the returned reference can be used to cancel any
requested action. A TRef is an Erlang term, which contents
must not be changed.
The time-outs are not exact, but are at least as long
as requested.
Creating timers using
erlang:send_after/3 and
erlang:start_timer/3
is more efficient than using the timers provided by this module. However,
the timer module has been improved in OTP 25, making it more efficient and
less susceptible to being overloaded. See
the
Timer Module section in the Efficiency Guide.
Time in milliseconds.
A timer reference.
Spawn a process evaluating Module:Function(Arguments)
after a specified Time.
Evaluates spawn(Module, Function,
Arguments) after Time
milliseconds.
Returns {ok, TRef} or
{error, Reason}.
Spawn a process evaluating Module:Function(Arguments)
repeatedly at intervals of Time.
Evaluates spawn(Module, Function,
Arguments) repeatedly at intervals of
Time, irrespective of whether a previously
spawned process has finished or not.
If the execution time of the spawned process is, on average,
greater than the given Time, multiple such
processes will run at the same time. With long execution times,
short intervals, and many interval timers running, this may even
lead to exceeding the number of allowed processes. As an extreme
example, consider
[timer:apply_interval(1, timer, sleep, [1000]) || _ <- lists:seq(1, 1000)],
that is, 1,000 interval timers executing a process that takes 1s
to complete, started in intervals of 1ms, which would result in
1,000,000 processes running at the same time, far more than a node
started with default settings allows (see the
System
Limits section in the Effiency Guide).
Returns {ok, TRef} or
{error, Reason}.
Spawn a process evaluating Module:Function(Arguments)
repeatedly at intervals of Time.
Evaluates spawn(Module, Function,
Arguments) repeatedly at intervals of
Time, waiting for the spawned process to
finish before starting the next.
If the execution time of the spawned process is greater than the
given Time, the next process is spawned immediately
after the one currently running has finished. Assuming that execution
times of the spawned processes performing the applies on average
are smaller than Time, the amount of applies
made over a large amount of time will be the same even if some
individual execution times are larger than
Time. The system will try to catch up as soon
as possible. For example, if one apply takes
2.5*Time, the following two applies will be
made immediately one after the other in sequence.
Returns {ok, TRef} or
{error, Reason}.
Cancel a previously requested time-out identified by
TRef.
Cancels a previously requested time-out. TRef is
a unique
timer reference returned by the related timer function.
Returns {ok, cancel}, or {error, Reason}
when TRef is not a timer reference.
Send an exit signal with Reason after a specified
Time.
exit_after/2 is the same as
exit_after(Time, self(),
Reason1).
exit_after/3 sends an exit signal with reason
Reason1 to Target,
which can be a local process identifier or an atom of a registered
name. Returns {ok, TRef}
or {error, Reason2}.
Convert Hours+Minutes+Seconds to
Milliseconds.
Returns the number of milliseconds in Hours +
Minutes + Seconds.
Convert Hours to Milliseconds.
Returns the number of milliseconds in Hours.
Send an exit signal with Reason after a specified
Time.
kill_after/1 is the same as
exit_after(Time, self(), kill).
kill_after/2 is the same as
exit_after(Time, Target, kill).
Converts Minutes to Milliseconds.
Returns the number of milliseconds in
Minutes.
Calculate time difference between time stamps.In microseconds
Calculates the time difference Tdiff =
T2 - T1 in microseconds,
where T1 and T2
are time-stamp tuples on the same format as returned from
erlang:timestamp/0 or
os:timestamp/0.
Convert Seconds to Milliseconds.
Returns the number of milliseconds in
Seconds.
Send Message to Destination after a specified
Time.send_after/3
Evaluates Destination ! Message after
Time milliseconds. (Destination can
be a remote or local process identifier, an atom of a registered name or
a tuple {RegName, Node} for a registered name at another node.)
Returns {ok, TRef} or
{error, Reason}.
See also
the Timer Module section in the Efficiency Guide.
send_after/2
Same as send_after(Time, self(),
Message).
Send Message repeatedly at intervals of Time.
send_interval/3
Evaluates Destination ! Message
repeatedly after Time milliseconds.
(Destination can be a remote or local process
identifier, an atom of a registered name or a tuple {RegName, Node}
for a registered name at another node.)
Returns {ok, TRef} or
{error, Reason}.
send_interval/2
Same as send_interval(Time, self(),
Message).
Suspend the calling process for Time milliseconds.
Suspends the process calling this function for
Time milliseconds and then returns ok,
or suspends the process forever if Time is the
atom infinity. Naturally, this
function does not return immediately.
Before OTP 25, timer:sleep/1 did not accept integer
timeout values greater than 16#ffffffff, that is, 2^32-1.
Since OTP 25, arbitrarily high integer values are accepted.
Start a global timer server (named timer_server).
Starts the timer server. Normally, the server does not need
to be started explicitly. It is started dynamically if it
is needed. This is useful during development, but in a
target system the server is to be started explicitly. Use
configuration parameters for
Kernel for this.
Measure the real time it takes to evaluate Fun.tc/3
Calls function timer:tc(Module, Function, Arguments, microsecond).
tc/2
Calls function timer:tc(Fun, Arguments, microsecond).
tc/1
Calls function timer:tc(Fun, microsecond).
Measure the real time it takes to evaluate apply(Module,
Function, Arguments) or apply(Fun, Arguments).In the specified TimeUnittc/4
Evaluates apply(Module, Function,
Arguments) and measures the elapsed real time as
reported by erlang:monotonic_time/0.
Returns {Time, Value}, where
Time is the elapsed real time in
the specified TimeUnit, and Value is what is
returned from the apply.
tc/3
Evaluates apply(Fun, Arguments).
Otherwise the same as tc/4.
tc/2
Evaluates Fun(). Otherwise the same as
tc/3.
Examples
Example 1
The following example shows how to print "Hello World!" in 5 seconds:
1> timer:apply_after(5000, io, format, ["~nHello World!~n", []]).
{ok,TRef}
Hello World!
Example 2
The following example shows a process performing a
certain action, and if this action is not completed within a certain
limit, the process is killed:
Pid = spawn(mod, fun, [foo, bar]),
%% If pid is not finished in 10 seconds, kill him
{ok, R} = timer:kill_after(timer:seconds(10), Pid),
...
%% We change our mind...
timer:cancel(R),
...Notes
A timer can always be removed by calling
cancel/1.
An interval timer, that is, a timer created by evaluating any of the
functions
apply_interval/4,
send_interval/3, and
send_interval/2
is linked to the process to which the timer performs its task.
A one-shot timer, that is, a timer created by evaluating any of the
functions
apply_after/4,
send_after/3,
send_after/2,
exit_after/3,
exit_after/2,
kill_after/2, and
kill_after/1
is not linked to any process. Hence, such a timer is removed only
when it reaches its time-out, or if it is explicitly removed by a call to
cancel/1.