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.290 by root, Tue Sep 1 18:27:46 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} = ...;
1105 1106
1106package AnyEvent; 1107package AnyEvent;
1107 1108
1108# basically a tuned-down version of common::sense 1109# basically a tuned-down version of common::sense
1109sub common_sense { 1110sub common_sense {
1110 # no warnings 1111 # from common:.sense 1.0
1111 ${^WARNING_BITS} ^= ${^WARNING_BITS}; 1112 ${^WARNING_BITS} = "\xfc\x3f\xf3\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x03";
1112 # use strict vars subs 1113 # use strict vars subs
1113 $^H |= 0x00000600; 1114 $^H |= 0x00000600;
1114} 1115}
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.12';
1121our $MODEL; 1122our $MODEL;
1122 1123
1123our $AUTOLOAD; 1124our $AUTOLOAD;
1124our @ISA; 1125our @ISA;
1125 1126
1342 1343
1343package AnyEvent::Base; 1344package AnyEvent::Base;
1344 1345
1345# default implementations for many methods 1346# default implementations for many methods
1346 1347
1347sub _time { 1348sub _time() {
1348 # probe for availability of Time::HiRes 1349 # probe for availability of Time::HiRes
1349 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1350 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1350 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1351 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1351 *_time = \&Time::HiRes::time; 1352 *_time = \&Time::HiRes::time;
1352 # if (eval "use POSIX (); (POSIX::times())... 1353 # if (eval "use POSIX (); (POSIX::times())...
1372 1373
1373our $HAVE_ASYNC_INTERRUPT; 1374our $HAVE_ASYNC_INTERRUPT;
1374 1375
1375sub _have_async_interrupt() { 1376sub _have_async_interrupt() {
1376 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT} 1377 $HAVE_ASYNC_INTERRUPT = 1*(!$ENV{PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT}
1377 && eval "use Async::Interrupt 1.0 (); 1") 1378 && eval "use Async::Interrupt 1.02 (); 1")
1378 unless defined $HAVE_ASYNC_INTERRUPT; 1379 unless defined $HAVE_ASYNC_INTERRUPT;
1379 1380
1380 $HAVE_ASYNC_INTERRUPT 1381 $HAVE_ASYNC_INTERRUPT
1381} 1382}
1382 1383
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