--- cvsroot/EV/README 2007/12/22 16:48:33 1.21 +++ cvsroot/EV/README 2008/10/23 04:56:49 1.30 @@ -3,63 +3,73 @@ loop SYNOPSIS - use EV; - - # TIMERS - - my $w = EV::timer 2, 0, sub { - warn "is called after 2s"; - }; - - my $w = EV::timer 2, 2, sub { - warn "is called roughly every 2s (repeat = 2)"; - }; - - undef $w; # destroy event watcher again - - my $w = EV::periodic 0, 60, 0, sub { - warn "is called every minute, on the minute, exactly"; - }; - - # IO - - my $w = EV::io *STDIN, EV::READ, sub { - my ($w, $revents) = @_; # all callbacks receive the watcher and event mask - warn "stdin is readable, you entered: ", ; - }; - - # SIGNALS - - my $w = EV::signal 'QUIT', sub { - warn "sigquit received\n"; - }; - - # CHILD/PID STATUS CHANGES - - my $w = EV::child 666, sub { - my ($w, $revents) = @_; - my $status = $w->rstatus; - }; - - # STAT CHANGES - my $w = EV::stat "/etc/passwd", 10, sub { - my ($w, $revents) = @_; - warn $w->path, " has changed somehow.\n"; - }; - - # MAINLOOP - EV::loop; # loop until EV::unloop is called or all watchers stop - EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled - EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block + use EV; + + # TIMERS + + my $w = EV::timer 2, 0, sub { + warn "is called after 2s"; + }; + + my $w = EV::timer 2, 2, sub { + warn "is called roughly every 2s (repeat = 2)"; + }; + + undef $w; # destroy event watcher again + + my $w = EV::periodic 0, 60, 0, sub { + warn "is called every minute, on the minute, exactly"; + }; + + # IO + + my $w = EV::io *STDIN, EV::READ, sub { + my ($w, $revents) = @_; # all callbacks receive the watcher and event mask + warn "stdin is readable, you entered: ", ; + }; + + # SIGNALS + + my $w = EV::signal 'QUIT', sub { + warn "sigquit received\n"; + }; + + # CHILD/PID STATUS CHANGES + + my $w = EV::child 666, 0, sub { + my ($w, $revents) = @_; + my $status = $w->rstatus; + }; + + # STAT CHANGES + my $w = EV::stat "/etc/passwd", 10, sub { + my ($w, $revents) = @_; + warn $w->path, " has changed somehow.\n"; + }; + + # MAINLOOP + EV::loop; # loop until EV::unloop is called or all watchers stop + EV::loop EV::LOOP_ONESHOT; # block until at least one event could be handled + EV::loop EV::LOOP_NONBLOCK; # try to handle same events, but do not block DESCRIPTION This module provides an interface to libev (). While the documentation below is comprehensive, one might also consult the documentation of - libev itself () for more subtle - details on watcher semantics or some discussion on the available - backends, or how to force a specific backend with "LIBEV_FLAGS", or just - about in any case because it has much more detailed information. + libev itself ( or + perldoc EV::libev) for more subtle details on watcher semantics or some + discussion on the available backends, or how to force a specific backend + with "LIBEV_FLAGS", or just about in any case because it has much more + detailed information. + + This module is very fast and scalable. It is actually so fast that you + can use it through the AnyEvent module, stay portable to other event + loops (if you don't rely on any watcher types not available through it) + and still be faster than with any other event loop currently supported + in Perl. + + MODULE EXPORTS + This module does not export any symbols. EVENT LOOPS EV supports multiple event loops: There is a single "default event loop" @@ -76,26 +86,45 @@ For specific programs you can create additional event loops dynamically. + If you want to take avdantage of kqueue (which often works properly for + sockets only) even though the default loop doesn't enable it, you can + *embed* a kqueue loop into the default loop: running the default loop + will then also service the kqueue loop to some extent. See the example + in the section about embed watchers for an example on how to achieve + that. + $loop = new EV::loop [$flags] Create a new event loop as per the specified flags. Please refer to the "ev_loop_new ()" function description in the libev documentation () for more info. + ONS>, or locally-installed as EV::libev manpage) for more info. The loop will automatically be destroyed when it is no longer referenced by any watcher and the loop object goes out of scope. - Using "EV::FLAG_FORKCHECK" is recommended, as only the default event - loop is protected by this module. + If you are not embedding the loop, then Using "EV::FLAG_FORKCHECK" + is recommended, as only the default event loop is protected by this + module. If you *are* embedding this loop in the default loop, this + is not necessary, as "EV::embed" automatically does the right thing + on fork. $loop->loop_fork Must be called after a fork in the child, before entering or continuing the event loop. An alternative is to use - "EV::FLAG_FORKCHECK" which calls this fucntion automatically, at + "EV::FLAG_FORKCHECK" which calls this function automatically, at some performance loss (refer to the libev documentation). + $loop->loop_verify + Calls "ev_verify" to make internal consistency checks (for debugging + libev) and abort the program if any data structures were found to be + corrupted. + $loop = EV::default_loop [$flags] - Return the default loop (which is a singleton object). + Return the default loop (which is a singleton object). Since this + module already creates the default loop with default flags, + specifying flags here will not have any effect unless you destroy + the default loop first, which isn't supported. So in short: don't do + it, and if you break it, you get to keep the pieces. BASIC INTERFACE $EV::DIED @@ -196,17 +225,18 @@ for I/O events and the minimum wait interval for timer events. See the libev documentation at for a more detailed discussion. + ROLLING_THE_EVENT_LOOP> (locally installed as EV::libev) for a more + detailed discussion. WATCHER OBJECTS A watcher is an object that gets created to record your interest in some event. For instance, if you want to wait for STDIN to become readable, you would create an EV::io watcher for that: - my $watcher = EV::io *STDIN, EV::READ, sub { - my ($watcher, $revents) = @_; - warn "yeah, STDIN should now be readable without blocking!\n" - }; + my $watcher = EV::io *STDIN, EV::READ, sub { + my ($watcher, $revents) = @_; + warn "yeah, STDIN should now be readable without blocking!\n" + }; All watchers can be active (waiting for events) or inactive (paused). Only active watchers will have their callbacks invoked. All callbacks @@ -318,7 +348,7 @@ my $udp_socket = ... my $udp_watcher = EV::io $udp_socket, EV::READ, sub { ... }; - $1000udp_watcher->keepalive (0); + $udp_watcher->keepalive (0); $loop = $w->loop Return the loop that this watcher is attached to. @@ -416,13 +446,15 @@ It has three distinct "modes": - * absolute timer ($interval = $reschedule_cb = 0) + * absolute timer ($interval = $reschedule_cb = 0) + This time simply fires at the wallclock time $at and doesn't repeat. It will not adjust when a time jump occurs, that is, if it is to be run at January 1st 2011 then it will run when the system time reaches or surpasses this time. - * non-repeating interval timer ($interval > 0, $reschedule_cb = 0) + * repeating interval timer ($interval > 0, $reschedule_cb = 0) + In this mode the watcher will always be scheduled to time out at the next "$at + N * $interval" time (for some integer N) and then repeat, regardless of any time jumps. @@ -441,21 +473,24 @@ at the next possible time where "$time = $at (mod $interval)", regardless of any time jumps. - * manual reschedule mode ($reschedule_cb = coderef) + * manual reschedule mode ($reschedule_cb = coderef) + In this mode $interval and $at are both being ignored. Instead, each time the periodic watcher gets scheduled, the reschedule callback ($reschedule_cb) will be called with the watcher as first, and the current time as second argument. *This callback MUST NOT stop or destroy this or any other - periodic watcher, ever*. If you need to stop it, return 1e30 and - stop it afterwards. + periodic watcher, ever, and MUST NOT call any event loop + functions or methods*. If you need to stop it, return 1e30 and + stop it afterwards. You may create and start a "EV::prepare" + watcher for this task. It must return the next time to trigger, based on the passed - time value (that is, the lowest time value larger than to the - second argument). It will usually be called just before the - callback will be triggered, but might be called at other times, - too. + time value (that is, the lowest time value larger than or equal + to to the second argument). It will usually be called just + before the callback will be triggered, but might be called at + other times, too. This can be used to create very complex timers, such as a timer that triggers on each midnight, local time (actually 24 hours @@ -512,14 +547,17 @@ optionally set a new one. CHILD WATCHERS - watch out for process status changes - $w = EV::child $pid, $callback - $w = EV::child_ns $pid, $callback - $w = $loop->child ($pid, $callback) - $w = $loop->child_ns ($pid, $callback) + $w = EV::child $pid, $trace, $callback + $w = EV::child_ns $pid, $trace, $callback + $w = $loop->child ($pid, $trace, $callback) + $w = $loop->child_ns ($pid, $trace, $callback) Call the callback when a status change for pid $pid (or any pid if - $pid is 0) has been received. More precisely: when the process - receives a "SIGCHLD", EV will fetch the outstanding exit/wait status - for all changed/zombie children and call the callback. + $pid is 0) has been received (a status change happens when the + process terminates or is killed, or, when trace is true, + additionally when it is stopped or continued). More precisely: when + the process receives a "SIGCHLD", EV will fetch the outstanding + exit/wait status for all changed/zombie children and call the + callback. It is valid (and fully supported) to install a child watcher after a child has exited but before the event loop has started its next @@ -536,12 +574,11 @@ The "child_ns" variant doesn't start (activate) the newly created watcher. - $w->set ($pid) + $w->set ($pid, $trace) Reconfigures the watcher, see the constructor above for details. Can be called at any time. $current_pid = $w->pid - $old_pid = $w->pid ($new_pid) Returns the previously set process id and optionally set a new one. $exit_status = $w->rstatus @@ -742,37 +779,51 @@ See the libev documentation at for more details. + _when_one_backend_> (locally installed as EV::libev) for more details. In short, this watcher is most useful on BSD systems without working kqueue to still be able to handle a large number of sockets: - my $socket_loop; - - # check wether we use SELECT or POLL _and_ KQUEUE is supported - if ( - (EV::backend & (EV::BACKEND_POLL | EV::BACKEND_SELECT)) - && (EV::supported_backends & EV::embeddable_backends & EV::BACKEND_KQUEUE) - ) { - # use kqueue for sockets - $socket_loop = new EV::Loop EV::BACKEND_KQUEUE | EV::FLAG_NOENV; - } - - # use the default loop otherwise - $socket_loop ||= EV::default_loop; - - $w = EV::embed $otherloop, $callback - $w = EV::embed_ns $otherloop, $callback - $w = $loop->embed ($otherloop, $callback) - $w = $loop->embed_ns ($otherloop, $callback) + my $socket_loop; + + # check wether we use SELECT or POLL _and_ KQUEUE is supported + if ( + (EV::backend & (EV::BACKEND_POLL | EV::BACKEND_SELECT)) + && (EV::supported_backends & EV::embeddable_backends & EV::BACKEND_KQUEUE) + ) { + # use kqueue for sockets + $socket_loop = new EV::Loop EV::BACKEND_KQUEUE | EV::FLAG_NOENV; + } + + # use the default loop otherwise + $socket_loop ||= EV::default_loop; + + $w = EV::embed $otherloop[, $callback] + $w = EV::embed_ns $otherloop[, $callback] + $w = $loop->embed ($otherloop[, $callback]) + $w = $loop->embed_ns ($otherloop[, $callback]) Call the callback when the embedded event loop ($otherloop) has any - I/O activity. The $callback should alwas be specified as "undef" in - this version of EV, which means the embedded event loop will be - managed automatically. + I/O activity. The $callback is optional: if it is missing, then the + embedded event loop will be managed automatically (which is + recommended), otherwise you have to invoke "sweep" yourself. The "embed_ns" variant doesn't start (activate) the newly created watcher. + ASYNC WATCHERS - how to wake up another event loop + Async watchers are provided by EV, but have little use in perl directly, + as perl neither supports threads nor direct access to signal handlers or + other contexts where they could be of value. + + It is, however, possible to use them from the XS level. + + Please see the libev documentation for further details. + + $w = EV::async $callback + $w = EV::async_ns $callback + $w->send + $bool = $w->async_pending + PERL SIGNALS While Perl signal handling (%SIG) is not affected by EV, the behaviour with EV is as the same as any other C library: Perl-signals will only be @@ -816,9 +867,10 @@ SEE ALSO EV::ADNS (asynchronous DNS), Glib::EV (makes Glib/Gtk2 use EV as event loop), EV::Glib (embed Glib into EV), Coro::EV (efficient coroutines - with EV), Net::SNMP::EV (asynchronous SNMP). + with EV), Net::SNMP::EV (asynchronous SNMP), AnyEvent for event-loop + agnostic and portable event driven programming. AUTHOR - Marc Lehmann - http://home.schmorp.de/ + Marc Lehmann + http://home.schmorp.de/