ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/README
Revision: 1.13
Committed: Tue Nov 27 16:35:47 2007 UTC (16 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-1_4
Changes since 1.12: +84 -19 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 NAME
2 EV - perl interface to libev, a high performance full-featured event
3 loop
4
5 SYNOPSIS
6 use EV;
7
8 # TIMERS
9
10 my $w = EV::timer 2, 0, sub {
11 warn "is called after 2s";
12 };
13
14 my $w = EV::timer 2, 2, sub {
15 warn "is called roughly every 2s (repeat = 2)";
16 };
17
18 undef $w; # destroy event watcher again
19
20 my $w = EV::periodic 0, 60, 0, sub {
21 warn "is called every minute, on the minute, exactly";
22 };
23
24 # IO
25
26 my $w = EV::io *STDIN, EV::READ, sub {
27 my ($w, $revents) = @_; # all callbacks receive the watcher and event mask
28 warn "stdin is readable, you entered: ", <STDIN>;
29 };
30
31 # SIGNALS
32
33 my $w = EV::signal 'QUIT', sub {
34 warn "sigquit received\n";
35 };
36
37 # CHILD/PID STATUS CHANGES
38
39 my $w = EV::child 666, sub {
40 my ($w, $revents) = @_;
41 my $status = $w->rstatus;
42 };
43
44 # STAT CHANGES
45 my $w = EV::stat "/etc/passwd", 10, sub {
46 my ($w, $revents) = @_;
47 warn $w->path, " has changed somehow.\n";
48 };
49
50 # MAINLOOP
51 EV::loop; # loop until EV::unloop is called or all watchers stop
52 EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled
53 EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block
54
55 DESCRIPTION
56 This module provides an interface to libev
57 (<http://software.schmorp.de/pkg/libev.html>). While the documentation
58 below is comprehensive, one might also consult the documentation of
59 libev itself (<http://cvs.schmorp.de/libev/ev.html>) for more subtle
60 details on watcher semantics or some discussion on the available
61 backends, or how to force a specific backend with "LIBEV_FLAGS".
62
63 BASIC INTERFACE
64 $EV::DIED
65 Must contain a reference to a function that is called when a
66 callback throws an exception (with $@ containing thr error). The
67 default prints an informative message and continues.
68
69 If this callback throws an exception it will be silently ignored.
70
71 $time = EV::time
72 Returns the current time in (fractional) seconds since the epoch.
73
74 $time = EV::now
75 Returns the time the last event loop iteration has been started.
76 This is the time that (relative) timers are based on, and refering
77 to it is usually faster then calling EV::time.
78
79 $method = EV::method
80 Returns an integer describing the backend used by libev
81 (EV::METHOD_SELECT or EV::METHOD_EPOLL).
82
83 EV::loop [$flags]
84 Begin checking for events and calling callbacks. It returns when a
85 callback calls EV::unloop.
86
87 The $flags argument can be one of the following:
88
89 0 as above
90 EV::LOOP_ONESHOT block at most once (wait, but do not loop)
91 EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait)
92
93 EV::unloop [$how]
94 When called with no arguments or an argument of EV::UNLOOP_ONE,
95 makes the innermost call to EV::loop return.
96
97 When called with an argument of EV::UNLOOP_ALL, all calls to
98 EV::loop will return as fast as possible.
99
100 EV::once $fh_or_undef, $events, $timeout, $cb->($revents)
101 This function rolls together an I/O and a timer watcher for a single
102 one-shot event without the need for managing a watcher object.
103
104 If $fh_or_undef is a filehandle or file descriptor, then $events
105 must be a bitset containing either "EV::READ", "EV::WRITE" or
106 "EV::READ | EV::WRITE", indicating the type of I/O event you want to
107 wait for. If you do not want to wait for some I/O event, specify
108 "undef" for $fh_or_undef and 0 for $events).
109
110 If timeout is "undef" or negative, then there will be no timeout.
111 Otherwise a EV::timer with this value will be started.
112
113 When an error occurs or either the timeout or I/O watcher triggers,
114 then the callback will be called with the received event set (in
115 general you can expect it to be a combination of "EV:ERROR",
116 "EV::READ", "EV::WRITE" and "EV::TIMEOUT").
117
118 EV::once doesn't return anything: the watchers stay active till
119 either of them triggers, then they will be stopped and freed, and
120 the callback invoked.
121
122 WATCHER OBJECTS
123 A watcher is an object that gets created to record your interest in some
124 event. For instance, if you want to wait for STDIN to become readable,
125 you would create an EV::io watcher for that:
126
127 my $watcher = EV::io *STDIN, EV::READ, sub {
128 my ($watcher, $revents) = @_;
129 warn "yeah, STDIN should not be readable without blocking!\n"
130 };
131
132 All watchers can be active (waiting for events) or inactive (paused).
133 Only active watchers will have their callbacks invoked. All callbacks
134 will be called with at least two arguments: the watcher and a bitmask of
135 received events.
136
137 Each watcher type has its associated bit in revents, so you can use the
138 same callback for multiple watchers. The event mask is named after the
139 type, i..e. EV::child sets EV::CHILD, EV::prepare sets EV::PREPARE,
140 EV::periodic sets EV::PERIODIC and so on, with the exception of IO
141 events (which can set both EV::READ and EV::WRITE bits), and EV::timer
142 (which uses EV::TIMEOUT).
143
144 In the rare case where one wants to create a watcher but not start it at
145 the same time, each constructor has a variant with a trailing "_ns" in
146 its name, e.g. EV::io has a non-starting variant EV::io_ns and so on.
147
148 Please note that a watcher will automatically be stopped when the
149 watcher object is destroyed, so you *need* to keep the watcher objects
150 returned by the constructors.
151
152 Also, all methods changing some aspect of a watcher (->set, ->priority,
153 ->fh and so on) automatically stop and start it again if it is active,
154 which means pending events get lost.
155
156 COMMON WATCHER METHODS
157 This section lists methods common to all watchers.
158
159 $w->start
160 Starts a watcher if it isn't active already. Does nothing to an
161 already active watcher. By default, all watchers start out in the
162 active state (see the description of the "_ns" variants if you need
163 stopped watchers).
164
165 $w->stop
166 Stop a watcher if it is active. Also clear any pending events
167 (events that have been received but that didn't yet result in a
168 callback invocation), regardless of wether the watcher was active or
169 not.
170
171 $bool = $w->is_active
172 Returns true if the watcher is active, false otherwise.
173
174 $current_data = $w->data
175 $old_data = $w->data ($new_data)
176 Queries a freely usable data scalar on the watcher and optionally
177 changes it. This is a way to associate custom data with a watcher:
178
179 my $w = EV::timer 60, 0, sub {
180 warn $_[0]->data;
181 };
182 $w->data ("print me!");
183
184 $current_cb = $w->cb
185 $old_cb = $w->cb ($new_cb)
186 Queries the callback on the watcher and optionally changes it. You
187 can do this at any time without the watcher restarting.
188
189 $current_priority = $w->priority
190 $old_priority = $w->priority ($new_priority)
191 Queries the priority on the watcher and optionally changes it.
192 Pending watchers with higher priority will be invoked first. The
193 valid range of priorities lies between EV::MAXPRI (default 2) and
194 EV::MINPRI (default -2). If the priority is outside this range it
195 will automatically be normalised to the nearest valid priority.
196
197 The default priority of any newly-created watcher is 0.
198
199 Note that the priority semantics have not yet been fleshed out and
200 are subject to almost certain change.
201
202 $w->trigger ($revents)
203 Call the callback *now* with the given event mask.
204
205 $previous_state = $w->keepalive ($bool)
206 Normally, "EV::loop" will return when there are no active watchers
207 (which is a "deadlock" because no progress can be made anymore).
208 This is convinient because it allows you to start your watchers (and
209 your jobs), call "EV::loop" once and when it returns you know that
210 all your jobs are finished (or they forgot to register some watchers
211 for their task :).
212
213 Sometimes, however, this gets in your way, for example when you the
214 module that calls "EV::loop" (usually the main program) is not the
215 same module as a long-living watcher (for example a DNS client
216 module written by somebody else even). Then you might want any
217 outstanding requests to be handled, but you would not want to keep
218 "EV::loop" from returning just because you happen to have this
219 long-running UDP port watcher.
220
221 In this case you can clear the keepalive status, which means that
222 even though your watcher is active, it won't keep "EV::loop" from
223 returning.
224
225 The initial value for keepalive is true (enabled), and you cna
226 change it any time.
227
228 Example: Register an IO watcher for some UDP socket but do not keep
229 the event loop from running just because of that watcher.
230
231 my $udp_socket = ...
232 my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... };
233 $udp_watcher->keepalive (0);
234
235 WATCHER TYPES
236 Each of the following subsections describes a single watcher type.
237
238 IO WATCHERS - is this file descriptor readable or writable?
239 $w = EV::io $fileno_or_fh, $eventmask, $callback
240 $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
241 As long as the returned watcher object is alive, call the $callback
242 when at least one of events specified in $eventmask occurs.
243
244 The $eventmask can be one or more of these constants ORed together:
245
246 EV::READ wait until read() wouldn't block anymore
247 EV::WRITE wait until write() wouldn't block anymore
248
249 The "io_ns" variant doesn't start (activate) the newly created
250 watcher.
251
252 $w->set ($fileno_or_fh, $eventmask)
253 Reconfigures the watcher, see the constructor above for details. Can
254 be called at any time.
255
256 $current_fh = $w->fh
257 $old_fh = $w->fh ($new_fh)
258 Returns the previously set filehandle and optionally set a new one.
259
260 $current_eventmask = $w->events
261 $old_eventmask = $w->events ($new_eventmask)
262 Returns the previously set event mask and optionally set a new one.
263
264 TIMER WATCHERS - relative and optionally repeating timeouts
265 $w = EV::timer $after, $repeat, $callback
266 $w = EV::timer_ns $after, $repeat, $callback
267 Calls the callback after $after seconds (which may be fractional).
268 If $repeat is non-zero, the timer will be restarted (with the
269 $repeat value as $after) after the callback returns.
270
271 This means that the callback would be called roughly after $after
272 seconds, and then every $repeat seconds. The timer does his best not
273 to drift, but it will not invoke the timer more often then once per
274 event loop iteration, and might drift in other cases. If that isn't
275 acceptable, look at EV::periodic, which can provide long-term stable
276 timers.
277
278 The timer is based on a monotonic clock, that is, if somebody is
279 sitting in front of the machine while the timer is running and
280 changes the system clock, the timer will nevertheless run (roughly)
281 the same time.
282
283 The "timer_ns" variant doesn't start (activate) the newly created
284 watcher.
285
286 $w->set ($after, $repeat)
287 Reconfigures the watcher, see the constructor above for details. Can
288 be called at any time.
289
290 $w->again
291 Similar to the "start" method, but has special semantics for
292 repeating timers:
293
294 If the timer is active and non-repeating, it will be stopped.
295
296 If the timer is active and repeating, reset the timeout to occur
297 $repeat seconds after now.
298
299 If the timer is inactive and repeating, start it using the repeat
300 value.
301
302 Otherwise do nothing.
303
304 This behaviour is useful when you have a timeout for some IO
305 operation. You create a timer object with the same value for $after
306 and $repeat, and then, in the read/write watcher, run the "again"
307 method on the timeout.
308
309 PERIODIC WATCHERS - to cron or not to cron?
310 $w = EV::periodic $at, $interval, $reschedule_cb, $callback
311 $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback
312 Similar to EV::timer, but is not based on relative timeouts but on
313 absolute times. Apart from creating "simple" timers that trigger
314 "at" the specified time, it can also be used for non-drifting
315 absolute timers and more complex, cron-like, setups that are not
316 adversely affected by time jumps (i.e. when the system clock is
317 changed by explicit date -s or other means such as ntpd). It is also
318 the most complex watcher type in EV.
319
320 It has three distinct "modes":
321
322 * absolute timer ($interval = $reschedule_cb = 0)
323 This time simply fires at the wallclock time $at and doesn't
324 repeat. It will not adjust when a time jump occurs, that is, if
325 it is to be run at January 1st 2011 then it will run when the
326 system time reaches or surpasses this time.
327
328 * non-repeating interval timer ($interval > 0, $reschedule_cb = 0)
329 In this mode the watcher will always be scheduled to time out at
330 the next "$at + N * $interval" time (for some integer N) and
331 then repeat, regardless of any time jumps.
332
333 This can be used to create timers that do not drift with respect
334 to system time:
335
336 my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" };
337
338 That doesn't mean there will always be 3600 seconds in between
339 triggers, but only that the the clalback will be called when the
340 system time shows a full hour (UTC).
341
342 Another way to think about it (for the mathematically inclined)
343 is that EV::periodic will try to run the callback in this mode
344 at the next possible time where "$time = $at (mod $interval)",
345 regardless of any time jumps.
346
347 * manual reschedule mode ($reschedule_cb = coderef)
348 In this mode $interval and $at are both being ignored. Instead,
349 each time the periodic watcher gets scheduled, the reschedule
350 callback ($reschedule_cb) will be called with the watcher as
351 first, and the current time as second argument.
352
353 *This callback MUST NOT stop or destroy this or any other
354 periodic watcher, ever*. If you need to stop it, return 1e30 and
355 stop it afterwards.
356
357 It must return the next time to trigger, based on the passed
358 time value (that is, the lowest time value larger than to the
359 second argument). It will usually be called just before the
360 callback will be triggered, but might be called at other times,
361 too.
362
363 This can be used to create very complex timers, such as a timer
364 that triggers on each midnight, local time (actually 24 hours
365 after the last midnight, to keep the example simple. If you know
366 a way to do it correctly in about the same space (without
367 requiring elaborate modules), drop me a note :):
368
369 my $daily = EV::periodic 0, 0, sub {
370 my ($w, $now) = @_;
371
372 use Time::Local ();
373 my (undef, undef, undef, $d, $m, $y) = localtime $now;
374 86400 + Time::Local::timelocal 0, 0, 0, $d, $m, $y
375 }, sub {
376 print "it's midnight or likely shortly after, now\n";
377 };
378
379 The "periodic_ns" variant doesn't start (activate) the newly created
380 watcher.
381
382 $w->set ($at, $interval, $reschedule_cb)
383 Reconfigures the watcher, see the constructor above for details. Can
384 be called at any time.
385
386 $w->again
387 Simply stops and starts the watcher again.
388
389 SIGNAL WATCHERS - signal me when a signal gets signalled!
390 $w = EV::signal $signal, $callback
391 $w = EV::signal_ns $signal, $callback
392 Call the callback when $signal is received (the signal can be
393 specified by number or by name, just as with "kill" or %SIG).
394
395 EV will grab the signal for the process (the kernel only allows one
396 component to receive a signal at a time) when you start a signal
397 watcher, and removes it again when you stop it. Perl does the same
398 when you add/remove callbacks to %SIG, so watch out.
399
400 You can have as many signal watchers per signal as you want.
401
402 The "signal_ns" variant doesn't start (activate) the newly created
403 watcher.
404
405 $w->set ($signal)
406 Reconfigures the watcher, see the constructor above for details. Can
407 be called at any time.
408
409 $current_signum = $w->signal
410 $old_signum = $w->signal ($new_signal)
411 Returns the previously set signal (always as a number not name) and
412 optionally set a new one.
413
414 CHILD WATCHERS - watch out for process status changes
415 $w = EV::child $pid, $callback
416 $w = EV::child_ns $pid, $callback
417 Call the callback when a status change for pid $pid (or any pid if
418 $pid is 0) has been received. More precisely: when the process
419 receives a "SIGCHLD", EV will fetch the outstanding exit/wait status
420 for all changed/zombie children and call the callback.
421
422 It is valid (and fully supported) to install a child watcher after a
423 child has exited but before the event loop has started its next
424 iteration (for example, first you "fork", then the new child process
425 might exit, and only then do you install a child watcher in the
426 parent for the new pid).
427
428 You can access both exit (or tracing) status and pid by using the
429 "rstatus" and "rpid" methods on the watcher object.
430
431 You can have as many pid watchers per pid as you want, they will all
432 be called.
433
434 The "child_ns" variant doesn't start (activate) the newly created
435 watcher.
436
437 $w->set ($pid)
438 Reconfigures the watcher, see the constructor above for details. Can
439 be called at any time.
440
441 $current_pid = $w->pid
442 $old_pid = $w->pid ($new_pid)
443 Returns the previously set process id and optionally set a new one.
444
445 $exit_status = $w->rstatus
446 Return the exit/wait status (as returned by waitpid, see the waitpid
447 entry in perlfunc).
448
449 $pid = $w->rpid
450 Return the pid of the awaited child (useful when you have installed
451 a watcher for all pids).
452
453 STAT WATCHERS - did the file attributes just change?
454 $w = EV::stat $path, $interval, $callback
455 $w = EV::stat_ns $path, $interval, $callback
456 Call the callback when a file status change has been detected on
457 $path. The $path does not need to exist, changing from "path exists"
458 to "path does not exist" is a status change like any other.
459
460 The $interval is a recommended polling interval for systems where
461 OS-supported change notifications don't exist or are not supported.
462 If you use 0 then an unspecified default is used (which is highly
463 recommended!), which is to be expected to be around five seconds
464 usually.
465
466 This watcher type is not meant for massive numbers of stat watchers,
467 as even with OS-supported change notifications, this can be
468 resource-intensive.
469
470 The "stat_ns" variant doesn't start (activate) the newly created
471 watcher.
472
473 $w->set ($path, $interval)
474 Reconfigures the watcher, see the constructor above for details. Can
475 be called at any time.
476
477 $current_path = $w->path
478 $old_path = $w->path ($new_path)
479 Returns the previously set path and optionally set a new one.
480
481 $current_interval = $w->interval
482 $old_interval = $w->interval ($new_interval)
483 Returns the previously set interval and optionally set a new one.
484 Can be used to query the actual interval used.
485
486 IDLE WATCHERS - when you've got nothing better to do...
487 $w = EV::idle $callback
488 $w = EV::idle_ns $callback
489 Call the callback when there are no pending io, timer/periodic,
490 signal or child events, i.e. when the process is idle.
491
492 The process will not block as long as any idle watchers are active,
493 and they will be called repeatedly until stopped.
494
495 The "idle_ns" variant doesn't start (activate) the newly created
496 watcher.
497
498 PREPARE WATCHERS - customise your event loop!
499 $w = EV::prepare $callback
500 $w = EV::prepare_ns $callback
501 Call the callback just before the process would block. You can still
502 create/modify any watchers at this point.
503
504 See the EV::check watcher, below, for explanations and an example.
505
506 The "prepare_ns" variant doesn't start (activate) the newly created
507 watcher.
508
509 CHECK WATCHERS - customise your event loop even more!
510 $w = EV::check $callback
511 $w = EV::check_ns $callback
512 Call the callback just after the process wakes up again (after it
513 has gathered events), but before any other callbacks have been
514 invoked.
515
516 This is used to integrate other event-based software into the EV
517 mainloop: You register a prepare callback and in there, you create
518 io and timer watchers as required by the other software. Here is a
519 real-world example of integrating Net::SNMP (with some details left
520 out):
521
522 our @snmp_watcher;
523
524 our $snmp_prepare = EV::prepare sub {
525 # do nothing unless active
526 $dispatcher->{_event_queue_h}
527 or return;
528
529 # make the dispatcher handle any outstanding stuff
530 ... not shown
531
532 # create an IO watcher for each and every socket
533 @snmp_watcher = (
534 (map { EV::io $_, EV::READ, sub { } }
535 keys %{ $dispatcher->{_descriptors} }),
536
537 EV::timer +($event->[Net::SNMP::Dispatcher::_ACTIVE]
538 ? $event->[Net::SNMP::Dispatcher::_TIME] - EV::now : 0),
539 0, sub { },
540 );
541 };
542
543 The callbacks are irrelevant (and are not even being called), the
544 only purpose of those watchers is to wake up the process as soon as
545 one of those events occurs (socket readable, or timer timed out).
546 The corresponding EV::check watcher will then clean up:
547
548 our $snmp_check = EV::check sub {
549 # destroy all watchers
550 @snmp_watcher = ();
551
552 # make the dispatcher handle any new stuff
553 ... not shown
554 };
555
556 The callbacks of the created watchers will not be called as the
557 watchers are destroyed before this cna happen (remember EV::check
558 gets called first).
559
560 The "check_ns" variant doesn't start (activate) the newly created
561 watcher.
562
563 FORK WATCHERS - the audacity to resume the event loop after a fork
564 Fork watchers are called when a "fork ()" was detected. The invocation
565 is done before the event loop blocks next and before "check" watchers
566 are being called, and only in the child after the fork.
567
568 $w = EV::fork $callback
569 $w = EV::fork_ns $callback
570 Call the callback before the event loop is resumed in the child
571 process after a fork.
572
573 The "fork_ns" variant doesn't start (activate) the newly created
574 watcher.
575
576 THREADS
577 Threads are not supported by this module in any way. Perl pseudo-threads
578 is evil stuff and must die. As soon as Perl gains real threads I will
579 work on thread support for it.
580
581 FORK
582 Most of the "improved" event delivering mechanisms of modern operating
583 systems have quite a few problems with fork(2) (to put it bluntly: it is
584 not supported and usually destructive). Libev makes it possible to work
585 around this by having a function that recreates the kernel state after
586 fork in the child.
587
588 On non-win32 platforms, this module requires the pthread_atfork
589 functionality to do this automatically for you. This function is quite
590 buggy on most BSDs, though, so YMMV. The overhead for this is quite
591 negligible, because everything the function currently does is set a flag
592 that is checked only when the event loop gets used the next time, so
593 when you do fork but not use EV, the overhead is minimal.
594
595 On win32, there is no notion of fork so all this doesn't apply, of
596 course.
597
598 SEE ALSO
599 EV::DNS.
600
601 AUTHOR
602 Marc Lehmann <schmorp@schmorp.de>
603 http://home.schmorp.de/
604