timer - Timer 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.
time() = integer() >= 0 Time in milliseconds. tref() A timer reference.
apply_after(Time, Module, Function, Arguments) -> {ok, TRef} | {error, Reason} Types: Time = time() Module = module() Function = atom() Arguments = [term()] TRef = tref() Reason = term() Evaluates apply(Module, Function, Arguments) after Time milliseconds. Returns {ok, TRef} or {error, Reason}. apply_interval(Time, Module, Function, Arguments) -> {ok, TRef} | {error, Reason} Types: Time = time() Module = module() Function = atom() Arguments = [term()] TRef = tref() Reason = term() Evaluates apply(Module, Function, Arguments) repeatedly at intervals of Time. Returns {ok, TRef} or {error, Reason}. cancel(TRef) -> {ok, cancel} | {error, Reason} Types: TRef = tref() Reason = term() 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. exit_after(Time, Reason1) -> {ok, TRef} | {error, Reason2} exit_after(Time, Pid, Reason1) -> {ok, TRef} | {error, Reason2} Types: Time = time() Pid = pid() | (RegName :: atom()) TRef = tref() Reason1 = Reason2 = term() exit_after/2 is the same as exit_after(Time, self(), Reason1). exit_after/3 sends an exit signal with reason Reason1 to pid Pid. Returns {ok, TRef} or {error, Reason2}. hms(Hours, Minutes, Seconds) -> MilliSeconds Types: Hours = Minutes = Seconds = MilliSeconds = integer() >= 0 Returns the number of milliseconds in Hours + Minutes + Seconds. hours(Hours) -> MilliSeconds Types: Hours = MilliSeconds = integer() >= 0 Returns the number of milliseconds in Hours. kill_after(Time) -> {ok, TRef} | {error, Reason2} kill_after(Time, Pid) -> {ok, TRef} | {error, Reason2} Types: Time = time() Pid = pid() | (RegName :: atom()) TRef = tref() Reason2 = term() kill_after/1 is the same as exit_after(Time, self(), kill). kill_after/2 is the same as exit_after(Time, Pid, kill). minutes(Minutes) -> MilliSeconds Types: Minutes = MilliSeconds = integer() >= 0 Returns the number of milliseconds in Minutes. now_diff(T2, T1) -> Tdiff Types: T1 = T2 = erlang:timestamp() Tdiff = integer() 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. seconds(Seconds) -> MilliSeconds Types: Seconds = MilliSeconds = integer() >= 0 Returns the number of milliseconds in Seconds. send_after(Time, Message) -> {ok, TRef} | {error, Reason} send_after(Time, Pid, Message) -> {ok, TRef} | {error, Reason} Types: Time = time() Pid = pid() | (RegName :: atom()) Message = term() TRef = tref() Reason = term() send_after/3: Evaluates Pid ! Message after Time milliseconds. (Pid can also be an atom of a registered name.) Returns {ok, TRef} or {error, Reason}. send_after/2: Same as send_after(Time, self(), Message). send_interval(Time, Message) -> {ok, TRef} | {error, Reason} send_interval(Time, Pid, Message) -> {ok, TRef} | {error, Reason} Types: Time = time() Pid = pid() | (RegName :: atom()) Message = term() TRef = tref() Reason = term() send_interval/3: Evaluates Pid ! Message repeatedly after Time milliseconds. (Pid can also be an atom of a registered name.) Returns {ok, TRef} or {error, Reason}. send_interval/2: Same as send_interval(Time, self(), Message). sleep(Time) -> ok Types: Time = timeout() 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. start() -> ok 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. tc(Fun) -> {Time, Value} tc(Fun, Arguments) -> {Time, Value} tc(Module, Function, Arguments) -> {Time, Value} Types: Module = module() Function = atom() Arguments = [term()] Time = integer() In microseconds Value = term() tc/3: Evaluates apply(Module, Function, Arguments) and measures the elapsed real time as reported by os:timestamp/0. Returns {Time, Value}, where Time is the elapsed real time in microseconds, and Value is what is returned from the apply. tc/2: Evaluates apply(Fun, Arguments). Otherwise the same as tc/3. tc/1: Evaluates Fun(). Otherwise the same as tc/2.
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), ...
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.
Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.
Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.
Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.
Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.
The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.
Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.
Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.
Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.