… | |
… | |
47 | my ($w, $revents) = @_; |
47 | my ($w, $revents) = @_; |
48 | warn $w->path, " has changed somehow.\n"; |
48 | warn $w->path, " has changed somehow.\n"; |
49 | }; |
49 | }; |
50 | |
50 | |
51 | # MAINLOOP |
51 | # MAINLOOP |
52 | EV::run; # loop until EV::unloop is called or all watchers stop |
52 | EV::run; # loop until EV::break is called or all watchers stop |
53 | EV::run EV::RUN_ONCE; # block until at least one event could be handled |
53 | EV::run EV::RUN_ONCE; # block until at least one event could be handled |
54 | EV::run EV::RUN_NOWAIT; # try to handle same events, but do not block |
54 | EV::run EV::RUN_NOWAIT; # try to handle same events, but do not block |
55 | |
55 | |
56 | =head1 BEFORE YOU START USING THIS MODULE |
56 | =head1 BEFORE YOU START USING THIS MODULE |
57 | |
57 | |
… | |
… | |
119 | package EV; |
119 | package EV; |
120 | |
120 | |
121 | use common::sense; |
121 | use common::sense; |
122 | |
122 | |
123 | BEGIN { |
123 | BEGIN { |
124 | our $VERSION = '4.15'; |
124 | our $VERSION = 4.27; |
125 | use XSLoader; |
125 | use XSLoader; |
126 | local $^W = 0; # avoid spurious warning |
126 | local $^W = 0; # avoid spurious warning |
127 | XSLoader::load "EV", $VERSION; |
127 | XSLoader::load "EV", $VERSION; |
128 | } |
128 | } |
129 | |
129 | |
… | |
… | |
248 | |
248 | |
249 | =item $loop->now_update |
249 | =item $loop->now_update |
250 | |
250 | |
251 | Establishes the current time by querying the kernel, updating the time |
251 | Establishes the current time by querying the kernel, updating the time |
252 | returned by C<EV::now> in the progress. This is a costly operation and |
252 | returned by C<EV::now> in the progress. This is a costly operation and |
253 | is usually done automatically within C<EV::loop>. |
253 | is usually done automatically within C<EV::run>. |
254 | |
254 | |
255 | This function is rarely useful, but when some event callback runs for a |
255 | This function is rarely useful, but when some event callback runs for a |
256 | very long time without entering the event loop, updating libev's idea of |
256 | very long time without entering the event loop, updating libev's idea of |
257 | the current time is a good idea. |
257 | the current time is a good idea. |
258 | |
258 | |
… | |
… | |
296 | =item $active = EV::run [$flags] |
296 | =item $active = EV::run [$flags] |
297 | |
297 | |
298 | =item $active = $loop->run ([$flags]) |
298 | =item $active = $loop->run ([$flags]) |
299 | |
299 | |
300 | Begin checking for events and calling callbacks. It returns when a |
300 | Begin checking for events and calling callbacks. It returns when a |
301 | callback calls EV::unloop or the flasg are nonzero (in which case the |
301 | callback calls EV::break or the flags are nonzero (in which case the |
302 | return value is true) or when there are no active watchers which reference |
302 | return value is true) or when there are no active watchers which reference |
303 | the loop (keepalive is true), in which case the return value will be |
303 | the loop (keepalive is true), in which case the return value will be |
304 | false. The returnv alue can generally be interpreted as "if true, there is |
304 | false. The return value can generally be interpreted as "if true, there is |
305 | more work left to do". |
305 | more work left to do". |
306 | |
306 | |
307 | The $flags argument can be one of the following: |
307 | The $flags argument can be one of the following: |
308 | |
308 | |
309 | 0 as above |
309 | 0 as above |
… | |
… | |
313 | =item EV::break [$how] |
313 | =item EV::break [$how] |
314 | |
314 | |
315 | =item $loop->break ([$how]) |
315 | =item $loop->break ([$how]) |
316 | |
316 | |
317 | When called with no arguments or an argument of EV::BREAK_ONE, makes the |
317 | When called with no arguments or an argument of EV::BREAK_ONE, makes the |
318 | innermost call to EV::loop return. |
318 | innermost call to EV::run return. |
319 | |
319 | |
320 | When called with an argument of EV::BREAK_ALL, all calls to EV::loop will |
320 | When called with an argument of EV::BREAK_ALL, all calls to EV::run will |
321 | return as fast as possible. |
321 | return as fast as possible. |
322 | |
322 | |
323 | When called with an argument of EV::BREAK_CANCEL, any pending break will |
323 | When called with an argument of EV::BREAK_CANCEL, any pending break will |
324 | be cancelled. |
324 | be cancelled. |
325 | |
325 | |
… | |
… | |
342 | | EV::WRITE>, indicating the type of I/O event you want to wait for. If |
342 | | EV::WRITE>, indicating the type of I/O event you want to wait for. If |
343 | you do not want to wait for some I/O event, specify C<undef> for |
343 | you do not want to wait for some I/O event, specify C<undef> for |
344 | C<$fh_or_undef> and C<0> for C<$events>). |
344 | C<$fh_or_undef> and C<0> for C<$events>). |
345 | |
345 | |
346 | If timeout is C<undef> or negative, then there will be no |
346 | If timeout is C<undef> or negative, then there will be no |
347 | timeout. Otherwise a EV::timer with this value will be started. |
347 | timeout. Otherwise an C<EV::timer> with this value will be started. |
348 | |
348 | |
349 | When an error occurs or either the timeout or I/O watcher triggers, then |
349 | When an error occurs or either the timeout or I/O watcher triggers, then |
350 | the callback will be called with the received event set (in general |
350 | the callback will be called with the received event set (in general |
351 | you can expect it to be a combination of C<EV::ERROR>, C<EV::READ>, |
351 | you can expect it to be a combination of C<EV::ERROR>, C<EV::READ>, |
352 | C<EV::WRITE> and C<EV::TIMER>). |
352 | C<EV::WRITE> and C<EV::TIMER>). |
… | |
… | |
370 | |
370 | |
371 | =item EV::feed_signal $signal |
371 | =item EV::feed_signal $signal |
372 | |
372 | |
373 | Feed a signal event into EV - unlike C<EV::feed_signal_event>, this works |
373 | Feed a signal event into EV - unlike C<EV::feed_signal_event>, this works |
374 | regardless of which loop has registered the signal, and is mainly useful |
374 | regardless of which loop has registered the signal, and is mainly useful |
375 | fro custom signal implementations. |
375 | for custom signal implementations. |
376 | |
376 | |
377 | =item EV::set_io_collect_interval $time |
377 | =item EV::set_io_collect_interval $time |
378 | |
378 | |
379 | =item $loop->set_io_collect_interval ($time) |
379 | =item $loop->set_io_collect_interval ($time) |
380 | |
380 | |
… | |
… | |
507 | returns its C<$revents> bitset (as if its callback was invoked). If the |
507 | returns its C<$revents> bitset (as if its callback was invoked). If the |
508 | watcher isn't pending it does nothing and returns C<0>. |
508 | watcher isn't pending it does nothing and returns C<0>. |
509 | |
509 | |
510 | =item $previous_state = $w->keepalive ($bool) |
510 | =item $previous_state = $w->keepalive ($bool) |
511 | |
511 | |
512 | Normally, C<EV::loop> will return when there are no active watchers |
512 | Normally, C<EV::run> will return when there are no active watchers |
513 | (which is a "deadlock" because no progress can be made anymore). This is |
513 | (which is a "deadlock" because no progress can be made anymore). This is |
514 | convenient because it allows you to start your watchers (and your jobs), |
514 | convenient because it allows you to start your watchers (and your jobs), |
515 | call C<EV::loop> once and when it returns you know that all your jobs are |
515 | call C<EV::run> once and when it returns you know that all your jobs are |
516 | finished (or they forgot to register some watchers for their task :). |
516 | finished (or they forgot to register some watchers for their task :). |
517 | |
517 | |
518 | Sometimes, however, this gets in your way, for example when the module |
518 | Sometimes, however, this gets in your way, for example when the module |
519 | that calls C<EV::loop> (usually the main program) is not the same module |
519 | that calls C<EV::run> (usually the main program) is not the same module |
520 | as a long-living watcher (for example a DNS client module written by |
520 | as a long-living watcher (for example a DNS client module written by |
521 | somebody else even). Then you might want any outstanding requests to be |
521 | somebody else even). Then you might want any outstanding requests to be |
522 | handled, but you would not want to keep C<EV::loop> from returning just |
522 | handled, but you would not want to keep C<EV::run> from returning just |
523 | because you happen to have this long-running UDP port watcher. |
523 | because you happen to have this long-running UDP port watcher. |
524 | |
524 | |
525 | In this case you can clear the keepalive status, which means that even |
525 | In this case you can clear the keepalive status, which means that even |
526 | though your watcher is active, it won't keep C<EV::loop> from returning. |
526 | though your watcher is active, it won't keep C<EV::run> from returning. |
527 | |
527 | |
528 | The initial value for keepalive is true (enabled), and you can change it |
528 | The initial value for keepalive is true (enabled), and you can change it |
529 | any time. |
529 | any time. |
530 | |
530 | |
531 | Example: Register an I/O watcher for some UDP socket but do not keep the |
531 | Example: Register an I/O watcher for some UDP socket but do not keep the |
… | |
… | |
598 | |
598 | |
599 | =item $w = $loop->timer ($after, $repeat, $callback) |
599 | =item $w = $loop->timer ($after, $repeat, $callback) |
600 | |
600 | |
601 | =item $w = $loop->timer_ns ($after, $repeat, $callback) |
601 | =item $w = $loop->timer_ns ($after, $repeat, $callback) |
602 | |
602 | |
603 | Calls the callback after C<$after> seconds (which may be fractional). If |
603 | Calls the callback after C<$after> seconds (which may be fractional or |
604 | C<$repeat> is non-zero, the timer will be restarted (with the $repeat |
604 | negative). If C<$repeat> is non-zero, the timer will be restarted (with |
605 | value as $after) after the callback returns. |
605 | the $repeat value as $after) after the callback returns. |
606 | |
606 | |
607 | This means that the callback would be called roughly after C<$after> |
607 | This means that the callback would be called roughly after C<$after> |
608 | seconds, and then every C<$repeat> seconds. The timer does his best not |
608 | seconds, and then every C<$repeat> seconds. The timer does his best not |
609 | to drift, but it will not invoke the timer more often then once per event |
609 | to drift, but it will not invoke the timer more often then once per event |
610 | loop iteration, and might drift in other cases. If that isn't acceptable, |
610 | loop iteration, and might drift in other cases. If that isn't acceptable, |
… | |
… | |
614 | in front of the machine while the timer is running and changes the system |
614 | in front of the machine while the timer is running and changes the system |
615 | clock, the timer will nevertheless run (roughly) the same time. |
615 | clock, the timer will nevertheless run (roughly) the same time. |
616 | |
616 | |
617 | The C<timer_ns> variant doesn't start (activate) the newly created watcher. |
617 | The C<timer_ns> variant doesn't start (activate) the newly created watcher. |
618 | |
618 | |
619 | =item $w->set ($after, $repeat) |
619 | =item $w->set ($after, $repeat = 0) |
620 | |
620 | |
621 | Reconfigures the watcher, see the constructor above for details. Can be called at |
621 | Reconfigures the watcher, see the constructor above for details. Can be called at |
622 | any time. |
622 | any time. |
623 | |
623 | |
624 | =item $w->again |
624 | =item $w->again |
|
|
625 | |
|
|
626 | =item $w->again ($repeat) |
625 | |
627 | |
626 | Similar to the C<start> method, but has special semantics for repeating timers: |
628 | Similar to the C<start> method, but has special semantics for repeating timers: |
627 | |
629 | |
628 | If the timer is active and non-repeating, it will be stopped. |
630 | If the timer is active and non-repeating, it will be stopped. |
629 | |
631 | |
… | |
… | |
636 | |
638 | |
637 | This behaviour is useful when you have a timeout for some IO |
639 | This behaviour is useful when you have a timeout for some IO |
638 | operation. You create a timer object with the same value for C<$after> and |
640 | operation. You create a timer object with the same value for C<$after> and |
639 | C<$repeat>, and then, in the read/write watcher, run the C<again> method |
641 | C<$repeat>, and then, in the read/write watcher, run the C<again> method |
640 | on the timeout. |
642 | on the timeout. |
|
|
643 | |
|
|
644 | If called with a C<$repeat> argument, then it uses this a timer repeat |
|
|
645 | value. |
|
|
646 | |
|
|
647 | =item $after = $w->remaining |
|
|
648 | |
|
|
649 | Calculates and returns the remaining time till the timer will fire. |
641 | |
650 | |
642 | =back |
651 | =back |
643 | |
652 | |
644 | |
653 | |
645 | =head3 PERIODIC WATCHERS - to cron or not to cron? |
654 | =head3 PERIODIC WATCHERS - to cron or not to cron? |
… | |
… | |
673 | surpasses this time. |
682 | surpasses this time. |
674 | |
683 | |
675 | =item * repeating interval timer ($interval > 0, $reschedule_cb = 0) |
684 | =item * repeating interval timer ($interval > 0, $reschedule_cb = 0) |
676 | |
685 | |
677 | In this mode the watcher will always be scheduled to time out at the |
686 | In this mode the watcher will always be scheduled to time out at the |
678 | next C<$at + N * $interval> time (for some integer N) and then repeat, |
687 | next C<$at + N * $interval> time (for the lowest integer N) and then repeat, |
679 | regardless of any time jumps. |
688 | regardless of any time jumps. Note that, since C<N> can be negative, the |
|
|
689 | first trigger can happen before C<$at>. |
680 | |
690 | |
681 | This can be used to create timers that do not drift with respect to system |
691 | This can be used to create timers that do not drift with respect to system |
682 | time: |
692 | time: |
683 | |
693 | |
684 | my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" }; |
694 | my $hourly = EV::periodic 0, 3600, 0, sub { print "once/hour\n" }; |
685 | |
695 | |
686 | That doesn't mean there will always be 3600 seconds in between triggers, |
696 | That doesn't mean there will always be 3600 seconds in between triggers, |
687 | but only that the the clalback will be called when the system time shows a |
697 | but only that the the callback will be called when the system time shows a |
688 | full hour (UTC). |
698 | full hour (UTC). |
689 | |
699 | |
690 | Another way to think about it (for the mathematically inclined) is that |
700 | Another way to think about it (for the mathematically inclined) is that |
691 | EV::periodic will try to run the callback in this mode at the next |
701 | EV::periodic will try to run the callback in this mode at the next |
692 | possible time where C<$time = $at (mod $interval)>, regardless of any time |
702 | possible time where C<$time = $at (mod $interval)>, regardless of any time |
… | |
… | |
700 | time as second argument. |
710 | time as second argument. |
701 | |
711 | |
702 | I<This callback MUST NOT stop or destroy this or any other periodic |
712 | I<This callback MUST NOT stop or destroy this or any other periodic |
703 | watcher, ever, and MUST NOT call any event loop functions or methods>. If |
713 | watcher, ever, and MUST NOT call any event loop functions or methods>. If |
704 | you need to stop it, return 1e30 and stop it afterwards. You may create |
714 | you need to stop it, return 1e30 and stop it afterwards. You may create |
705 | and start a C<EV::prepare> watcher for this task. |
715 | and start an C<EV::prepare> watcher for this task. |
706 | |
716 | |
707 | It must return the next time to trigger, based on the passed time value |
717 | It must return the next time to trigger, based on the passed time value |
708 | (that is, the lowest time value larger than or equal to to the second |
718 | (that is, the lowest time value larger than or equal to to the second |
709 | argument). It will usually be called just before the callback will be |
719 | argument). It will usually be called just before the callback will be |
710 | triggered, but might be called at other times, too. |
720 | triggered, but might be called at other times, too. |
711 | |
721 | |
712 | This can be used to create very complex timers, such as a timer that |
722 | This can be used to create very complex timers, such as a timer that |
713 | triggers on each midnight, local time (actually 24 hours after the last |
723 | triggers on each midnight, local time (actually one day after the last |
714 | midnight, to keep the example simple. If you know a way to do it correctly |
724 | midnight, to keep the example simple): |
715 | in about the same space (without requiring elaborate modules), drop me a |
|
|
716 | note :): |
|
|
717 | |
725 | |
718 | my $daily = EV::periodic 0, 0, sub { |
726 | my $daily = EV::periodic 0, 0, sub { |
719 | my ($w, $now) = @_; |
727 | my ($w, $now) = @_; |
720 | |
728 | |
721 | use Time::Local (); |
729 | use Time::Local (); |
722 | my (undef, undef, undef, $d, $m, $y) = localtime $now; |
730 | my (undef, undef, undef, $d, $m, $y) = localtime $now; |
723 | 86400 + Time::Local::timelocal 0, 0, 0, $d, $m, $y |
731 | Time::Local::timelocal_nocheck 0, 0, 0, $d + 1, $m, $y |
724 | }, sub { |
732 | }, sub { |
725 | print "it's midnight or likely shortly after, now\n"; |
733 | print "it's midnight or likely shortly after, now\n"; |
726 | }; |
734 | }; |
727 | |
735 | |
728 | =back |
736 | =back |