1 | =head1 NAME |
1 | =head1 => NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
6 | |
6 | |
… | |
… | |
691 | no warnings; |
691 | no warnings; |
692 | use strict; |
692 | use strict; |
693 | |
693 | |
694 | use Carp; |
694 | use Carp; |
695 | |
695 | |
696 | our $VERSION = '3.4'; |
696 | our $VERSION = '3.6'; |
697 | our $MODEL; |
697 | our $MODEL; |
698 | |
698 | |
699 | our $AUTOLOAD; |
699 | our $AUTOLOAD; |
700 | our @ISA; |
700 | our @ISA; |
701 | |
701 | |
… | |
… | |
730 | 1 |
730 | 1 |
731 | } else { |
731 | } else { |
732 | push @post_detect, $cb; |
732 | push @post_detect, $cb; |
733 | |
733 | |
734 | defined wantarray |
734 | defined wantarray |
735 | ? bless \$cb, "AnyEvent::Util::Guard" |
735 | ? bless \$cb, "AnyEvent::Util::PostDetect" |
736 | : () |
736 | : () |
737 | } |
737 | } |
738 | } |
738 | } |
739 | |
739 | |
740 | sub AnyEvent::Util::Guard::DESTROY { |
740 | sub AnyEvent::Util::PostDetect::DESTROY { |
741 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
741 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
742 | } |
742 | } |
743 | |
743 | |
744 | sub detect() { |
744 | sub detect() { |
745 | unless ($MODEL) { |
745 | unless ($MODEL) { |
… | |
… | |
811 | package AnyEvent::Base; |
811 | package AnyEvent::Base; |
812 | |
812 | |
813 | # default implementation for ->condvar |
813 | # default implementation for ->condvar |
814 | |
814 | |
815 | sub condvar { |
815 | sub condvar { |
816 | bless {}, AnyEvent::CondVar:: |
816 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
817 | } |
817 | } |
818 | |
818 | |
819 | # default implementation for ->signal |
819 | # default implementation for ->signal |
820 | |
820 | |
821 | our %SIG_CB; |
821 | our %SIG_CB; |
… | |
… | |
944 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
944 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
945 | } |
945 | } |
946 | |
946 | |
947 | sub end { |
947 | sub end { |
948 | return if --$_[0]{_ae_counter}; |
948 | return if --$_[0]{_ae_counter}; |
949 | &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; |
949 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
950 | } |
950 | } |
951 | |
951 | |
952 | # undocumented/compatibility with pre-3.4 |
952 | # undocumented/compatibility with pre-3.4 |
953 | *broadcast = \&send; |
953 | *broadcast = \&send; |
954 | *wait = \&_wait; |
954 | *wait = \&_wait; |
… | |
… | |
1048 | poll => 'r', |
1048 | poll => 'r', |
1049 | cb => sub { |
1049 | cb => sub { |
1050 | warn "io event <$_[0]>\n"; # will always output <r> |
1050 | warn "io event <$_[0]>\n"; # will always output <r> |
1051 | chomp (my $input = <STDIN>); # read a line |
1051 | chomp (my $input = <STDIN>); # read a line |
1052 | warn "read: $input\n"; # output what has been read |
1052 | warn "read: $input\n"; # output what has been read |
1053 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
1053 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1054 | }, |
1054 | }, |
1055 | ); |
1055 | ); |
1056 | |
1056 | |
1057 | my $time_watcher; # can only be used once |
1057 | my $time_watcher; # can only be used once |
1058 | |
1058 | |
… | |
… | |
1063 | }); |
1063 | }); |
1064 | } |
1064 | } |
1065 | |
1065 | |
1066 | new_timer; # create first timer |
1066 | new_timer; # create first timer |
1067 | |
1067 | |
1068 | $cv->wait; # wait until user enters /^q/i |
1068 | $cv->recv; # wait until user enters /^q/i |
1069 | |
1069 | |
1070 | =head1 REAL-WORLD EXAMPLE |
1070 | =head1 REAL-WORLD EXAMPLE |
1071 | |
1071 | |
1072 | Consider the L<Net::FCP> module. It features (among others) the following |
1072 | Consider the L<Net::FCP> module. It features (among others) the following |
1073 | API calls, which are to freenet what HTTP GET requests are to http: |
1073 | API calls, which are to freenet what HTTP GET requests are to http: |
… | |
… | |
1129 | |
1129 | |
1130 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1130 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1131 | |
1131 | |
1132 | if (end-of-file or data complete) { |
1132 | if (end-of-file or data complete) { |
1133 | $txn->{result} = $txn->{buf}; |
1133 | $txn->{result} = $txn->{buf}; |
1134 | $txn->{finished}->broadcast; |
1134 | $txn->{finished}->send; |
1135 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1135 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1136 | } |
1136 | } |
1137 | |
1137 | |
1138 | The C<result> method, finally, just waits for the finished signal (if the |
1138 | The C<result> method, finally, just waits for the finished signal (if the |
1139 | request was already finished, it doesn't wait, of course, and returns the |
1139 | request was already finished, it doesn't wait, of course, and returns the |
1140 | data: |
1140 | data: |
1141 | |
1141 | |
1142 | $txn->{finished}->wait; |
1142 | $txn->{finished}->recv; |
1143 | return $txn->{result}; |
1143 | return $txn->{result}; |
1144 | |
1144 | |
1145 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1145 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1146 | that occured during request processing. The C<result> method detects |
1146 | that occured during request processing. The C<result> method detects |
1147 | whether an exception as thrown (it is stored inside the $txn object) |
1147 | whether an exception as thrown (it is stored inside the $txn object) |
… | |
… | |
1182 | |
1182 | |
1183 | my $quit = AnyEvent->condvar; |
1183 | my $quit = AnyEvent->condvar; |
1184 | |
1184 | |
1185 | $fcp->txn_client_get ($url)->cb (sub { |
1185 | $fcp->txn_client_get ($url)->cb (sub { |
1186 | ... |
1186 | ... |
1187 | $quit->broadcast; |
1187 | $quit->send; |
1188 | }); |
1188 | }); |
1189 | |
1189 | |
1190 | $quit->wait; |
1190 | $quit->recv; |
1191 | |
1191 | |
1192 | |
1192 | |
1193 | =head1 BENCHMARKS |
1193 | =head1 BENCHMARKS |
1194 | |
1194 | |
1195 | To give you an idea of the performance and overheads that AnyEvent adds |
1195 | To give you an idea of the performance and overheads that AnyEvent adds |
… | |
… | |
1224 | all watchers, to avoid adding memory overhead. That means closure creation |
1224 | all watchers, to avoid adding memory overhead. That means closure creation |
1225 | and memory usage is not included in the figures. |
1225 | and memory usage is not included in the figures. |
1226 | |
1226 | |
1227 | I<invoke> is the time, in microseconds, used to invoke a simple |
1227 | I<invoke> is the time, in microseconds, used to invoke a simple |
1228 | callback. The callback simply counts down a Perl variable and after it was |
1228 | callback. The callback simply counts down a Perl variable and after it was |
1229 | invoked "watcher" times, it would C<< ->broadcast >> a condvar once to |
1229 | invoked "watcher" times, it would C<< ->send >> a condvar once to |
1230 | signal the end of this phase. |
1230 | signal the end of this phase. |
1231 | |
1231 | |
1232 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1232 | I<destroy> is the time, in microseconds, that it takes to destroy a single |
1233 | watcher. |
1233 | watcher. |
1234 | |
1234 | |
… | |
… | |
1495 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1495 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1496 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1496 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
1497 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1497 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1498 | L<AnyEvent::Impl::POE>. |
1498 | L<AnyEvent::Impl::POE>. |
1499 | |
1499 | |
|
|
1500 | Asynchronous DNS: L<AnyEvent::DNS>. |
|
|
1501 | |
1500 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1502 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>, |
1501 | |
1503 | |
1502 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1504 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1503 | |
1505 | |
1504 | |
1506 | |