… | |
… | |
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 | |