… | |
… | |
170 | |
170 | |
171 | Some event loops issue spurious readyness notifications, so you should |
171 | Some event loops issue spurious readyness notifications, so you should |
172 | always use non-blocking calls when reading/writing from/to your file |
172 | always use non-blocking calls when reading/writing from/to your file |
173 | handles. |
173 | handles. |
174 | |
174 | |
175 | Example: |
|
|
176 | |
|
|
177 | # wait for readability of STDIN, then read a line and disable the watcher |
175 | Example: wait for readability of STDIN, then read a line and disable the |
|
|
176 | watcher. |
|
|
177 | |
178 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
178 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
179 | chomp (my $input = <STDIN>); |
179 | chomp (my $input = <STDIN>); |
180 | warn "read: $input\n"; |
180 | warn "read: $input\n"; |
181 | undef $w; |
181 | undef $w; |
182 | }); |
182 | }); |
… | |
… | |
192 | |
192 | |
193 | Although the callback might get passed parameters, their value and |
193 | Although the callback might get passed parameters, their value and |
194 | presence is undefined and you cannot rely on them. Portable AnyEvent |
194 | presence is undefined and you cannot rely on them. Portable AnyEvent |
195 | callbacks cannot use arguments passed to time watcher callbacks. |
195 | callbacks cannot use arguments passed to time watcher callbacks. |
196 | |
196 | |
197 | The timer callback will be invoked at most once: if you want a repeating |
197 | The callback will normally be invoked once only. If you specify another |
198 | timer you have to create a new watcher (this is a limitation by both Tk |
198 | parameter, C<interval>, as a positive number, then the callback will be |
199 | and Glib). |
199 | invoked regularly at that interval (in fractional seconds) after the first |
|
|
200 | invocation. |
200 | |
201 | |
201 | Example: |
202 | The callback will be rescheduled before invoking the callback, but no |
|
|
203 | attempt is done to avoid timer drift in most backends, so the interval is |
|
|
204 | only approximate. |
202 | |
205 | |
203 | # fire an event after 7.7 seconds |
206 | Example: fire an event after 7.7 seconds. |
|
|
207 | |
204 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
208 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
205 | warn "timeout\n"; |
209 | warn "timeout\n"; |
206 | }); |
210 | }); |
207 | |
211 | |
208 | # to cancel the timer: |
212 | # to cancel the timer: |
209 | undef $w; |
213 | undef $w; |
210 | |
214 | |
211 | Example 2: |
|
|
212 | |
|
|
213 | # fire an event after 0.5 seconds, then roughly every second |
215 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
214 | my $w; |
|
|
215 | |
216 | |
216 | my $cb = sub { |
|
|
217 | # cancel the old timer while creating a new one |
|
|
218 | $w = AnyEvent->timer (after => 1, cb => $cb); |
217 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
|
|
218 | warn "timeout\n"; |
219 | }; |
219 | }; |
220 | |
|
|
221 | # start the "loop" by creating the first watcher |
|
|
222 | $w = AnyEvent->timer (after => 0.5, cb => $cb); |
|
|
223 | |
220 | |
224 | =head3 TIMING ISSUES |
221 | =head3 TIMING ISSUES |
225 | |
222 | |
226 | There are two ways to handle timers: based on real time (relative, "fire |
223 | There are two ways to handle timers: based on real time (relative, "fire |
227 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
224 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
… | |
… | |
738 | =item L<AnyEvent::Util> |
735 | =item L<AnyEvent::Util> |
739 | |
736 | |
740 | Contains various utility functions that replace often-used but blocking |
737 | Contains various utility functions that replace often-used but blocking |
741 | functions such as C<inet_aton> by event-/callback-based versions. |
738 | functions such as C<inet_aton> by event-/callback-based versions. |
742 | |
739 | |
743 | =item L<AnyEvent::Handle> |
|
|
744 | |
|
|
745 | Provide read and write buffers and manages watchers for reads and writes. |
|
|
746 | |
|
|
747 | =item L<AnyEvent::Socket> |
740 | =item L<AnyEvent::Socket> |
748 | |
741 | |
749 | Provides various utility functions for (internet protocol) sockets, |
742 | Provides various utility functions for (internet protocol) sockets, |
750 | addresses and name resolution. Also functions to create non-blocking tcp |
743 | addresses and name resolution. Also functions to create non-blocking tcp |
751 | connections or tcp servers, with IPv6 and SRV record support and more. |
744 | connections or tcp servers, with IPv6 and SRV record support and more. |
752 | |
745 | |
|
|
746 | =item L<AnyEvent::Handle> |
|
|
747 | |
|
|
748 | Provide read and write buffers, manages watchers for reads and writes, |
|
|
749 | supports raw and formatted I/O, I/O queued and fully transparent and |
|
|
750 | non-blocking SSL/TLS. |
|
|
751 | |
753 | =item L<AnyEvent::DNS> |
752 | =item L<AnyEvent::DNS> |
754 | |
753 | |
755 | Provides rich asynchronous DNS resolver capabilities. |
754 | Provides rich asynchronous DNS resolver capabilities. |
756 | |
755 | |
757 | =item L<AnyEvent::HTTP> |
756 | =item L<AnyEvent::HTTP> |
… | |
… | |
765 | |
764 | |
766 | =item L<AnyEvent::FastPing> |
765 | =item L<AnyEvent::FastPing> |
767 | |
766 | |
768 | The fastest ping in the west. |
767 | The fastest ping in the west. |
769 | |
768 | |
|
|
769 | =item L<AnyEvent::DBI> |
|
|
770 | |
|
|
771 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
772 | |
|
|
773 | =item L<AnyEvent::AIO> |
|
|
774 | |
|
|
775 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
776 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
777 | together. |
|
|
778 | |
|
|
779 | =item L<AnyEvent::BDB> |
|
|
780 | |
|
|
781 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
782 | L<BDB> and AnyEvent together. |
|
|
783 | |
|
|
784 | =item L<AnyEvent::GPSD> |
|
|
785 | |
|
|
786 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
787 | |
|
|
788 | =item L<AnyEvent::IGS> |
|
|
789 | |
|
|
790 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
791 | L<App::IGS>). |
|
|
792 | |
770 | =item L<Net::IRC3> |
793 | =item L<Net::IRC3> |
771 | |
794 | |
772 | AnyEvent based IRC client module family. |
795 | AnyEvent based IRC client module family. |
773 | |
796 | |
774 | =item L<Net::XMPP2> |
797 | =item L<Net::XMPP2> |
… | |
… | |
786 | |
809 | |
787 | =item L<Coro> |
810 | =item L<Coro> |
788 | |
811 | |
789 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
812 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
790 | |
813 | |
791 | =item L<AnyEvent::AIO>, L<IO::AIO> |
|
|
792 | |
|
|
793 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
794 | programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent |
|
|
795 | together. |
|
|
796 | |
|
|
797 | =item L<AnyEvent::BDB>, L<BDB> |
|
|
798 | |
|
|
799 | Truly asynchronous Berkeley DB access. AnyEvent::AIO transparently fuses |
|
|
800 | IO::AIO and AnyEvent together. |
|
|
801 | |
|
|
802 | =item L<IO::Lambda> |
814 | =item L<IO::Lambda> |
803 | |
815 | |
804 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
816 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
805 | |
817 | |
806 | =back |
818 | =back |
… | |
… | |
812 | no warnings; |
824 | no warnings; |
813 | use strict; |
825 | use strict; |
814 | |
826 | |
815 | use Carp; |
827 | use Carp; |
816 | |
828 | |
817 | our $VERSION = 4.14; |
829 | our $VERSION = 4.2; |
818 | our $MODEL; |
830 | our $MODEL; |
819 | |
831 | |
820 | our $AUTOLOAD; |
832 | our $AUTOLOAD; |
821 | our @ISA; |
833 | our @ISA; |
822 | |
834 | |
… | |
… | |
984 | sub AnyEvent::Base::Signal::DESTROY { |
996 | sub AnyEvent::Base::Signal::DESTROY { |
985 | my ($signal, $cb) = @{$_[0]}; |
997 | my ($signal, $cb) = @{$_[0]}; |
986 | |
998 | |
987 | delete $SIG_CB{$signal}{$cb}; |
999 | delete $SIG_CB{$signal}{$cb}; |
988 | |
1000 | |
989 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
1001 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
990 | } |
1002 | } |
991 | |
1003 | |
992 | # default implementation for ->child |
1004 | # default implementation for ->child |
993 | |
1005 | |
994 | our %PID_CB; |
1006 | our %PID_CB; |