ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/README
Revision: 1.6
Committed: Thu Nov 1 17:32:39 2007 UTC (16 years, 6 months ago) by root
Branch: MAIN
Changes since 1.5: +269 -144 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 NAME
2 root 1.6 EV - perl interface to libev, a high performance full-featured event
3     loop
4 root 1.1
5 root 1.2 SYNOPSIS
6 root 1.4 use EV;
7    
8 root 1.6 # TIMERS
9 root 1.4
10     my $w = EV::timer 2, 0, sub {
11     warn "is called after 2s";
12     };
13    
14     my $w = EV::timer 2, 1, sub {
15     warn "is called roughly every 2s (repeat = 1)";
16     };
17    
18     undef $w; # destroy event watcher again
19    
20 root 1.6 my $w = EV::periodic 0, 60, sub {
21 root 1.4 warn "is called every minute, on the minute, exactly";
22     };
23    
24     # IO
25    
26 root 1.6 my $w = EV::io *STDIN, EV::READ, sub {
27 root 1.5 my ($w, $revents) = @_; # all callbacks get the watcher object and event mask
28 root 1.6 warn "stdin is readable, you entered: ", <STDIN>;
29 root 1.4 };
30    
31     # SIGNALS
32    
33     my $w = EV::signal 'QUIT', sub {
34     warn "sigquit received\n";
35     };
36    
37     my $w = EV::signal 3, sub {
38     warn "sigquit received (this is GNU/Linux, right?)\n";
39     };
40 root 1.5
41     # CHILD/PID STATUS CHANGES
42    
43     my $w = EV::child 666, sub {
44     my ($w, $revents, $status) = @_;
45     };
46 root 1.4
47     # MAINLOOP
48 root 1.6 EV::loop; # loop until EV::loop_done is called
49     EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled
50     EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block
51 root 1.2
52     DESCRIPTION
53 root 1.5 This module provides an interface to libev
54 root 1.6 (<http://software.schmorp.de/pkg/libev.html>).
55 root 1.2
56 root 1.3 BASIC INTERFACE
57     $EV::DIED
58     Must contain a reference to a function that is called when a
59     callback throws an exception (with $@ containing thr error). The
60     default prints an informative message and continues.
61    
62     If this callback throws an exception it will be silently ignored.
63    
64 root 1.6 $time = EV::time
65     Returns the current time in (fractional) seconds since the epoch.
66    
67 root 1.2 $time = EV::now
68 root 1.6 Returns the time the last event loop iteration has been started.
69     This is the time that (relative) timers are based on, and refering
70     to it is usually faster then calling EV::time.
71    
72     $method = EV::ev_method
73     Returns an integer describing the backend used by libev
74     (EV::METHOD_SELECT or EV::METHOD_EPOLL).
75    
76     EV::loop [$flags]
77     Begin checking for events and calling callbacks. It returns when a
78     callback calls EV::loop_done.
79    
80     The $flags argument can be one of the following:
81    
82     0 as above
83     EV::LOOP_ONESHOT block at most once (wait, but do not loop)
84     EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait)
85    
86     EV::loop_done [$how]
87     When called with no arguments or an argument of 1, makes the
88     innermost call to EV::loop return.
89    
90     When called with an agrument of 2, all calls to EV::loop will return
91     as fast as possible.
92    
93     WATCHER
94     A watcher is an object that gets created to record your interest in some
95     event. For instance, if you want to wait for STDIN to become readable,
96     you would create an EV::io watcher for that:
97    
98     my $watcher = EV::io *STDIN, EV::READ, sub {
99     my ($watcher, $revents) = @_;
100     warn "yeah, STDIN should not be readable without blocking!\n"
101     };
102 root 1.2
103 root 1.6 All watchers can be active (waiting for events) or inactive (paused).
104     Only active watchers will have their callbacks invoked. All callbacks
105     will be called with at least two arguments: the watcher and a bitmask of
106     received events.
107    
108     Each watcher type has its associated bit in revents, so you can use the
109     same callback for multiple watchers. The event mask is named after the
110     type, i..e. EV::child sets EV::CHILD, EV::prepare sets EV::PREPARE,
111     EV::periodic sets EV::PERIODIC and so on, with the exception of IO
112     events (which can set both EV::READ and EV::WRITE bits), and EV::timer
113     (which uses EV::TIMEOUT).
114    
115     In the rare case where one wants to create a watcher but not start it at
116     the same time, each constructor has a variant with a trailing "_ns" in
117     its name, e.g. EV::io has a non-starting variant EV::io_ns and so on.
118    
119     Please note that a watcher will automatically be stopped when the
120     watcher object is returned, so you *need* to keep the watcher objects
121     returned by the constructors.
122    
123     WATCHER TYPES
124     Now lets move to the existing watcher types and asociated methods.
125    
126     The following methods are available for all watchers. Then followes a
127     description of each watcher constructor (EV::io, EV::timer,
128     EV::periodic, EV::signal, EV::child, EV::idle, EV::prepare and
129     EV::check), followed by any type-specific methods (if any).
130 root 1.2
131 root 1.6 $w->start
132     Starts a watcher if it isn't active already. Does nothing to an
133     already active watcher. By default, all watchers start out in the
134     active state (see the description of the "_ns" variants if you need
135     stopped watchers).
136    
137     $w->stop
138     Stop a watcher if it is active. Also clear any pending events
139     (events that have been received but that didn't yet result in a
140     callback invocation), regardless of wether the watcher was active or
141     not.
142    
143     $bool = $w->is_active
144     Returns true if the watcher is active, false otherwise.
145    
146     $current_cb = $w->cb
147     $old_cb = $w->cb ($new_cb)
148     Queries the callback on the watcher and optionally changes it. You
149     cna do this at any time.
150 root 1.2
151 root 1.6 $w->trigger ($revents)
152     Call the callback *now* with the given event mask.
153 root 1.2
154 root 1.6 $w = EV::io $fileno_or_fh, $eventmask, $callback
155     $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
156     As long as the returned watcher object is alive, call the $callback
157     when the events specified in $eventmask.
158 root 1.2
159 root 1.6 The $eventmask can be one or more of these constants ORed together:
160 root 1.1
161 root 1.2 EV::READ wait until read() wouldn't block anymore
162     EV::WRITE wait until write() wouldn't block anymore
163    
164 root 1.6 The "io_ns" variant doesn't start (activate) the newly created
165     watcher.
166    
167     $w->set ($fileno_or_fh, $eventmask)
168     Reconfigures the watcher, see the constructor above for details. Can
169     be called at any time.
170    
171     $current_fh = $w->fh
172     $old_fh = $w->fh ($new_fh)
173     Returns the previously set filehandle and optionally set a new one.
174    
175     $current_eventmask = $w->events
176     $old_eventmask = $w->events ($new_eventmask)
177     Returns the previously set event mask and optionally set a new one.
178    
179     $w = EV::timer $after, $repeat, $callback
180     $w = EV::timer_ns $after, $repeat, $callback
181     Calls the callback after $after seconds. If $repeat is non-zero, the
182     timer will be restarted (with the $repeat value as $after) after the
183     callback returns.
184    
185     This means that the callback would be called roughly after $after
186     seconds, and then every $repeat seconds. "Roughly" because the time
187     of callback processing is not taken into account, so the timer will
188     slowly drift. If that isn't acceptable, look at EV::periodic.
189    
190     The timer is based on a monotonic clock, that is if somebody is
191     sitting in front of the machine while the timer is running and
192     changes the system clock, the timer will nevertheless run (roughly)
193     the same time.
194    
195     The "timer_ns" variant doesn't start (activate) the newly created
196     watcher.
197    
198     $w->set ($after, $repeat)
199     Reconfigures the watcher, see the constructor above for details. Can
200     be at any time.
201    
202     $w->again
203     Similar to the "start" method, but has special semantics for
204     repeating timers:
205 root 1.2
206 root 1.6 If the timer is active and repeating, reset the timeout to occur
207     $repeat seconds after now.
208 root 1.2
209 root 1.6 If the timer is active and non-repeating, it will be stopped.
210 root 1.2
211 root 1.6 If the timer is in active and repeating, start it.
212    
213     Otherwise do nothing.
214    
215     This behaviour is useful when you have a timeout for some IO
216     operation. You create a timer object with the same value for $after
217     and $repeat, and then, in the read/write watcher, run the "again"
218     method on the timeout.
219    
220     $w = EV::periodic $at, $interval, $callback
221     $w = EV::periodic_ns $at, $interval, $callback
222 root 1.2 Similar to EV::timer, but the time is given as an absolute point in
223     time ($at), plus an optional $interval.
224    
225     If the $interval is zero, then the callback will be called at the
226 root 1.6 time $at if that is in the future, or as soon as possible if it is
227     in the past. It will not automatically repeat.
228 root 1.2
229     If the $interval is nonzero, then the watcher will always be
230 root 1.6 scheduled to time out at the next "$at + N * $interval" time.
231 root 1.2
232     This can be used to schedule a callback to run at very regular
233     intervals, as long as the processing time is less then the interval
234     (otherwise obviously events will be skipped).
235    
236     Another way to think about it (for the mathematically inclined) is
237 root 1.6 that EV::periodic will try to run the callback at the next possible
238 root 1.2 time where "$time = $at (mod $interval)", regardless of any time
239     jumps.
240    
241 root 1.6 This periodic timer is based on "wallclock time", that is, if the
242     clock changes ("ntp", "date -s" etc.), then the timer will
243     nevertheless run at the specified time. This means it will never
244     drift (it might jitter, but it will not drift).
245    
246     The "periodic_ns" variant doesn't start (activate) the newly created
247 root 1.2 watcher.
248    
249 root 1.6 $w->set ($at, $interval)
250     Reconfigures the watcher, see the constructor above for details. Can
251     be at any time.
252    
253     $w = EV::signal $signal, $callback
254     $w = EV::signal_ns $signal, $callback
255 root 1.4 Call the callback when $signal is received (the signal can be
256 root 1.6 specified by number or by name, just as with kill or %SIG).
257 root 1.4
258     EV will grab the signal for the process (the kernel only allows one
259 root 1.6 component to receive a signal at a time) when you start a signal
260     watcher, and removes it again when you stop it. Perl does the same
261     when you add/remove callbacks to %SIG, so watch out.
262    
263     You can have as many signal watchers per signal as you want.
264 root 1.1
265 root 1.6 The "signal_ns" variant doesn't start (activate) the newly created
266     watcher.
267    
268     $w->set ($signal)
269     Reconfigures the watcher, see the constructor above for details. Can
270     be at any time.
271    
272     $w = EV::child $pid, $callback
273     $w = EV::child_ns $pid, $callback
274     Call the callback when a status change for pid $pid (or any pid if
275     $pid is 0) has been received. More precisely: when the process
276     receives a SIGCHLD, EV will fetch the outstanding exit/wait status
277     for all changed/zombie children and call the callback.
278    
279     Unlike all other callbacks, this callback will be called with an
280     additional third argument which is the exit status. See the
281     "waitpid" function for details.
282    
283     You can have as many pid watchers per pid as you want.
284    
285     The "child_ns" variant doesn't start (activate) the newly created
286     watcher.
287 root 1.1
288 root 1.6 $w->set ($pid)
289     Reconfigures the watcher, see the constructor above for details. Can
290     be at any time.
291    
292     $w = EV::idle $callback
293     $w = EV::idle_ns $callback
294     Call the callback when there are no pending io, timer/periodic,
295     signal or child events, i.e. when the process is idle.
296 root 1.1
297 root 1.6 The process will not block as long as any idle watchers are active,
298     and they will be called repeatedly until stopped.
299 root 1.1
300 root 1.6 The "idle_ns" variant doesn't start (activate) the newly created
301     watcher.
302 root 1.4
303 root 1.6 $w = EV::prepare $callback
304     $w = EV::prepare_ns $callback
305     Call the callback just before the process would block. You can still
306     create/modify any watchers at this point.
307 root 1.1
308 root 1.6 See the EV::check watcher, below, for explanations and an example.
309 root 1.1
310 root 1.6 The "prepare_ns" variant doesn't start (activate) the newly created
311     watcher.
312 root 1.1
313 root 1.6 $w = EV::check $callback
314     $w = EV::check_ns $callback
315     Call the callback just after the process wakes up again (after it
316     has gathered events), but before any other callbacks have been
317     invoked.
318    
319     This is used to integrate other event-based software into the EV
320     mainloop: You register a prepare callback and in there, you create
321     io and timer watchers as required by the other software. Here is a
322     real-world example of integrating Net::SNMP (with some details left
323     out):
324    
325     our @snmp_watcher;
326    
327     our $snmp_prepare = EV::prepare sub {
328     # do nothing unless active
329     $dispatcher->{_event_queue_h}
330     or return;
331    
332     # make the dispatcher handle any outstanding stuff
333    
334     # create an IO watcher for each and every socket
335     @snmp_watcher = (
336     (map { EV::io $_, EV::READ, sub { } }
337     keys %{ $dispatcher->{_descriptors} }),
338     );
339    
340     # if there are any timeouts, also create a timer
341     push @snmp_watcher, EV::timer $event->[Net::SNMP::Dispatcher::_TIME] - EV::now, 0, sub { }
342     if $event->[Net::SNMP::Dispatcher::_ACTIVE];
343     };
344    
345     The callbacks are irrelevant, the only purpose of those watchers is
346     to wake up the process as soon as one of those events occurs (socket
347     readable, or timer timed out). The corresponding EV::check watcher
348     will then clean up:
349    
350     our $snmp_check = EV::check sub {
351     # destroy all watchers
352     @snmp_watcher = ();
353    
354     # make the dispatcher handle any new stuff
355     };
356    
357     The callbacks of the created watchers will not be called as the
358     watchers are destroyed before this cna happen (remember EV::check
359     gets called first).
360 root 1.1
361 root 1.6 The "check_ns" variant doesn't start (activate) the newly created
362     watcher.
363 root 1.1
364 root 1.5 THREADS
365     Threads are not supported by this in any way. Perl pseudo-threads is
366 root 1.6 evil stuff and must die.
367 root 1.2
368     SEE ALSO
369 root 1.6 L<EV::DNS>, L<EV::AnyEvent>.
370 root 1.1
371     AUTHOR
372     Marc Lehmann <schmorp@schmorp.de>
373     http://home.schmorp.de/
374