… | |
… | |
722 | poll => 'r', |
722 | poll => 'r', |
723 | cb => sub { |
723 | cb => sub { |
724 | warn "io event <$_[0]>\n"; # will always output <r> |
724 | warn "io event <$_[0]>\n"; # will always output <r> |
725 | chomp (my $input = <STDIN>); # read a line |
725 | chomp (my $input = <STDIN>); # read a line |
726 | warn "read: $input\n"; # output what has been read |
726 | warn "read: $input\n"; # output what has been read |
727 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
727 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
728 | }, |
728 | }, |
729 | ); |
729 | ); |
730 | |
730 | |
731 | my $time_watcher; # can only be used once |
731 | my $time_watcher; # can only be used once |
732 | |
732 | |
… | |
… | |
737 | }); |
737 | }); |
738 | } |
738 | } |
739 | |
739 | |
740 | new_timer; # create first timer |
740 | new_timer; # create first timer |
741 | |
741 | |
742 | $cv->wait; # wait until user enters /^q/i |
742 | $cv->recv; # wait until user enters /^q/i |
743 | |
743 | |
744 | REAL-WORLD EXAMPLE |
744 | REAL-WORLD EXAMPLE |
745 | Consider the Net::FCP module. It features (among others) the following |
745 | Consider the Net::FCP module. It features (among others) the following |
746 | API calls, which are to freenet what HTTP GET requests are to http: |
746 | API calls, which are to freenet what HTTP GET requests are to http: |
747 | |
747 | |
… | |
… | |
802 | |
802 | |
803 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
803 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
804 | |
804 | |
805 | if (end-of-file or data complete) { |
805 | if (end-of-file or data complete) { |
806 | $txn->{result} = $txn->{buf}; |
806 | $txn->{result} = $txn->{buf}; |
807 | $txn->{finished}->broadcast; |
807 | $txn->{finished}->send; |
808 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
808 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
809 | } |
809 | } |
810 | |
810 | |
811 | The "result" method, finally, just waits for the finished signal (if the |
811 | The "result" method, finally, just waits for the finished signal (if the |
812 | request was already finished, it doesn't wait, of course, and returns |
812 | request was already finished, it doesn't wait, of course, and returns |
813 | the data: |
813 | the data: |
814 | |
814 | |
815 | $txn->{finished}->wait; |
815 | $txn->{finished}->recv; |
816 | return $txn->{result}; |
816 | return $txn->{result}; |
817 | |
817 | |
818 | The actual code goes further and collects all errors ("die"s, |
818 | The actual code goes further and collects all errors ("die"s, |
819 | exceptions) that occured during request processing. The "result" method |
819 | exceptions) that occured during request processing. The "result" method |
820 | detects whether an exception as thrown (it is stored inside the $txn |
820 | detects whether an exception as thrown (it is stored inside the $txn |
… | |
… | |
855 | |
855 | |
856 | my $quit = AnyEvent->condvar; |
856 | my $quit = AnyEvent->condvar; |
857 | |
857 | |
858 | $fcp->txn_client_get ($url)->cb (sub { |
858 | $fcp->txn_client_get ($url)->cb (sub { |
859 | ... |
859 | ... |
860 | $quit->broadcast; |
860 | $quit->send; |
861 | }); |
861 | }); |
862 | |
862 | |
863 | $quit->wait; |
863 | $quit->recv; |
864 | |
864 | |
865 | BENCHMARKS |
865 | BENCHMARKS |
866 | To give you an idea of the performance and overheads that AnyEvent adds |
866 | To give you an idea of the performance and overheads that AnyEvent adds |
867 | over the event loops themselves and to give you an impression of the |
867 | over the event loops themselves and to give you an impression of the |
868 | speed of various event loops I prepared some benchmarks. |
868 | speed of various event loops I prepared some benchmarks. |
… | |
… | |
893 | between all watchers, to avoid adding memory overhead. That means |
893 | between all watchers, to avoid adding memory overhead. That means |
894 | closure creation and memory usage is not included in the figures. |
894 | closure creation and memory usage is not included in the figures. |
895 | |
895 | |
896 | *invoke* is the time, in microseconds, used to invoke a simple callback. |
896 | *invoke* is the time, in microseconds, used to invoke a simple callback. |
897 | The callback simply counts down a Perl variable and after it was invoked |
897 | The callback simply counts down a Perl variable and after it was invoked |
898 | "watcher" times, it would "->broadcast" a condvar once to signal the end |
898 | "watcher" times, it would "->send" a condvar once to signal the end of |
899 | of this phase. |
899 | this phase. |
900 | |
900 | |
901 | *destroy* is the time, in microseconds, that it takes to destroy a |
901 | *destroy* is the time, in microseconds, that it takes to destroy a |
902 | single watcher. |
902 | single watcher. |
903 | |
903 | |
904 | Results |
904 | Results |