ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.117 by root, Sun May 11 17:54:13 2008 UTC vs.
Revision 1.118 by root, Sun May 11 18:08:36 2008 UTC

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
1072Consider the L<Net::FCP> module. It features (among others) the following 1072Consider the L<Net::FCP> module. It features (among others) the following
1073API calls, which are to freenet what HTTP GET requests are to http: 1073API 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
1138The C<result> method, finally, just waits for the finished signal (if the 1138The C<result> method, finally, just waits for the finished signal (if the
1139request was already finished, it doesn't wait, of course, and returns the 1139request was already finished, it doesn't wait, of course, and returns the
1140data: 1140data:
1141 1141
1142 $txn->{finished}->wait; 1142 $txn->{finished}->recv;
1143 return $txn->{result}; 1143 return $txn->{result};
1144 1144
1145The actual code goes further and collects all errors (C<die>s, exceptions) 1145The actual code goes further and collects all errors (C<die>s, exceptions)
1146that occured during request processing. The C<result> method detects 1146that occured during request processing. The C<result> method detects
1147whether an exception as thrown (it is stored inside the $txn object) 1147whether 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
1195To give you an idea of the performance and overheads that AnyEvent adds 1195To give you an idea of the performance and overheads that AnyEvent adds
1224all watchers, to avoid adding memory overhead. That means closure creation 1224all watchers, to avoid adding memory overhead. That means closure creation
1225and memory usage is not included in the figures. 1225and memory usage is not included in the figures.
1226 1226
1227I<invoke> is the time, in microseconds, used to invoke a simple 1227I<invoke> is the time, in microseconds, used to invoke a simple
1228callback. The callback simply counts down a Perl variable and after it was 1228callback. The callback simply counts down a Perl variable and after it was
1229invoked "watcher" times, it would C<< ->broadcast >> a condvar once to 1229invoked "watcher" times, it would C<< ->send >> a condvar once to
1230signal the end of this phase. 1230signal the end of this phase.
1231 1231
1232I<destroy> is the time, in microseconds, that it takes to destroy a single 1232I<destroy> is the time, in microseconds, that it takes to destroy a single
1233watcher. 1233watcher.
1234 1234

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines