1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported |
|
|
6 | event loops. |
6 | |
7 | |
7 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
8 | |
9 | |
9 | use AnyEvent; |
10 | use AnyEvent; |
10 | |
11 | |
… | |
… | |
930 | no warnings; |
931 | no warnings; |
931 | use strict qw(vars subs); |
932 | use strict qw(vars subs); |
932 | |
933 | |
933 | use Carp; |
934 | use Carp; |
934 | |
935 | |
935 | our $VERSION = 4.41; |
936 | our $VERSION = 4.411; |
936 | our $MODEL; |
937 | our $MODEL; |
937 | |
938 | |
938 | our $AUTOLOAD; |
939 | our $AUTOLOAD; |
939 | our @ISA; |
940 | our @ISA; |
940 | |
941 | |
941 | our @REGISTRY; |
942 | our @REGISTRY; |
942 | |
943 | |
943 | our $WIN32; |
944 | our $WIN32; |
944 | |
945 | |
945 | BEGIN { |
946 | BEGIN { |
946 | my $win32 = ! ! ($^O =~ /mswin32/i); |
947 | eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; |
947 | eval "sub WIN32(){ $win32 }"; |
948 | eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; |
|
|
949 | |
|
|
950 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
|
|
951 | if ${^TAINT}; |
948 | } |
952 | } |
949 | |
953 | |
950 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
954 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
951 | |
955 | |
952 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
956 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
… | |
… | |
1141 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
1145 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W) if $SIGPIPE_W; # just in case |
1142 | } else { |
1146 | } else { |
1143 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1147 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1144 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1148 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1145 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
1149 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
|
|
1150 | |
|
|
1151 | # not strictly required, as $^F is normally 2, but let's make sure... |
|
|
1152 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1153 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1146 | } |
1154 | } |
1147 | |
1155 | |
1148 | $SIGPIPE_R |
1156 | $SIGPIPE_R |
1149 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1157 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1150 | |
|
|
1151 | # not strictly required, as $^F is normally 2, but let's make sure... |
|
|
1152 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1153 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
|
|
1154 | |
1158 | |
1155 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1159 | $SIG_IO = AnyEvent->io (fh => $SIGPIPE_R, poll => "r", cb => \&_signal_exec); |
1156 | } |
1160 | } |
1157 | |
1161 | |
1158 | my $signal = uc $arg{signal} |
1162 | my $signal = uc $arg{signal} |
… | |
… | |
1336 | so on. |
1340 | so on. |
1337 | |
1341 | |
1338 | =head1 ENVIRONMENT VARIABLES |
1342 | =head1 ENVIRONMENT VARIABLES |
1339 | |
1343 | |
1340 | The following environment variables are used by this module or its |
1344 | The following environment variables are used by this module or its |
1341 | submodules: |
1345 | submodules. |
|
|
1346 | |
|
|
1347 | Note that AnyEvent will remove I<all> environment variables starting with |
|
|
1348 | C<PERL_ANYEVENT_> from C<%ENV> when it is loaded while taint mode is |
|
|
1349 | enabled. |
1342 | |
1350 | |
1343 | =over 4 |
1351 | =over 4 |
1344 | |
1352 | |
1345 | =item C<PERL_ANYEVENT_VERBOSE> |
1353 | =item C<PERL_ANYEVENT_VERBOSE> |
1346 | |
1354 | |
… | |
… | |
1886 | =item * C-based event loops perform very well with small number of |
1894 | =item * C-based event loops perform very well with small number of |
1887 | watchers, as the management overhead dominates. |
1895 | watchers, as the management overhead dominates. |
1888 | |
1896 | |
1889 | =back |
1897 | =back |
1890 | |
1898 | |
|
|
1899 | =head2 THE IO::Lambda BENCHMARK |
|
|
1900 | |
|
|
1901 | Recently I was told about the benchmark in the IO::Lambda manpage, which |
|
|
1902 | could be misinterpreted to make AnyEvent look bad. In fact, the benchmark |
|
|
1903 | simply compares IO::Lambda with POE, and IO::Lambda looks better (which |
|
|
1904 | shouldn't come as a surprise to anybody). As such, the benchmark is |
|
|
1905 | fine, and shows that the AnyEvent backend from IO::Lambda isn't very |
|
|
1906 | optimal. But how would AnyEvent compare when used without the extra |
|
|
1907 | baggage? To explore this, I wrote the equivalent benchmark for AnyEvent. |
|
|
1908 | |
|
|
1909 | The benchmark itself creates an echo-server, and then, for 500 times, |
|
|
1910 | connects to the echo server, sends a line, waits for the reply, and then |
|
|
1911 | creates the next connection. This is a rather bad benchmark, as it doesn't |
|
|
1912 | test the efficiency of the framework, but it is a benchmark nevertheless. |
|
|
1913 | |
|
|
1914 | name runtime |
|
|
1915 | Lambda/select 0.330 sec |
|
|
1916 | + optimized 0.122 sec |
|
|
1917 | Lambda/AnyEvent 0.327 sec |
|
|
1918 | + optimized 0.138 sec |
|
|
1919 | Raw sockets/select 0.077 sec |
|
|
1920 | POE/select, components 0.662 sec |
|
|
1921 | POE/select, raw sockets 0.226 sec |
|
|
1922 | POE/select, optimized 0.404 sec |
|
|
1923 | |
|
|
1924 | AnyEvent/select/nb 0.085 sec |
|
|
1925 | AnyEvent/EV/nb 0.068 sec |
|
|
1926 | +state machine 0.134 sec |
|
|
1927 | |
|
|
1928 | The benchmark is also a bit unfair (my fault) - the IO::Lambda |
|
|
1929 | benchmarks actually make blocking connects and use 100% blocking I/O, |
|
|
1930 | defeating the purpose of an event-based solution. All of the newly |
|
|
1931 | written AnyEvent benchmarks use 100% non-blocking connects (using |
|
|
1932 | AnyEvent::Socket::tcp_connect and the asynchronous pure perl DNS |
|
|
1933 | resolver), so AnyEvent is at a disadvantage here as non-blocking connects |
|
|
1934 | generally require a lot more bookkeeping and event handling than blocking |
|
|
1935 | connects (which involve a single syscall only). |
|
|
1936 | |
|
|
1937 | The last AnyEvent benchmark additionally uses L<AnyEvent::Handle>, which |
|
|
1938 | offers similar expressive power as POE and IO::Lambda (using conventional |
|
|
1939 | Perl syntax), which means both the echo server and the client are 100% |
|
|
1940 | non-blocking w.r.t. I/O, further placing it at a disadvantage. |
|
|
1941 | |
|
|
1942 | As you can see, AnyEvent + EV even beats the hand-optimised "raw sockets |
|
|
1943 | benchmark", while AnyEvent + its pure perl backend easily beats |
|
|
1944 | IO::Lambda and POE. |
|
|
1945 | |
|
|
1946 | And even the 100% non-blocking version written using the high-level (and |
|
|
1947 | slow :) L<AnyEvent::Handle> abstraction beats both POE and IO::Lambda, |
|
|
1948 | even thought it does all of DNS, tcp-connect and socket I/O in a |
|
|
1949 | non-blocking way. |
|
|
1950 | |
|
|
1951 | The two AnyEvent benchmarks can be found as F<eg/ae0.pl> and F<eg/ae2.pl> |
|
|
1952 | in the AnyEvent distribution, the remaining benchmarks are part of the |
|
|
1953 | IO::lambda distribution and were used without any changes. |
|
|
1954 | |
1891 | |
1955 | |
1892 | =head1 SIGNALS |
1956 | =head1 SIGNALS |
1893 | |
1957 | |
1894 | AnyEvent currently installs handlers for these signals: |
1958 | AnyEvent currently installs handlers for these signals: |
1895 | |
1959 | |
… | |
… | |
1952 | use AnyEvent; |
2016 | use AnyEvent; |
1953 | |
2017 | |
1954 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
2018 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1955 | be used to probe what backend is used and gain other information (which is |
2019 | be used to probe what backend is used and gain other information (which is |
1956 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and |
2020 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and |
1957 | $ENV{PERL_ANYEGENT_STRICT}. |
2021 | $ENV{PERL_ANYEVENT_STRICT}. |
1958 | |
2022 | |
1959 | |
2023 | |
1960 | =head1 BUGS |
2024 | =head1 BUGS |
1961 | |
2025 | |
1962 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |
2026 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |