… | |
… | |
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 | |
600 | Example: wait for a timer, but take advantage of the fact that condition |
600 | Example: wait for a timer, but take advantage of the fact that condition |
601 | variables are also callable directly. |
601 | variables are also callable directly. |
602 | |
602 | |
… | |
… | |
666 | one. For example, a function that pings many hosts in parallel might want |
666 | one. For example, a function that pings many hosts in parallel might want |
667 | to use a condition variable for the whole process. |
667 | to use a condition variable for the whole process. |
668 | |
668 | |
669 | Every call to C<< ->begin >> will increment a counter, and every call to |
669 | Every call to C<< ->begin >> will increment a counter, and every call to |
670 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
670 | C<< ->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 |
672 | is I<supposed> to call C<< ->send >>, but that is not required. If no |
672 | condvar as first argument. That callback is I<supposed> to call C<< ->send |
673 | callback 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 |
|
|
674 | be called without any arguments. |
674 | |
675 | |
675 | You can think of C<< $cv->send >> giving you an OR condition (one call |
676 | You can think of C<< $cv->send >> giving you an OR condition (one call |
676 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
677 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
677 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
678 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
678 | |
679 | |
… | |
… | |
705 | begung can potentially be zero: |
706 | begung 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 | |
1106 | package AnyEvent; |
1107 | package AnyEvent; |
1107 | |
1108 | |
1108 | # basically a tuned-down version of common::sense |
1109 | # basically a tuned-down version of common::sense |
1109 | sub common_sense { |
1110 | sub 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 | |
1116 | BEGIN { AnyEvent::common_sense } |
1117 | BEGIN { AnyEvent::common_sense } |
1117 | |
1118 | |
1118 | use Carp (); |
1119 | use Carp (); |
1119 | |
1120 | |
1120 | our $VERSION = 4.92; |
1121 | our $VERSION = '5.12'; |
1121 | our $MODEL; |
1122 | our $MODEL; |
1122 | |
1123 | |
1123 | our $AUTOLOAD; |
1124 | our $AUTOLOAD; |
1124 | our @ISA; |
1125 | our @ISA; |
1125 | |
1126 | |
… | |
… | |
1342 | |
1343 | |
1343 | package AnyEvent::Base; |
1344 | package AnyEvent::Base; |
1344 | |
1345 | |
1345 | # default implementations for many methods |
1346 | # default implementations for many methods |
1346 | |
1347 | |
1347 | sub _time { |
1348 | sub _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 | |
1373 | our $HAVE_ASYNC_INTERRUPT; |
1374 | our $HAVE_ASYNC_INTERRUPT; |
1374 | |
1375 | |
1375 | sub _have_async_interrupt() { |
1376 | sub _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 | |
… | |
… | |
2374 | As you can see, the AnyEvent + EV combination even beats the |
2368 | As you can see, the AnyEvent + EV combination even beats the |
2375 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
2369 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
2376 | backend easily beats IO::Lambda and POE. |
2370 | backend easily beats IO::Lambda and POE. |
2377 | |
2371 | |
2378 | And even the 100% non-blocking version written using the high-level (and |
2372 | And even the 100% non-blocking version written using the high-level (and |
2379 | slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda by a |
2373 | slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda |
2380 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
2374 | higher level ("unoptimised") abstractions by a large margin, even though |
2381 | in a non-blocking way. |
2375 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
2382 | |
2376 | |
2383 | The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and |
2377 | The two AnyEvent benchmarks programs can be found as F<eg/ae0.pl> and |
2384 | F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are |
2378 | F<eg/ae2.pl> in the AnyEvent distribution, the remaining benchmarks are |
2385 | part of the IO::lambda distribution and were used without any changes. |
2379 | part of the IO::Lambda distribution and were used without any changes. |
2386 | |
2380 | |
2387 | |
2381 | |
2388 | =head1 SIGNALS |
2382 | =head1 SIGNALS |
2389 | |
2383 | |
2390 | AnyEvent currently installs handlers for these signals: |
2384 | AnyEvent currently installs handlers for these signals: |
… | |
… | |
2479 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2473 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2480 | purely used for performance. |
2474 | purely used for performance. |
2481 | |
2475 | |
2482 | =item L<JSON> and L<JSON::XS> |
2476 | =item L<JSON> and L<JSON::XS> |
2483 | |
2477 | |
2484 | This module is required when you want to read or write JSON data via |
2478 | One of these modules is required when you want to read or write JSON data |
2485 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2479 | via L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2486 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2480 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2487 | |
2481 | |
2488 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2482 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2489 | installed. |
2483 | installed. |
2490 | |
2484 | |