--- EV/README 2007/11/03 16:25:49 1.7 +++ EV/README 2007/11/14 21:25:46 1.10 @@ -11,20 +11,20 @@ warn "is called after 2s"; }; - my $w = EV::timer 2, 1, sub { - warn "is called roughly every 2s (repeat = 1)"; + 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, sub { + 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 get the watcher object and event mask + my ($w, $revents) = @_; # all callbacks receive the watcher and event mask warn "stdin is readable, you entered: ", ; }; @@ -34,20 +34,15 @@ warn "sigquit received\n"; }; - my $w = EV::signal 3, sub { - warn "sigquit received (this is GNU/Linux, right?)\n"; - }; - # CHILD/PID STATUS CHANGES my $w = EV::child 666, sub { my ($w, $revents) = @_; - # my $pid = $w->rpid; my $status = $w->rstatus; }; # MAINLOOP - EV::loop; # loop until EV::loop_done is called + 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 @@ -77,7 +72,7 @@ EV::loop [$flags] Begin checking for events and calling callbacks. It returns when a - callback calls EV::loop_done. + callback calls EV::unloop. The $flags argument can be one of the following: @@ -85,12 +80,12 @@ EV::LOOP_ONESHOT block at most once (wait, but do not loop) EV::LOOP_NONBLOCK do not block at all (fetch/handle events but do not wait) - EV::loop_done [$how] - When called with no arguments or an argument of 1, makes the - innermost call to EV::loop return. + EV::unloop [$how] + When called with no arguments or an argument of EV::UNLOOP_ONE, + makes the innermost call to EV::loop return. - When called with an agrument of 2, all calls to EV::loop will return - as fast as possible. + When called with an argument of EV::UNLOOP_ALL, all calls to + EV::loop will return as fast as possible. WATCHER A watcher is an object that gets created to record your interest in some @@ -149,6 +144,16 @@ $bool = $w->is_active Returns true if the watcher is active, false otherwise. + $current_data = $w->data + $old_data = $w->data ($new_data) + Queries a freely usable data scalar on the watcher and optionally + changes it. This is a way to associate custom data with a watcher: + + my $w = EV::timer 60, 0, sub { + warn $_[0]->data; + }; + $w->data ("print me!"); + $current_cb = $w->cb $old_cb = $w->cb ($new_cb) Queries the callback on the watcher and optionally changes it. You @@ -199,11 +204,13 @@ callback returns. This means that the callback would be called roughly after $after - seconds, and then every $repeat seconds. "Roughly" because the time - of callback processing is not taken into account, so the timer will - slowly drift. If that isn't acceptable, look at EV::periodic. + seconds, and then every $repeat seconds. The timer does his best not + to drift, but it will not invoke the timer more often then once per + event loop iteration, and might drift in other cases. If that isn't + acceptable, look at EV::periodic, which can provide long-term stable + timers. - The timer is based on a monotonic clock, that is if somebody is + The timer is based on a monotonic clock, that is, if somebody is sitting in front of the machine while the timer is running and changes the system clock, the timer will nevertheless run (roughly) the same time. @@ -219,12 +226,13 @@ Similar to the "start" method, but has special semantics for repeating timers: + If the timer is active and non-repeating, it will be stopped. + If the timer is active and repeating, reset the timeout to occur $repeat seconds after now. - If the timer is active and non-repeating, it will be stopped. - - If the timer is in active and repeating, start it. + If the timer is inactive and repeating, start it using the repeat + value. Otherwise do nothing. @@ -233,39 +241,85 @@ and $repeat, and then, in the read/write watcher, run the "again" method on the timeout. - $w = EV::periodic $at, $interval, $callback - $w = EV::periodic_ns $at, $interval, $callback - Similar to EV::timer, but the time is given as an absolute point in - time ($at), plus an optional $interval. - - If the $interval is zero, then the callback will be called at the - time $at if that is in the future, or as soon as possible if it is - in the past. It will not automatically repeat. - - If the $interval is nonzero, then the watcher will always be - scheduled to time out at the next "$at + N * $interval" time. - - This can be used to schedule a callback to run at very regular - intervals, as long as the processing time is less then the interval - (otherwise obviously events will be skipped). - - Another way to think about it (for the mathematically inclined) is - that EV::periodic will try to run the callback at the next possible - time where "$time = $at (mod $interval)", regardless of any time - jumps. - - This periodic timer is based on "wallclock time", that is, if the - clock changes ("ntp", "date -s" etc.), then the timer will - nevertheless run at the specified time. This means it will never - drift (it might jitter, but it will not drift). + $w = EV::periodic $at, $interval, $reschedule_cb, $callback + $w = EV::periodic_ns $at, $interval, $reschedule_cb, $callback + Similar to EV::timer, but is not based on relative timeouts but on + absolute times. Apart from creating "simple" timers that trigger + "at" the specified time, it can also be used for non-drifting + absolute timers and more complex, cron-like, setups that are not + adversely affected by time jumps (i.e. when the system clock is + changed by explicit date -s or other means such as ntpd). It is also + the most complex watcher type in EV. + + It has three distinct "modes": + + * 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) + 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. + + This can be used to create timers that do not drift with respect + to system time: + + my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" }; + + That doesn't mean there will always be 3600 seconds in between + triggers, but only that the the clalback will be called when the + system time shows a full hour (UTC). + + Another way to think about it (for the mathematically inclined) + is that EV::periodic will try to run the callback in this mode + at the next possible time where "$time = $at (mod $interval)", + regardless of any time jumps. + + * 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. + + 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. + + This can be used to create very complex timers, such as a timer + that triggers on each midnight, local time (actually 24 hours + after the last midnight, to keep the example simple. If you know + a way to do it correctly in about the same space (without + requiring elaborate modules), drop me a note :): + + my $daily = EV::periodic 0, 0, sub { + my ($w, $now) = @_; + + use Time::Local (); + my (undef, undef, undef, $d, $m, $y) = localtime $now; + 86400 + Time::Local::timelocal 0, 0, 0, $d, $m, $y + }, sub { + print "it's midnight or likely shortly after, now\n"; + }; The "periodic_ns" variant doesn't start (activate) the newly created watcher. - $w->set ($at, $interval) + $w->set ($at, $interval, $reschedule_cb) Reconfigures the watcher, see the constructor above for details. Can be at any time. + $w->again + Simply stops and starts the watcher again. + $w = EV::signal $signal, $callback $w = EV::signal_ns $signal, $callback Call the callback when $signal is received (the signal can be