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::Base::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; |
… | |
… | |
895 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
895 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
896 | |
896 | |
897 | undef $CHLD_W unless keys %PID_CB; |
897 | undef $CHLD_W unless keys %PID_CB; |
898 | } |
898 | } |
899 | |
899 | |
900 | package AnyEvent::Base::CondVar; |
900 | package AnyEvent::CondVar; |
901 | |
901 | |
902 | # wake up the waiter |
902 | our @ISA = AnyEvent::CondVar::Base::; |
|
|
903 | |
|
|
904 | package AnyEvent::CondVar::Base; |
|
|
905 | |
903 | sub _send { |
906 | sub _send { |
904 | &{ delete $_[0]{_ae_cb} } if $_[0]{_ae_cb}; |
907 | # nop |
905 | } |
908 | } |
906 | |
909 | |
907 | sub send { |
910 | sub send { |
908 | my $cv = shift; |
911 | my $cv = shift; |
909 | $cv->{_ae_sent} = [@_]; |
912 | $cv->{_ae_sent} = [@_]; |
|
|
913 | (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; |
910 | $cv->_send; |
914 | $cv->_send; |
911 | } |
915 | } |
912 | |
916 | |
913 | sub croak { |
917 | sub croak { |
914 | $_[0]{_ae_croak} = $_[1]; |
918 | $_[0]{_ae_croak} = $_[1]; |
… | |
… | |
917 | |
921 | |
918 | sub ready { |
922 | sub ready { |
919 | $_[0]{_ae_sent} |
923 | $_[0]{_ae_sent} |
920 | } |
924 | } |
921 | |
925 | |
|
|
926 | sub _wait { |
|
|
927 | AnyEvent->one_event while !$_[0]{_ae_sent}; |
|
|
928 | } |
|
|
929 | |
922 | sub recv { |
930 | sub recv { |
923 | AnyEvent->one_event while !$_[0]{_ae_sent}; |
931 | $_[0]->_wait; |
924 | |
932 | |
925 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
933 | Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; |
926 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
934 | wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] |
927 | } |
935 | } |
928 | |
936 | |
… | |
… | |
936 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
944 | $_[0]{_ae_end_cb} = $_[1] if @_ > 1; |
937 | } |
945 | } |
938 | |
946 | |
939 | sub end { |
947 | sub end { |
940 | return if --$_[0]{_ae_counter}; |
948 | return if --$_[0]{_ae_counter}; |
941 | &{ $_[0]{_ae_end_cb} } if $_[0]{_ae_end_cb}; |
949 | &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; |
942 | } |
950 | } |
943 | |
951 | |
944 | # undocumented/compatibility with pre-3.4 |
952 | # undocumented/compatibility with pre-3.4 |
945 | *broadcast = \&send; |
953 | *broadcast = \&send; |
946 | *wait = \&recv; |
954 | *wait = \&_wait; |
947 | |
955 | |
948 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
956 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
949 | |
957 | |
950 | This is an advanced topic that you do not normally need to use AnyEvent in |
958 | This is an advanced topic that you do not normally need to use AnyEvent in |
951 | a module. This section is only of use to event loop authors who want to |
959 | a module. This section is only of use to event loop authors who want to |
… | |
… | |
1040 | poll => 'r', |
1048 | poll => 'r', |
1041 | cb => sub { |
1049 | cb => sub { |
1042 | warn "io event <$_[0]>\n"; # will always output <r> |
1050 | warn "io event <$_[0]>\n"; # will always output <r> |
1043 | chomp (my $input = <STDIN>); # read a line |
1051 | chomp (my $input = <STDIN>); # read a line |
1044 | warn "read: $input\n"; # output what has been read |
1052 | warn "read: $input\n"; # output what has been read |
1045 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
1053 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1046 | }, |
1054 | }, |
1047 | ); |
1055 | ); |
1048 | |
1056 | |
1049 | my $time_watcher; # can only be used once |
1057 | my $time_watcher; # can only be used once |
1050 | |
1058 | |
… | |
… | |
1055 | }); |
1063 | }); |
1056 | } |
1064 | } |
1057 | |
1065 | |
1058 | new_timer; # create first timer |
1066 | new_timer; # create first timer |
1059 | |
1067 | |
1060 | $cv->wait; # wait until user enters /^q/i |
1068 | $cv->recv; # wait until user enters /^q/i |
1061 | |
1069 | |
1062 | =head1 REAL-WORLD EXAMPLE |
1070 | =head1 REAL-WORLD EXAMPLE |
1063 | |
1071 | |
1064 | 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 |
1065 | 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: |
… | |
… | |
1121 | |
1129 | |
1122 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1130 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1123 | |
1131 | |
1124 | if (end-of-file or data complete) { |
1132 | if (end-of-file or data complete) { |
1125 | $txn->{result} = $txn->{buf}; |
1133 | $txn->{result} = $txn->{buf}; |
1126 | $txn->{finished}->broadcast; |
1134 | $txn->{finished}->send; |
1127 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1135 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
1128 | } |
1136 | } |
1129 | |
1137 | |
1130 | 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 |
1131 | 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 |
1132 | data: |
1140 | data: |
1133 | |
1141 | |
1134 | $txn->{finished}->wait; |
1142 | $txn->{finished}->recv; |
1135 | return $txn->{result}; |
1143 | return $txn->{result}; |
1136 | |
1144 | |
1137 | 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) |
1138 | that occured during request processing. The C<result> method detects |
1146 | that occured during request processing. The C<result> method detects |
1139 | 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) |
… | |
… | |
1174 | |
1182 | |
1175 | my $quit = AnyEvent->condvar; |
1183 | my $quit = AnyEvent->condvar; |
1176 | |
1184 | |
1177 | $fcp->txn_client_get ($url)->cb (sub { |
1185 | $fcp->txn_client_get ($url)->cb (sub { |
1178 | ... |
1186 | ... |
1179 | $quit->broadcast; |
1187 | $quit->send; |
1180 | }); |
1188 | }); |
1181 | |
1189 | |
1182 | $quit->wait; |
1190 | $quit->recv; |
1183 | |
1191 | |
1184 | |
1192 | |
1185 | =head1 BENCHMARKS |
1193 | =head1 BENCHMARKS |
1186 | |
1194 | |
1187 | 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 |
… | |
… | |
1216 | all watchers, to avoid adding memory overhead. That means closure creation |
1224 | all watchers, to avoid adding memory overhead. That means closure creation |
1217 | and memory usage is not included in the figures. |
1225 | and memory usage is not included in the figures. |
1218 | |
1226 | |
1219 | 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 |
1220 | 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 |
1221 | invoked "watcher" times, it would C<< ->broadcast >> a condvar once to |
1229 | invoked "watcher" times, it would C<< ->send >> a condvar once to |
1222 | signal the end of this phase. |
1230 | signal the end of this phase. |
1223 | |
1231 | |
1224 | 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 |
1225 | watcher. |
1233 | watcher. |
1226 | |
1234 | |
… | |
… | |
1487 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1495 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
1488 | 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>, |
1489 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1497 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
1490 | L<AnyEvent::Impl::POE>. |
1498 | L<AnyEvent::Impl::POE>. |
1491 | |
1499 | |
|
|
1500 | Asynchronous DNS: L<AnyEvent::DNS>. |
|
|
1501 | |
1492 | 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>, |
1493 | |
1503 | |
1494 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1504 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
1495 | |
1505 | |
1496 | |
1506 | |