1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<HTML>
<HEAD>
<!-- refpage -->
<TITLE>timer</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<A HREF="http://www.erlang.se"><IMG BORDER=0 ALT="[Erlang Systems]" SRC="min_head.gif"></A>
<H1>timer</H1>
</CENTER>
<H3>MODULE</H3>
<UL>
timer</UL>
<H3>MODULE SUMMARY</H3>
<UL>
Timer Functions
</UL>
<H3>DESCRIPTION</H3>
<UL>
<P>This module provides useful functions related to time. Unless otherwise
stated, time is always measured in <CODE>milliseconds</CODE>. All
timer functions return immediately, regardless of work carried
out by another process.
<P>Successful evaluations of the timer functions yield return values
containing a timer reference, denoted <CODE>TRef</CODE> below. By using
<CODE>cancel/1</CODE>, the returned reference can be used to cancel any
requested action. A <CODE>TRef</CODE> is an Erlang term, the contents
of which must not be altered.
<P>The timeouts are not exact, but should be <CODE>at least</CODE> as long
as requested.
</UL>
<H3>EXPORTS</H3>
<P><A NAME="start%0"><STRONG><CODE>start() -> ok</CODE></STRONG></A><BR>
<UL>
<P>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 should be started explicitly. Use
configuration parameters for <CODE>kernel</CODE> for this.
</UL>
<P><A NAME="apply_after%4"><STRONG><CODE>apply_after(Time, Module, Function, Arguments) -> {ok, Tref} |
{error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in Milliseconds</CODE></STRONG><BR>
<STRONG><CODE>Module = Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Arguments = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Evaluates <CODE>apply(M, F, A)</CODE> after <CODE>Time</CODE> amount of time
has elapsed. Returns <CODE>{ok, TRef}</CODE>, or <CODE>{error, Reason}</CODE>.
</UL>
<P><A NAME="send_after%3"><STRONG><CODE>send_after(Time, Pid, Message) -> {ok, TRef} | {error,Reason}
</CODE></STRONG></A><BR>
<A NAME="send_after%2"><STRONG><CODE>send_after(Time, Message) -> {ok, TRef} | {error,Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in Milliseconds</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid() | atom()</CODE></STRONG><BR>
<STRONG><CODE>Message = term()</CODE></STRONG><BR>
<STRONG><CODE>Result = {ok, TRef} | {error, Reason}</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P> <P><DL>
<DT><CODE>send_after/3</CODE></DT>
<DD>Evaluates <CODE>Pid ! Message</CODE> after <CODE>Time</CODE> amount
of time has elapsed. (<CODE>Pid</CODE> can also be an atom of a
registered name.) Returns <CODE>{ok, TRef}</CODE>, or
<CODE>{error, Reason}</CODE>.
<BR>
</DD>
<DT><CODE>send_after/2</CODE></DT>
<DD>Same as <CODE>send_after(Time, self(), Message)</CODE>.
<BR>
</DD>
</DL>
</UL>
<P><A NAME="exit_after%3"><STRONG><CODE>exit_after(Time, Pid, Reason1) -> {ok, TRef} | {error,Reason2}</CODE></STRONG></A><BR>
<A NAME="exit_after%2"><STRONG><CODE>exit_after(Time, Reason1) -> {ok, TRef} | {error,Reason2}</CODE></STRONG></A><BR>
<A NAME="kill_after%2"><STRONG><CODE>kill_after(Time, Pid)-> {ok, TRef} | {error,Reason2}</CODE></STRONG></A><BR>
<A NAME="kill_after%1"><STRONG><CODE>kill_after(Time) -> {ok, TRef} | {error,Reason2}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in milliseconds</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid() | atom()</CODE></STRONG><BR>
<STRONG><CODE>Reason1 = Reason2 = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P> <P><DL>
<DT><CODE>exit_after/3</CODE></DT>
<DD>Send an exit signal with reason <CODE>Reason1</CODE> to Pid
<CODE>Pid</CODE>. Returns <CODE>{ok, TRef}</CODE>, or
<CODE>{error, Reason2}</CODE>.
<BR>
</DD>
<DT><CODE>exit_after/2</CODE></DT>
<DD>Same as <CODE>exit_after(Time, self(), Reason1)</CODE>.
<BR>
</DD>
<DT><CODE>kill_after/2</CODE></DT>
<DD>Same as <CODE>exit_after(Time, Pid, kill)</CODE>.
<BR>
</DD>
<DT><CODE>kill_after/1</CODE></DT>
<DD>Same as <CODE>exit_after(Time, self(), kill)</CODE>.
<BR>
</DD>
</DL>
</UL>
<P><A NAME="apply_interval%4"><STRONG><CODE>apply_interval(Time, Module, Function, Arguments) -> {ok, TRef} | {error, Reason}</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in milliseconds</CODE></STRONG><BR>
<STRONG><CODE>Module = Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Arguments = [term()]</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Evaluates <CODE>apply(Module, Function, Arguments)</CODE> repeatedly at
intervals of <CODE>Time</CODE>. Returns <CODE>{ok, TRef}</CODE>, or
<CODE>{error, Reason}</CODE>.
</UL>
<P><A NAME="send_interval%3"><STRONG><CODE>send_interval(Time, Pid, Message) -> {ok, TRef} | {error, Reason}
</CODE></STRONG></A><BR>
<A NAME="send_interval%2"><STRONG><CODE>send_interval(Time, Message) -> {ok, TRef} | {error, Reason}
</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in milliseconds</CODE></STRONG><BR>
<STRONG><CODE>Pid = pid() | atom()</CODE></STRONG><BR>
<STRONG><CODE>Message = term()</CODE></STRONG><BR>
<STRONG><CODE>Reason = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P> <P><DL>
<DT><CODE>send_interval/3</CODE></DT>
<DD>Evaluates <CODE>Pid ! Message</CODE> repeatedly after <CODE>Time</CODE>
amount of time has elapsed. (<CODE>Pid</CODE> can also be an atom of
a registered name.) Returns <CODE>{ok, TRef}</CODE> or
<CODE>{error, Reason}</CODE>.
<BR>
</DD>
<DT><CODE>send_interval/2</CODE></DT>
<DD>Same as <CODE>send_interval(Time, self(), Message)</CODE>.
<BR>
</DD>
</DL>
</UL>
<P><A NAME="cancel%1"><STRONG><CODE>cancel(TRef) -> {ok, cancel} | {error, Reason}</CODE></STRONG></A><BR>
<UL>
<P>Cancels a previously requested timeout. <CODE>TRef</CODE> is a unique
timer reference returned by the timer function in question. Returns
<CODE>{ok, cancel}</CODE>, or <CODE>{error, Reason}</CODE> when <CODE>TRef</CODE>
is not a timer reference.
</UL>
<P><A NAME="sleep%1"><STRONG><CODE>sleep(Time) -> ok</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Time = integer() in milliseconds</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Suspends the process calling this function for <CODE>Time</CODE> amount
of milliseconds and then returns <CODE>ok</CODE>. Naturally, this
function does <STRONG>not</STRONG> return immediately.
</UL>
<P><A NAME="tc%3"><STRONG><CODE>tc(Module, Function, Arguments) -> {Time, Value}
</CODE></STRONG></A><BR>
<P><UL>Types:
<UL>
<STRONG><CODE>Module = Function = atom()</CODE></STRONG><BR>
<STRONG><CODE>Arguments = [term()]</CODE></STRONG><BR>
<STRONG><CODE>Time = integer() in microseconds</CODE></STRONG><BR>
<STRONG><CODE>Value = term()</CODE></STRONG><BR>
</UL>
</UL>
<UL>
<P>Evaluates <CODE>apply(Module, Function, Arguments)</CODE> and measures
the elapsed real time. Returns <CODE>{Time, Value}</CODE>, where
<CODE>Time</CODE> is the elapsed real time in <STRONG>microseconds</STRONG>,
and <CODE>Value</CODE> is what is returned from the apply.
</UL>
<P><A NAME="seconds%1"><STRONG><CODE>seconds(Seconds) -> Milliseconds</CODE></STRONG></A><BR>
<UL>
<P>Returns the number of milliseconds in <CODE>Seconds</CODE>.
</UL>
<P><A NAME="minutes%1"><STRONG><CODE>minutes(Minutes) -> Milliseconds</CODE></STRONG></A><BR>
<UL>
<P>Return the number of milliseconds in <CODE>Minutes</CODE>.
</UL>
<P><A NAME="hours%1"><STRONG><CODE>hours(Hours) -> Milliseconds</CODE></STRONG></A><BR>
<UL>
<P>Returns the number of milliseconds in <CODE>Hours</CODE>.
</UL>
<P><A NAME="hms%3"><STRONG><CODE>hms(Hours, Minutes, Seconds) -> Milliseconds
</CODE></STRONG></A><BR>
<UL>
<P>Returns the number of milliseconds in <CODE>Hours + Minutes +
Seconds</CODE>.
</UL>
<H3>Examples</H3>
<UL>
<P>This example illustrates how to print out "Hello World!" in 5 seconds:
<P> <PRE> 1> timer:apply_after(5000, io, format, ["~nHello World!~n", []]).
{ok,TRef}
Hello World!
2>
</PRE><P>The following coding example illustrates a process which performs a
certain action and if this action is not completed within a certain
limit, then the process is killed.<PRE> 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),
...
</PRE></UL>
<H3>WARNING</H3>
<UL>
<P>A timer can always be removed by calling <CODE>cancel/1</CODE>.
<P>An interval timer, i.e. a timer created by evaluating any of the
functions <CODE>apply_interval/4</CODE>, <CODE>send_interval/3</CODE>, and
<CODE>send_interval/2</CODE>, is linked to the process towards which
the timer performs its task.
<P>A one-shot timer, i.e. a timer created by evaluating any of the
functions <CODE>apply_after/4</CODE>, <CODE>send_after/3</CODE>,
<CODE>send_after/2</CODE>, <CODE>exit_after/3</CODE>, <CODE>exit_after/2</CODE>,
<CODE>kill_after/2</CODE>, and <CODE>kill_after/1</CODE> is not linked to any
process. Hence, such a timer is removed only when it reaches its
timeout, or if it is explicitly removed by a call to <CODE>cancel/1</CODE>.
</UL>
<H3>AUTHORS</H3>
<UL>
Sebastian Strollo - support@erlang.ericsson.se<BR>
Dan Gudmundsson - support@erlang.ericsson.se<BR>
Peter Högfeldt - support@erlang.ericsson.se<BR>
</UL>
<CENTER>
<HR>
<FONT SIZE=-1>stdlib 1.10<BR>
Copyright © 1991-2001
<A HREF="http://www.erlang.se">Ericsson Utvecklings AB</A><BR>
<!--#include virtual="/ssi/otp_footer.html"-->
</FONT>
</CENTER>
</BODY>
</HTML>
|