ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/README
(Generate patch)

Comparing EV/README (file contents):
Revision 1.5 by root, Thu Nov 1 13:33:12 2007 UTC vs.
Revision 1.6 by root, Thu Nov 1 17:32:39 2007 UTC

1NAME 1NAME
2 EV - perl interface to libevent, monkey.org/~provos/libevent/ 2 EV - perl interface to libev, a high performance full-featured event
3 loop
3 4
4SYNOPSIS 5SYNOPSIS
5 use EV; 6 use EV;
6 7
7 # TIMER 8 # TIMERS
8 9
9 my $w = EV::timer 2, 0, sub { 10 my $w = EV::timer 2, 0, sub {
10 warn "is called after 2s"; 11 warn "is called after 2s";
11 }; 12 };
12 13
14 warn "is called roughly every 2s (repeat = 1)"; 15 warn "is called roughly every 2s (repeat = 1)";
15 }; 16 };
16 17
17 undef $w; # destroy event watcher again 18 undef $w; # destroy event watcher again
18 19
19 my $w = EV::timer_abs 0, 60, sub { 20 my $w = EV::periodic 0, 60, sub {
20 warn "is called every minute, on the minute, exactly"; 21 warn "is called every minute, on the minute, exactly";
21 }; 22 };
22 23
23 # IO 24 # IO
24 25
25 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { 26 my $w = EV::io *STDIN, EV::READ, sub {
26 my ($w, $revents) = @_; # all callbacks get the watcher object and event mask 27 my ($w, $revents) = @_; # all callbacks get the watcher object and event mask
27 if ($revents & EV::TIMEOUT) {
28 warn "nothing received on stdin for 10 seconds, retrying";
29 } else {
30 warn "stdin is readable, you entered: ", <STDIN>; 28 warn "stdin is readable, you entered: ", <STDIN>;
31 }
32 };
33 $w->timeout (10);
34
35 my $w = EV::timed_io \*STDIN, EV::READ, 30, sub {
36 my ($w, $revents) = @_;
37 if ($revents & EV::TIMEOUT) {
38 warn "nothing entered within 30 seconds, bye bye.\n";
39 $w->stop;
40 } else {
41 my $line = <STDIN>;
42 warn "you entered something, you again have 30 seconds.\n";
43 }
44 }; 29 };
45 30
46 # SIGNALS 31 # SIGNALS
47 32
48 my $w = EV::signal 'QUIT', sub { 33 my $w = EV::signal 'QUIT', sub {
58 my $w = EV::child 666, sub { 43 my $w = EV::child 666, sub {
59 my ($w, $revents, $status) = @_; 44 my ($w, $revents, $status) = @_;
60 }; 45 };
61 46
62 # MAINLOOP 47 # MAINLOOP
63 EV::dispatch; # loop as long as watchers are active 48 EV::loop; # loop until EV::loop_done is called
64 EV::loop; # the same thing
65 EV::loop EV::LOOP_ONESHOT; # block until some events could be handles 49 EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled
66 EV::loop EV::LOOP_NONBLOCK; # check and handle some events, but do not wait 50 EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block
67 51
68DESCRIPTION 52DESCRIPTION
69 This module provides an interface to libev 53 This module provides an interface to libev
70 (<http://software.schmorp.de/pkg/libev.html>). You probably should 54 (<http://software.schmorp.de/pkg/libev.html>).
71 acquaint yourself with its documentation and source code to be able to
72 use this module fully.
73 55
74BASIC INTERFACE 56BASIC INTERFACE
75 $EV::NPRI
76 How many priority levels are available.
77
78 $EV::DIED 57 $EV::DIED
79 Must contain a reference to a function that is called when a 58 Must contain a reference to a function that is called when a
80 callback throws an exception (with $@ containing thr error). The 59 callback throws an exception (with $@ containing thr error). The
81 default prints an informative message and continues. 60 default prints an informative message and continues.
82 61
83 If this callback throws an exception it will be silently ignored. 62 If this callback throws an exception it will be silently ignored.
84 63
64 $time = EV::time
65 Returns the current time in (fractional) seconds since the epoch.
66
85 $time = EV::now 67 $time = EV::now
86 Returns the time in (fractional) seconds since the epoch. 68 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.
87 71
88 $version = EV::version
89 $method = EV::method 72 $method = EV::ev_method
90 Return version string and event polling method used. 73 Returns an integer describing the backend used by libev
74 (EV::METHOD_SELECT or EV::METHOD_EPOLL).
91 75
92 EV::loop $flags # EV::LOOP_ONCE, EV::LOOP_ONESHOT 76 EV::loop [$flags]
93 EV::loopexit $after 77 Begin checking for events and calling callbacks. It returns when a
94 Exit any active loop or dispatch after $after seconds or immediately 78 callback calls EV::loop_done.
95 if $after is missing or zero.
96 79
97 EV::dispatch 80 The $flags argument can be one of the following:
98 Same as "EV::loop 0".
99 81
100 EV::event $callback 82 0 as above
101 Creates a new event watcher waiting for nothing, calling the given 83 EV::LOOP_ONESHOT block at most once (wait, but do not loop)
102 callback. 84 EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait)
103 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
103 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
131 $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
151 $w->trigger ($revents)
152 Call the callback *now* with the given event mask.
153
104 my $w = EV::io $fileno_or_fh, $eventmask, $callback 154 $w = EV::io $fileno_or_fh, $eventmask, $callback
105 my $w = EV::io_ns $fileno_or_fh, $eventmask, $callback 155 $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
106 As long as the returned watcher object is alive, call the $callback 156 As long as the returned watcher object is alive, call the $callback
107 when the events specified in $eventmask happen. Initially, the 157 when the events specified in $eventmask.
108 timeout is disabled.
109 158
110 You can additionall set a timeout to occur on the watcher, but note
111 that this timeout will not be reset when you get an I/O event in the
112 EV::PERSIST case, and reaching a timeout will always stop the
113 watcher even in the EV::PERSIST case.
114
115 If you want a timeout to occur only after a specific time of
116 inactivity, set a repeating timeout and do NOT use EV::PERSIST.
117
118 Eventmask can be one or more of these constants ORed together: 159 The $eventmask can be one or more of these constants ORed together:
119 160
120 EV::READ wait until read() wouldn't block anymore 161 EV::READ wait until read() wouldn't block anymore
121 EV::WRITE wait until write() wouldn't block anymore 162 EV::WRITE wait until write() wouldn't block anymore
122 EV::PERSIST stay active after a (non-timeout) event occured
123 163
124 The "io_ns" variant doesn't add/start the newly created watcher. 164 The "io_ns" variant doesn't start (activate) the newly created
165 watcher.
125 166
126 my $w = EV::timed_io $fileno_or_fh, $eventmask, $timeout, $callback 167 $w->set ($fileno_or_fh, $eventmask)
127 my $w = EV::timed_io_ns $fileno_or_fh, $eventmask, $timeout, $callback 168 Reconfigures the watcher, see the constructor above for details. Can
128 Same as "io" and "io_ns", but also specifies a timeout (as if there 169 be called at any time.
129 was a call to "$w->timeout ($timout, 1)". The persist flag is not
130 allowed and will automatically be cleared. The watcher will be
131 restarted after each event.
132 170
133 If the timeout is zero or undef, no timeout will be set, and a 171 $current_fh = $w->fh
134 normal watcher (with the persist flag set!) will be created. 172 $old_fh = $w->fh ($new_fh)
173 Returns the previously set filehandle and optionally set a new one.
135 174
136 This has the effect of timing out after the specified period of 175 $current_eventmask = $w->events
137 inactivity has happened. 176 $old_eventmask = $w->events ($new_eventmask)
177 Returns the previously set event mask and optionally set a new one.
138 178
139 Due to the design of libevent, this is also relatively inefficient,
140 having one or two io watchers and a separate timeout watcher that
141 you reset on activity (by calling its "start" method) is usually
142 more efficient.
143
144 my $w = EV::timer $after, $repeat, $callback 179 $w = EV::timer $after, $repeat, $callback
145 my $w = EV::timer_ns $after, $repeat, $callback 180 $w = EV::timer_ns $after, $repeat, $callback
146 Calls the callback after $after seconds. If $repeat is true, the 181 Calls the callback after $after seconds. If $repeat is non-zero, the
147 timer will be restarted after the callback returns. This means that 182 timer will be restarted (with the $repeat value as $after) after the
148 the callback would be called roughly every $after seconds, prolonged 183 callback returns.
149 by the time the callback takes.
150 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
151 The "timer_ns" variant doesn't add/start the newly created watcher. 195 The "timer_ns" variant doesn't start (activate) the newly created
196 watcher.
152 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
206 If the timer is active and repeating, reset the timeout to occur
207 $repeat seconds after now.
208
209 If the timer is active and non-repeating, it will be stopped.
210
211 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
153 my $w = EV::timer_abs $at, $interval, $callback 221 $w = EV::periodic_ns $at, $interval, $callback
154 my $w = EV::timer_abs_ns $at, $interval, $callback
155 Similar to EV::timer, but the time is given as an absolute point in 222 Similar to EV::timer, but the time is given as an absolute point in
156 time ($at), plus an optional $interval. 223 time ($at), plus an optional $interval.
157 224
158 If the $interval is zero, then the callback will be called at the 225 If the $interval is zero, then the callback will be called at the
159 time $at if that is in the future, or as soon as possible if its in 226 time $at if that is in the future, or as soon as possible if it is
160 the past. It will not automatically repeat. 227 in the past. It will not automatically repeat.
161 228
162 If the $interval is nonzero, then the watcher will always be 229 If the $interval is nonzero, then the watcher will always be
163 scheduled to time out at the next "$at + integer * $interval" time. 230 scheduled to time out at the next "$at + N * $interval" time.
164 231
165 This can be used to schedule a callback to run at very regular 232 This can be used to schedule a callback to run at very regular
166 intervals, as long as the processing time is less then the interval 233 intervals, as long as the processing time is less then the interval
167 (otherwise obviously events will be skipped). 234 (otherwise obviously events will be skipped).
168 235
169 Another way to think about it (for the mathematically inclined) is 236 Another way to think about it (for the mathematically inclined) is
170 that "timer_abs" will try to tun the callback at the next possible 237 that EV::periodic will try to run the callback at the next possible
171 time where "$time = $at (mod $interval)", regardless of any time 238 time where "$time = $at (mod $interval)", regardless of any time
172 jumps. 239 jumps.
173 240
241 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
174 The "timer_abs_ns" variant doesn't add/start the newly created 246 The "periodic_ns" variant doesn't start (activate) the newly created
175 watcher. 247 watcher.
176 248
249 $w->set ($at, $interval)
250 Reconfigures the watcher, see the constructor above for details. Can
251 be at any time.
252
177 my $w = EV::signal $signal, $callback 253 $w = EV::signal $signal, $callback
178 my $w = EV::signal_ns $signal, $callback 254 $w = EV::signal_ns $signal, $callback
179 Call the callback when $signal is received (the signal can be 255 Call the callback when $signal is received (the signal can be
180 specified by number or by name, just as with kill or %SIG). Signal 256 specified by number or by name, just as with kill or %SIG).
181 watchers are persistent no natter what.
182 257
183 EV will grab the signal for the process (the kernel only allows one 258 EV will grab the signal for the process (the kernel only allows one
184 component to receive signals) when you start a signal watcher, and 259 component to receive a signal at a time) when you start a signal
185 removes it again when you stop it. Pelr does the same when you 260 watcher, and removes it again when you stop it. Perl does the same
186 add/remove callbacks to %SIG, so watch out. 261 when you add/remove callbacks to %SIG, so watch out.
187 262
188 Unfortunately, only one handler can be registered per signal. Screw 263 You can have as many signal watchers per signal as you want.
189 libevent.
190 264
191 The "signal_ns" variant doesn't add/start the newly created watcher. 265 The "signal_ns" variant doesn't start (activate) the newly created
266 watcher.
192 267
193THE EV::Event CLASS 268 $w->set ($signal)
194 All EV functions creating an event watcher (designated by "my $w =" 269 Reconfigures the watcher, see the constructor above for details. Can
195 above) support the following methods on the returned watcher object: 270 be at any time.
196 271
197 $w->add ($timeout) 272 $w = EV::child $pid, $callback
198 Stops and (re-)starts the event watcher, setting the optional 273 $w = EV::child_ns $pid, $callback
199 timeout to the given value, or clearing the timeout if none is 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
288 $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
297 The process will not block as long as any idle watchers are active,
298 and they will be called repeatedly until stopped.
299
300 The "idle_ns" variant doesn't start (activate) the newly created
301 watcher.
302
303 $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
308 See the EV::check watcher, below, for explanations and an example.
309
310 The "prepare_ns" variant doesn't start (activate) the newly created
311 watcher.
312
313 $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
200 given. 317 invoked.
201 318
202 $w->start 319 This is used to integrate other event-based software into the EV
203 Stops and (re-)starts the event watcher without touching the 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
204 timeout. 323 out):
205 324
206 $w->del 325 our @snmp_watcher;
207 $w->stop
208 Stop the event watcher if it was started.
209 326
210 $current_callback = $w->cb 327 our $snmp_prepare = EV::prepare sub {
211 $old_callback = $w->cb ($new_callback) 328 # do nothing unless active
212 Return the previously set callback and optionally set a new one. 329 $dispatcher->{_event_queue_h}
330 or return;
213 331
214 $current_fh = $w->fh 332 # make the dispatcher handle any outstanding stuff
215 $old_fh = $w->fh ($new_fh)
216 Returns the previously set filehandle and optionally set a new one
217 (also clears the EV::SIGNAL flag when setting a filehandle).
218 333
219 $current_signal = $w->signal 334 # create an IO watcher for each and every socket
220 $old_signal = $w->signal ($new_signal) 335 @snmp_watcher = (
221 Returns the previously set signal number and optionally set a new 336 (map { EV::io $_, EV::READ, sub { } }
222 one (also sets the EV::SIGNAL flag when setting a signal). 337 keys %{ $dispatcher->{_descriptors} }),
338 );
223 339
224 $current_eventmask = $w->events 340 # if there are any timeouts, also create a timer
225 $old_eventmask = $w->events ($new_eventmask) 341 push @snmp_watcher, EV::timer $event->[Net::SNMP::Dispatcher::_TIME] - EV::now, 0, sub { }
226 Returns the previously set event mask and optionally set a new one. 342 if $event->[Net::SNMP::Dispatcher::_ACTIVE];
343 };
227 344
228 $w->timeout ($after, $repeat) 345 The callbacks are irrelevant, the only purpose of those watchers is
229 Resets the timeout (see "EV::timer" for details). 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:
230 349
231 $w->timeout_abs ($at, $interval) 350 our $snmp_check = EV::check sub {
232 Resets the timeout (see "EV::timer_abs" for details). 351 # destroy all watchers
352 @snmp_watcher = ();
233 353
234 $w->priority_set ($priority) 354 # make the dispatcher handle any new stuff
235 Set the priority of the watcher to $priority (0 <= $priority < 355 };
236 $EV::NPRI). 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
361 The "check_ns" variant doesn't start (activate) the newly created
362 watcher.
237 363
238THREADS 364THREADS
239 Threads are not supported by this in any way. Perl pseudo-threads is 365 Threads are not supported by this in any way. Perl pseudo-threads is
240 evil and must die. 366 evil stuff and must die.
241 367
242SEE ALSO 368SEE ALSO
243 L<EV::DNS>, L<event(3)>, L<event.h>, L<evdns.h>.
244 L<EV::AnyEvent>. 369 L<EV::DNS>, L<EV::AnyEvent>.
245 370
246AUTHOR 371AUTHOR
247 Marc Lehmann <schmorp@schmorp.de> 372 Marc Lehmann <schmorp@schmorp.de>
248 http://home.schmorp.de/ 373 http://home.schmorp.de/
249 374

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines