summaryrefslogtreecommitdiff
path: root/lib/stdlib/doc/src/timer.xml
blob: 75706f7b2dffb6d8046f8ebade6a0ac46d84e408 (plain)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE erlref SYSTEM "erlref.dtd">

<erlref>
  <header>
    <copyright>
      <year>1996</year><year>2023</year>
      <holder>Ericsson AB. All Rights Reserved.</holder>
    </copyright>
    <legalnotice>
      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.
    
    </legalnotice>

    <title>timer</title>
    <prepared>Sebastian Strollo</prepared>
    <responsible>Bjarne D&auml;cker</responsible>
    <docno>1</docno>
    <approved>Bjarne D&auml;cker</approved>
    <checked></checked>
    <date>1998-09-09</date>
    <rev>D</rev>
    <file>timer.xml</file>
  </header>
  <module since="">timer</module>
  <modulesummary>Timer functions.</modulesummary>
  <description>
    <p>This module provides useful functions related to time. Unless otherwise
      stated, time is always measured in <em>milliseconds</em>. All
      timer functions return immediately, regardless of work done by another
      process.</p>
    <p>Successful evaluations of the timer functions give return values
      containing a timer reference, denoted <c>TRef</c>. By using
      <seemfa marker="#cancel/1"><c>cancel/1</c></seemfa>,
      the returned reference can be used to cancel any
      requested action. A <c>TRef</c> is an Erlang term, which contents
      must not be changed.</p>
    <p>The time-outs are not exact, but are <em>at least</em> as long
      as requested.</p>
    <p>Creating timers using
      <seemfa marker="erts:erlang#send_after/3">erlang:send_after/3</seemfa> and
      <seemfa marker="erts:erlang#start_timer/3">erlang:start_timer/3</seemfa>
      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
      <seeguide marker="system/efficiency_guide:commoncaveats#timer-module">the
      Timer Module section in the Efficiency Guide</seeguide>.</p>
  </description>

  <datatypes>
    <datatype>
      <name name="time"/>
      <desc><p>Time in milliseconds.</p></desc>
    </datatype>
    <datatype>
      <name name="tref"/>
      <desc><p>A timer reference.</p></desc>
    </datatype>
  </datatypes>

  <funcs>
    <func>
      <name name="apply_after" arity="4" since=""/>
      <fsummary>Spawn a process evaluating <c>Module:Function(Arguments)</c>
        after a specified <c>Time</c>.</fsummary>
      <desc>
        <p>Evaluates <c>spawn(<anno>Module</anno>, <anno>Function</anno>,
          <anno>Arguments</anno>)</c> after <c><anno>Time</anno></c>
          milliseconds.</p>
        <p>Returns <c>{ok, <anno>TRef</anno>}</c> or
          <c>{error, <anno>Reason</anno>}</c>.</p>
      </desc>
    </func>

    <func>
      <name name="apply_interval" arity="4" since=""/>
      <fsummary>Spawn a process evaluating <c>Module:Function(Arguments)</c>
        repeatedly at intervals of <c>Time</c>.</fsummary>
      <desc>
        <p>Evaluates <c>spawn(<anno>Module</anno>, <anno>Function</anno>,
          <anno>Arguments</anno>)</c> repeatedly at intervals of
          <c><anno>Time</anno></c>, irrespective of whether a previously
          spawned process has finished or not.</p>
        <warning>
          <p>If the execution time of the spawned process is, on average,
            greater than the given <c><anno>Time</anno></c>, 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
            <c>[timer:apply_interval(1, timer, sleep, [1000]) || _ &lt;- lists:seq(1, 1000)]</c>,
            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
            <seeguide marker="system/efficiency_guide:advanced#system-limits">System
            Limits section in the Effiency Guide</seeguide>).</p>
        </warning>
        <p>Returns <c>{ok, <anno>TRef</anno>}</c> or
          <c>{error, <anno>Reason</anno>}</c>.</p>
      </desc>
    </func>

    <func>
      <name name="apply_repeatedly" arity="4" since="OTP 26.0"/>
      <fsummary>Spawn a process evaluating <c>Module:Function(Arguments)</c>
        repeatedly at intervals of <c>Time</c>.</fsummary>
      <desc>
        <p>Evaluates <c>spawn(<anno>Module</anno>, <anno>Function</anno>,
          <anno>Arguments</anno>)</c> repeatedly at intervals of
          <c><anno>Time</anno></c>, waiting for the spawned process to
          finish before starting the next.</p>
        <p>If the execution time of the spawned process is greater than the
          given <c><anno>Time</anno></c>, 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 <c><anno>Time</anno></c>, the amount of applies
          made over a large amount of time will be the same even if some
          individual execution times are larger than
          <c><anno>Time</anno></c>. The system will try to catch up as soon
          as possible. For example, if one apply takes
          <c>2.5*<anno>Time</anno></c>, the following two applies will be
          made immediately one after the other in sequence.</p>
        <p>Returns <c>{ok, <anno>TRef</anno>}</c> or
          <c>{error, <anno>Reason</anno>}</c>.</p>
      </desc>
    </func>

    <func>
      <name name="cancel" arity="1" since=""/>
      <fsummary>Cancel a previously requested time-out identified by
        <c>TRef</c>.</fsummary>
      <desc>
        <p>Cancels a previously requested time-out. <c><anno>TRef</anno></c> is
          a unique
          timer reference returned by the related timer function.</p>
        <p>Returns <c>{ok, cancel}</c>, or <c>{error, <anno>Reason</anno>}</c>
          when <c><anno>TRef</anno></c> is not a timer reference.</p>
      </desc>
    </func>

    <func>
      <name name="exit_after" arity="2" since=""/>
      <name name="exit_after" arity="3" since=""/>
      <fsummary>Send an exit signal with <c>Reason</c> after a specified
        <c>Time</c>.</fsummary>
      <desc>
        <p><c>exit_after/2</c> is the same as
          <c>exit_after(<anno>Time</anno>, self(),
          <anno>Reason1</anno>)</c>.</p>
        <p><c>exit_after/3</c> sends an exit signal with reason
          <c><anno>Reason1</anno></c> to <c><anno>Target</anno></c>,
          which can be a local process identifier or an atom of a registered
          name. Returns <c>{ok, <anno>TRef</anno>}</c>
          or <c>{error, <anno>Reason2</anno>}</c>.</p>
      </desc>
    </func>

    <func>
      <name name="hms" arity="3" since=""/>
      <fsummary>Convert <c>Hours</c>+<c>Minutes</c>+<c>Seconds</c> to
        <c>Milliseconds</c>.</fsummary>
      <desc>
        <p>Returns the number of milliseconds in <c><anno>Hours</anno> +
          <anno>Minutes</anno> + <anno>Seconds</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="hours" arity="1" since=""/>
      <fsummary>Convert <c>Hours</c> to <c>Milliseconds</c>.</fsummary>
      <desc>
        <p>Returns the number of milliseconds in <c><anno>Hours</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="kill_after" arity="1" since=""/>
      <name name="kill_after" arity="2" since=""/>
      <fsummary>Send an exit signal with <c>Reason</c> after a specified
        <c>Time</c>.</fsummary>
      <desc>
        <p><c>kill_after/1</c> is the same as
          <c>exit_after(<anno>Time</anno>, self(), kill)</c>.</p>
        <p><c>kill_after/2</c> is the same as
          <c>exit_after(<anno>Time</anno>, <anno>Target</anno>, kill)</c>.</p>
      </desc>
    </func>

    <func>
      <name name="minutes" arity="1" since=""/>
      <fsummary>Converts <c>Minutes</c> to <c>Milliseconds</c>.</fsummary>
      <desc>
        <p>Returns the number of milliseconds in
          <c><anno>Minutes</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="now_diff" arity="2" since=""/>
      <fsummary>Calculate time difference between time stamps.</fsummary>
      <type_desc variable="Tdiff">In microseconds</type_desc>
      <desc>
        <p>Calculates the time difference <c><anno>Tdiff</anno> =
          <anno>T2</anno> - <anno>T1</anno></c> in <em>microseconds</em>,
          where <c><anno>T1</anno></c> and <c><anno>T2</anno></c>
          are time-stamp tuples on the same format as returned from
          <seemfa marker="erts:erlang#timestamp/0">
          <c>erlang:timestamp/0</c></seemfa> or
          <seemfa marker="kernel:os#timestamp/0">
          <c>os:timestamp/0</c></seemfa>.</p>
      </desc>
    </func>

    <func>
      <name name="seconds" arity="1" since=""/>
      <fsummary>Convert <c>Seconds</c> to <c>Milliseconds</c>.</fsummary>
      <desc>
        <p>Returns the number of milliseconds in
          <c><anno>Seconds</anno></c>.</p>
      </desc>
    </func>

    <func>
      <name name="send_after" arity="2" since=""/>
      <name name="send_after" arity="3" since=""/>
      <fsummary>Send <c>Message</c> to <c>Destination</c> after a specified
        <c>Time</c>.</fsummary>
      <desc>
        <taglist>
          <tag><c>send_after/3</c></tag>
          <item>
            <p>Evaluates <c><anno>Destination</anno> ! <anno>Message</anno></c> after
              <c><anno>Time</anno></c> milliseconds. (<c><anno>Destination</anno></c> can
              be a remote or local process identifier, an atom of a registered name or
              a tuple <c>{RegName, Node}</c> for a registered name at another node.)</p>
            <p>Returns <c>{ok, <anno>TRef</anno>}</c> or
              <c>{error, <anno>Reason</anno>}</c>.</p>
            <p>See also
              <seeguide marker="system/efficiency_guide:commoncaveats#timer-module">
              the Timer Module section in the Efficiency Guide</seeguide>.</p>
          </item>
          <tag><c>send_after/2</c></tag>
          <item>
            <p>Same as <c>send_after(<anno>Time</anno>, self(),
              <anno>Message</anno>)</c>.</p>
          </item>
        </taglist>
      </desc>
    </func>

    <func>
      <name name="send_interval" arity="2" since=""/>
      <name name="send_interval" arity="3" since=""/>
      <fsummary>Send <c>Message</c> repeatedly at intervals of <c>Time</c>.
      </fsummary>
      <desc>
        <taglist>
          <tag><c>send_interval/3</c></tag>
          <item>
            <p>Evaluates <c><anno>Destination</anno> ! <anno>Message</anno></c>
              repeatedly after <c><anno>Time</anno></c> milliseconds.
              (<c><anno>Destination</anno></c> can be a remote or local process
              identifier, an atom of a registered name or a tuple <c>{RegName, Node}</c>
              for a registered name at another node.)</p>
            <p>Returns <c>{ok, <anno>TRef</anno>}</c> or
              <c>{error, <anno>Reason</anno>}</c>.</p>
          </item>
          <tag><c>send_interval/2</c></tag>
          <item>
            <p>Same as <c>send_interval(<anno>Time</anno>, self(),
              <anno>Message</anno>)</c>.</p>
          </item>
        </taglist>
      </desc>
    </func>

    <func>
      <name name="sleep" arity="1" since=""/>
      <fsummary>Suspend the calling process for <c>Time</c> milliseconds.
      </fsummary>
      <desc>
        <p>Suspends the process calling this function for
          <c><anno>Time</anno></c> milliseconds and then returns <c>ok</c>,
          or suspends the process forever if <c><anno>Time</anno></c> is the
          atom <c>infinity</c>. Naturally, this
          function does <em>not</em> return immediately.</p>
        <note>
          <p>Before OTP 25, <c>timer:sleep/1</c> did not accept integer
            timeout values greater than <c>16#ffffffff</c>, that is, <c>2^32-1</c>.
            Since OTP 25, arbitrarily high integer values are accepted.</p>
        </note>
      </desc>
    </func>

    <func>
      <name name="start" arity="0" since=""/>
      <fsummary>Start a global timer server (named <c>timer_server</c>).
      </fsummary>
      <desc>
        <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 is to be started explicitly. Use
          configuration parameters for
          <seeapp marker="kernel:index">Kernel</seeapp> for this.</p>
      </desc>
    </func>

    <func>
      <name name="tc" arity="1" since="OTP R14B03"/>
      <name name="tc" arity="2" clause_i="1" since="OTP R14B"/>
      <name name="tc" arity="3" clause_i="1" since=""/>
      <fsummary>Measure the real time it takes to evaluate <c>Fun</c>.</fsummary>
      <desc>
        <taglist>
          <tag><c>tc/3</c></tag>
          <item>
            <p>Calls function <c>timer:tc(Module, Function, Arguments, microsecond)</c>.</p>
          </item>
          <tag><c>tc/2</c></tag>
          <item>
            <p>Calls function <c>timer:tc(Fun, Arguments, microsecond)</c>.</p>
          </item>
          <tag><c>tc/1</c></tag>
          <item>
            <p>Calls function <c>timer:tc(Fun, microsecond)</c>.</p>
          </item>
        </taglist>
      </desc>
    </func>

    <func>
      <name name="tc" arity="2" clause_i="2" since="OTP 26.0"/>
      <name name="tc" arity="3" clause_i="2" since="OTP 26.0"/>
      <name name="tc" arity="4" since="OTP 26.0"/>
      <fsummary>Measure the real time it takes to evaluate <c>apply(Module,
        Function, Arguments)</c> or <c>apply(Fun, Arguments)</c>.</fsummary>
      <type_desc variable="Time">In the specified <c>TimeUnit</c></type_desc>
      <desc>
        <taglist>
          <tag><c>tc/4</c></tag>
          <item>
            <p>Evaluates <c>apply(<anno>Module</anno>, <anno>Function</anno>,
              <anno>Arguments</anno>)</c> and measures the elapsed real time as 
              reported by <seemfa marker="erts:erlang#monotonic_time/0">
              <c>erlang:monotonic_time/0</c></seemfa>.</p>
            <p>Returns <c>{<anno>Time</anno>, <anno>Value</anno>}</c>, where
              <c><anno>Time</anno></c> is the elapsed real time in
              the specified <c>TimeUnit</c>, and <c><anno>Value</anno></c> is what is
              returned from the apply.</p>
          </item>
          <tag><c>tc/3</c></tag>
          <item>
            <p>Evaluates <c>apply(<anno>Fun</anno>, <anno>Arguments</anno>)</c>.
              Otherwise the same as <c>tc/4</c>.</p>
          </item>
          <tag><c>tc/2</c></tag>
          <item>
            <p>Evaluates <c><anno>Fun</anno>()</c>. Otherwise the same as
              <c>tc/3</c>.</p>
          </item>
        </taglist>
      </desc>
    </func>
  </funcs>

  <section>
    <title>Examples</title>
    <p><em>Example 1</em></p>
    <p>The following example shows how to print "Hello World!" in 5 seconds:</p>
    <pre>
1> <input>timer:apply_after(5000, io, format, ["~nHello World!~n", []]).</input>
{ok,TRef}
Hello World!</pre>

    <p><em>Example 2</em></p>
    <p>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:</p>
    <code type="none">
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),
...</code>
  </section>

  <section>
    <title>Notes</title>
    <p>A timer can always be removed by calling
      <seemfa marker="#cancel/1"><c>cancel/1</c></seemfa>.</p>

    <p>An interval timer, that is, a timer created by evaluating any of the
      functions
      <seemfa marker="#apply_interval/4"><c>apply_interval/4</c></seemfa>,
      <seemfa marker="#send_interval/3"><c>send_interval/3</c></seemfa>, and
      <seemfa marker="#send_interval/2"><c>send_interval/2</c></seemfa>
      is linked to the process to which the timer performs its task.</p>

    <p>A one-shot timer, that is, a timer created by evaluating any of the
      functions
      <seemfa marker="#apply_after/4"><c>apply_after/4</c></seemfa>,
      <seemfa marker="#send_after/3"><c>send_after/3</c></seemfa>,
      <seemfa marker="#send_after/2"><c>send_after/2</c></seemfa>,
      <seemfa marker="#exit_after/3"><c>exit_after/3</c></seemfa>,
      <seemfa marker="#exit_after/2"><c>exit_after/2</c></seemfa>,
      <seemfa marker="#kill_after/2"><c>kill_after/2</c></seemfa>, and
      <seemfa marker="#kill_after/1"><c>kill_after/1</c></seemfa>
      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
      <seemfa marker="#cancel/1"><c>cancel/1</c></seemfa>.</p>
  </section>
</erlref>