… | |
… | |
576 | after => 1, |
576 | after => 1, |
577 | cb => sub { $result_ready->send }, |
577 | cb => sub { $result_ready->send }, |
578 | ); |
578 | ); |
579 | |
579 | |
580 | # this "blocks" (while handling events) till the callback |
580 | # this "blocks" (while handling events) till the callback |
581 | # calls -<send |
581 | # calls ->send |
582 | $result_ready->recv; |
582 | $result_ready->recv; |
583 | |
583 | |
584 | Example: wait for a timer, but take advantage of the fact that condition |
584 | Example: wait for a timer, but take advantage of the fact that condition |
585 | variables are also callable directly. |
585 | variables are also callable directly. |
586 | |
586 | |
… | |
… | |
643 | into one. For example, a function that pings many hosts in parallel |
643 | into one. For example, a function that pings many hosts in parallel |
644 | might want to use a condition variable for the whole process. |
644 | might want to use a condition variable for the whole process. |
645 | |
645 | |
646 | Every call to "->begin" will increment a counter, and every call to |
646 | Every call to "->begin" will increment a counter, and every call to |
647 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
647 | "->end" will decrement it. If the counter reaches 0 in "->end", the |
648 | (last) callback passed to "begin" will be executed. That callback is |
648 | (last) callback passed to "begin" will be executed, passing the |
649 | *supposed* to call "->send", but that is not required. If no |
649 | condvar as first argument. That callback is *supposed* to call |
|
|
650 | "->send", but that is not required. If no group callback was set, |
650 | callback was set, "send" will be called without any arguments. |
651 | "send" will be called without any arguments. |
651 | |
652 | |
652 | You can think of "$cv->send" giving you an OR condition (one call |
653 | You can think of "$cv->send" giving you an OR condition (one call |
653 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
654 | sends), while "$cv->begin" and "$cv->end" giving you an AND |
654 | condition (all "begin" calls must be "end"'ed before the condvar |
655 | condition (all "begin" calls must be "end"'ed before the condvar |
655 | sends). |
656 | sends). |
… | |
… | |
683 | that are begung can potentially be zero: |
684 | that are begung can potentially be zero: |
684 | |
685 | |
685 | my $cv = AnyEvent->condvar; |
686 | my $cv = AnyEvent->condvar; |
686 | |
687 | |
687 | my %result; |
688 | my %result; |
688 | $cv->begin (sub { $cv->send (\%result) }); |
689 | $cv->begin (sub { shift->send (\%result) }); |
689 | |
690 | |
690 | for my $host (@list_of_hosts) { |
691 | for my $host (@list_of_hosts) { |
691 | $cv->begin; |
692 | $cv->begin; |
692 | ping_host_then_call_callback $host, sub { |
693 | ping_host_then_call_callback $host, sub { |
693 | $result{$host} = ...; |
694 | $result{$host} = ...; |
… | |
… | |
1227 | warn "read: $input\n"; # output what has been read |
1228 | warn "read: $input\n"; # output what has been read |
1228 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1229 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1229 | }, |
1230 | }, |
1230 | ); |
1231 | ); |
1231 | |
1232 | |
1232 | my $time_watcher; # can only be used once |
|
|
1233 | |
|
|
1234 | sub new_timer { |
|
|
1235 | $timer = AnyEvent->timer (after => 1, cb => sub { |
1233 | my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { |
1236 | warn "timeout\n"; # print 'timeout' about every second |
1234 | warn "timeout\n"; # print 'timeout' at most every second |
1237 | &new_timer; # and restart the time |
|
|
1238 | }); |
|
|
1239 | } |
1235 | }); |
1240 | |
|
|
1241 | new_timer; # create first timer |
|
|
1242 | |
1236 | |
1243 | $cv->recv; # wait until user enters /^q/i |
1237 | $cv->recv; # wait until user enters /^q/i |
1244 | |
1238 | |
1245 | REAL-WORLD EXAMPLE |
1239 | REAL-WORLD EXAMPLE |
1246 | Consider the Net::FCP module. It features (among others) the following |
1240 | Consider the Net::FCP module. It features (among others) the following |
… | |
… | |
1664 | As you can see, the AnyEvent + EV combination even beats the |
1658 | As you can see, the AnyEvent + EV combination even beats the |
1665 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1659 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1666 | backend easily beats IO::Lambda and POE. |
1660 | backend easily beats IO::Lambda and POE. |
1667 | |
1661 | |
1668 | And even the 100% non-blocking version written using the high-level (and |
1662 | And even the 100% non-blocking version written using the high-level (and |
1669 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda by a |
1663 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda |
1670 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
1664 | higher level ("unoptimised") abstractions by a large margin, even though |
1671 | in a non-blocking way. |
1665 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
1672 | |
1666 | |
1673 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1667 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1674 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1668 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1675 | part of the IO::lambda distribution and were used without any changes. |
1669 | part of the IO::Lambda distribution and were used without any changes. |
1676 | |
1670 | |
1677 | SIGNALS |
1671 | SIGNALS |
1678 | AnyEvent currently installs handlers for these signals: |
1672 | AnyEvent currently installs handlers for these signals: |
1679 | |
1673 | |
1680 | SIGCHLD |
1674 | SIGCHLD |
… | |
… | |
1748 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1742 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1749 | uses a lot less memory), but otherwise doesn't affect guard |
1743 | uses a lot less memory), but otherwise doesn't affect guard |
1750 | operation much. It is purely used for performance. |
1744 | operation much. It is purely used for performance. |
1751 | |
1745 | |
1752 | JSON and JSON::XS |
1746 | JSON and JSON::XS |
1753 | This module is required when you want to read or write JSON data via |
1747 | One of these modules is required when you want to read or write JSON |
1754 | AnyEvent::Handle. It is also written in pure-perl, but can take |
1748 | data via AnyEvent::Handle. It is also written in pure-perl, but can |
1755 | advantage of the ultra-high-speed JSON::XS module when it is |
1749 | take advantage of the ultra-high-speed JSON::XS module when it is |
1756 | installed. |
1750 | installed. |
1757 | |
1751 | |
1758 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1752 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1759 | installed. |
1753 | installed. |
1760 | |
1754 | |