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