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.279 by root, Sun Aug 9 16:05:11 2009 UTC vs.
Revision 1.288 by root, Tue Sep 1 10:40:05 2009 UTC

592 after => 1, 592 after => 1,
593 cb => sub { $result_ready->send }, 593 cb => sub { $result_ready->send },
594 ); 594 );
595 595
596 # this "blocks" (while handling events) till the callback 596 # this "blocks" (while handling events) till the callback
597 # calls -<send 597 # calls ->send
598 $result_ready->recv; 598 $result_ready->recv;
599 599
600Example: wait for a timer, but take advantage of the fact that condition 600Example: wait for a timer, but take advantage of the fact that condition
601variables are also callable directly. 601variables are also callable directly.
602 602
666one. For example, a function that pings many hosts in parallel might want 666one. For example, a function that pings many hosts in parallel might want
667to use a condition variable for the whole process. 667to use a condition variable for the whole process.
668 668
669Every call to C<< ->begin >> will increment a counter, and every call to 669Every call to C<< ->begin >> will increment a counter, and every call to
670C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end 670C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end
671>>, the (last) callback passed to C<begin> will be executed. That callback 671>>, the (last) callback passed to C<begin> will be executed, passing the
672is I<supposed> to call C<< ->send >>, but that is not required. If no 672condvar as first argument. That callback is I<supposed> to call C<< ->send
673callback was set, C<send> will be called without any arguments. 673>>, but that is not required. If no group callback was set, C<send> will
674be called without any arguments.
674 675
675You can think of C<< $cv->send >> giving you an OR condition (one call 676You can think of C<< $cv->send >> giving you an OR condition (one call
676sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND 677sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND
677condition (all C<begin> calls must be C<end>'ed before the condvar sends). 678condition (all C<begin> calls must be C<end>'ed before the condvar sends).
678 679
705begung can potentially be zero: 706begung can potentially be zero:
706 707
707 my $cv = AnyEvent->condvar; 708 my $cv = AnyEvent->condvar;
708 709
709 my %result; 710 my %result;
710 $cv->begin (sub { $cv->send (\%result) }); 711 $cv->begin (sub { shift->send (\%result) });
711 712
712 for my $host (@list_of_hosts) { 713 for my $host (@list_of_hosts) {
713 $cv->begin; 714 $cv->begin;
714 ping_host_then_call_callback $host, sub { 715 ping_host_then_call_callback $host, sub {
715 $result{$host} = ...; 716 $result{$host} = ...;
1115 1116
1116BEGIN { AnyEvent::common_sense } 1117BEGIN { AnyEvent::common_sense }
1117 1118
1118use Carp (); 1119use Carp ();
1119 1120
1120our $VERSION = '5.0'; 1121our $VERSION = '5.112';
1121our $MODEL; 1122our $MODEL;
1122 1123
1123our $AUTOLOAD; 1124our $AUTOLOAD;
1124our @ISA; 1125our @ISA;
1125 1126
1911 warn "read: $input\n"; # output what has been read 1912 warn "read: $input\n"; # output what has been read
1912 $cv->send if $input =~ /^q/i; # quit program if /^q/i 1913 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1913 }, 1914 },
1914 ); 1915 );
1915 1916
1916 my $time_watcher; # can only be used once
1917
1918 sub new_timer {
1919 $timer = AnyEvent->timer (after => 1, cb => sub { 1917 my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub {
1920 warn "timeout\n"; # print 'timeout' about every second 1918 warn "timeout\n"; # print 'timeout' at most every second
1921 &new_timer; # and restart the time
1922 }); 1919 });
1923 }
1924
1925 new_timer; # create first timer
1926 1920
1927 $cv->recv; # wait until user enters /^q/i 1921 $cv->recv; # wait until user enters /^q/i
1928 1922
1929=head1 REAL-WORLD EXAMPLE 1923=head1 REAL-WORLD EXAMPLE
1930 1924
2374As you can see, the AnyEvent + EV combination even beats the 2368As you can see, the AnyEvent + EV combination even beats the
2375hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl 2369hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl
2376backend easily beats IO::Lambda and POE. 2370backend easily beats IO::Lambda and POE.
2377 2371
2378And even the 100% non-blocking version written using the high-level (and 2372And even the 100% non-blocking version written using the high-level (and
2379slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda by a 2373slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda
2380large margin, even though it does all of DNS, tcp-connect and socket I/O 2374higher level ("unoptimised") abstractions by a large margin, even though
2381in a non-blocking way. 2375it does all of DNS, tcp-connect and socket I/O in a non-blocking way.
2382 2376
2383The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and 2377The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and
2384F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are 2378F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are
2385part of the IO::lambda distribution and were used without any changes. 2379part of the IO::Lambda distribution and were used without any changes.
2386 2380
2387 2381
2388=head1 SIGNALS 2382=head1 SIGNALS
2389 2383
2390AnyEvent currently installs handlers for these signals: 2384AnyEvent currently installs handlers for these signals:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines