… | |
… | |
176 | =head2 I/O WATCHERS |
176 | =head2 I/O WATCHERS |
177 | |
177 | |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
179 | with the following mandatory key-value pairs as arguments: |
179 | with the following mandatory key-value pairs as arguments: |
180 | |
180 | |
181 | C<fh> is the Perl I<file handle> (I<not> file descriptor) to watch |
181 | C<fh> is the Perl I<file handle> (I<not> file descriptor, see below) to |
182 | for events (AnyEvent might or might not keep a reference to this file |
182 | watch for events (AnyEvent might or might not keep a reference to this |
183 | handle). Note that only file handles pointing to things for which |
183 | file handle). Note that only file handles pointing to things for which |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
185 | most character devices, pipes, fifos and so on, but not for example files |
185 | most character devices, pipes, fifos and so on, but not for example files |
186 | or block devices. |
186 | or block devices. |
187 | |
187 | |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
… | |
… | |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
209 | chomp (my $input = <STDIN>); |
209 | chomp (my $input = <STDIN>); |
210 | warn "read: $input\n"; |
210 | warn "read: $input\n"; |
211 | undef $w; |
211 | undef $w; |
212 | }); |
212 | }); |
|
|
213 | |
|
|
214 | =head3 GETTING A FILE HANDLE FROM A FILE DESCRIPTOR |
|
|
215 | |
|
|
216 | It is not uncommon to only have a file descriptor, while AnyEvent requires |
|
|
217 | a Perl file handle. |
|
|
218 | |
|
|
219 | There are basically two methods to convert a file descriptor into a file handle. If you own |
|
|
220 | the file descriptor, you can open it with C<&=>, as in: |
|
|
221 | |
|
|
222 | open my $fh, "<&=$fileno" or die "xxx: ยง!"; |
|
|
223 | |
|
|
224 | This will "own" the file descriptor, meaning that when C<$fh> is |
|
|
225 | destroyed, it will automatically close the C<$fileno>. Also, note that |
|
|
226 | the open mode (read, write, read/write) must correspond with how the |
|
|
227 | underlying file descriptor was opened. |
|
|
228 | |
|
|
229 | In many cases, taking over the file descriptor is now what you want, in |
|
|
230 | which case the only alternative is to dup the file descriptor: |
|
|
231 | |
|
|
232 | open my $fh, "<&$fileno" or die "xxx: $!"; |
|
|
233 | |
|
|
234 | This has the advantage of not closing the file descriptor and the |
|
|
235 | disadvantage of making a slow copy. |
213 | |
236 | |
214 | =head2 TIME WATCHERS |
237 | =head2 TIME WATCHERS |
215 | |
238 | |
216 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
239 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
217 | method with the following mandatory arguments: |
240 | method with the following mandatory arguments: |
… | |
… | |
599 | |
622 | |
600 | =item $cv->begin ([group callback]) |
623 | =item $cv->begin ([group callback]) |
601 | |
624 | |
602 | =item $cv->end |
625 | =item $cv->end |
603 | |
626 | |
604 | These two methods are EXPERIMENTAL and MIGHT CHANGE. |
|
|
605 | |
|
|
606 | These two methods can be used to combine many transactions/events into |
627 | These two methods can be used to combine many transactions/events into |
607 | one. For example, a function that pings many hosts in parallel might want |
628 | one. For example, a function that pings many hosts in parallel might want |
608 | to use a condition variable for the whole process. |
629 | to use a condition variable for the whole process. |
609 | |
630 | |
610 | Every call to C<< ->begin >> will increment a counter, and every call to |
631 | Every call to C<< ->begin >> will increment a counter, and every call to |
611 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
632 | C<< ->end >> will decrement it. If the counter reaches C<0> in C<< ->end |
612 | >>, the (last) callback passed to C<begin> will be executed. That callback |
633 | >>, the (last) callback passed to C<begin> will be executed. That callback |
613 | is I<supposed> to call C<< ->send >>, but that is not required. If no |
634 | is I<supposed> to call C<< ->send >>, but that is not required. If no |
614 | callback was set, C<send> will be called without any arguments. |
635 | callback was set, C<send> will be called without any arguments. |
615 | |
636 | |
616 | Let's clarify this with the ping example: |
637 | You can think of C<< $cv->send >> giving you an OR condition (one call |
|
|
638 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
|
|
639 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
|
|
640 | |
|
|
641 | Let's start with a simple example: you have two I/O watchers (for example, |
|
|
642 | STDOUT and STDERR for a program), and you want to wait for both streams to |
|
|
643 | close before activating a condvar: |
|
|
644 | |
|
|
645 | my $cv = AnyEvent->condvar; |
|
|
646 | |
|
|
647 | $cv->begin; # first watcher |
|
|
648 | my $w1 = AnyEvent->io (fh => $fh1, cb => sub { |
|
|
649 | defined sysread $fh1, my $buf, 4096 |
|
|
650 | or $cv->end; |
|
|
651 | }); |
|
|
652 | |
|
|
653 | $cv->begin; # second watcher |
|
|
654 | my $w2 = AnyEvent->io (fh => $fh2, cb => sub { |
|
|
655 | defined sysread $fh2, my $buf, 4096 |
|
|
656 | or $cv->end; |
|
|
657 | }); |
|
|
658 | |
|
|
659 | $cv->recv; |
|
|
660 | |
|
|
661 | This works because for every event source (EOF on file handle), there is |
|
|
662 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
|
|
663 | sending. |
|
|
664 | |
|
|
665 | The ping example mentioned above is slightly more complicated, as the |
|
|
666 | there are results to be passwd back, and the number of tasks that are |
|
|
667 | begung can potentially be zero: |
617 | |
668 | |
618 | my $cv = AnyEvent->condvar; |
669 | my $cv = AnyEvent->condvar; |
619 | |
670 | |
620 | my %result; |
671 | my %result; |
621 | $cv->begin (sub { $cv->send (\%result) }); |
672 | $cv->begin (sub { $cv->send (\%result) }); |
… | |
… | |
641 | loop, which serves two important purposes: first, it sets the callback |
692 | loop, which serves two important purposes: first, it sets the callback |
642 | to be called once the counter reaches C<0>, and second, it ensures that |
693 | to be called once the counter reaches C<0>, and second, it ensures that |
643 | C<send> is called even when C<no> hosts are being pinged (the loop |
694 | C<send> is called even when C<no> hosts are being pinged (the loop |
644 | doesn't execute once). |
695 | doesn't execute once). |
645 | |
696 | |
646 | This is the general pattern when you "fan out" into multiple subrequests: |
697 | This is the general pattern when you "fan out" into multiple (but |
647 | use an outer C<begin>/C<end> pair to set the callback and ensure C<end> |
698 | potentially none) subrequests: use an outer C<begin>/C<end> pair to set |
648 | is called at least once, and then, for each subrequest you start, call |
699 | the callback and ensure C<end> is called at least once, and then, for each |
649 | C<begin> and for each subrequest you finish, call C<end>. |
700 | subrequest you start, call C<begin> and for each subrequest you finish, |
|
|
701 | call C<end>. |
650 | |
702 | |
651 | =back |
703 | =back |
652 | |
704 | |
653 | =head3 METHODS FOR CONSUMERS |
705 | =head3 METHODS FOR CONSUMERS |
654 | |
706 | |
… | |
… | |
939 | no warnings; |
991 | no warnings; |
940 | use strict qw(vars subs); |
992 | use strict qw(vars subs); |
941 | |
993 | |
942 | use Carp; |
994 | use Carp; |
943 | |
995 | |
944 | our $VERSION = 4.412; |
996 | our $VERSION = 4.8; |
945 | our $MODEL; |
997 | our $MODEL; |
946 | |
998 | |
947 | our $AUTOLOAD; |
999 | our $AUTOLOAD; |
948 | our @ISA; |
1000 | our @ISA; |
949 | |
1001 | |
… | |
… | |
1442 | =item C<PERL_ANYEVENT_MAX_FORKS> |
1494 | =item C<PERL_ANYEVENT_MAX_FORKS> |
1443 | |
1495 | |
1444 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
1496 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
1445 | will create in parallel. |
1497 | will create in parallel. |
1446 | |
1498 | |
|
|
1499 | =item C<PERL_ANYEVENT_MAX_OUTSTANDING_DNS> |
|
|
1500 | |
|
|
1501 | The default value for the C<max_outstanding> parameter for the default DNS |
|
|
1502 | resolver - this is the maximum number of parallel DNS requests that are |
|
|
1503 | sent to the DNS server. |
|
|
1504 | |
|
|
1505 | =item C<PERL_ANYEVENT_RESOLV_CONF> |
|
|
1506 | |
|
|
1507 | The file to use instead of F</etc/resolv.conf> (or OS-specific |
|
|
1508 | configuration) in the default resolver. When set to the empty string, no |
|
|
1509 | default config will be used. |
|
|
1510 | |
|
|
1511 | =item C<PERL_ANYEVENT_CA_FILE>, C<PERL_ANYEVENT_CA_PATH>. |
|
|
1512 | |
|
|
1513 | When neither C<ca_file> nor C<ca_path> was specified during |
|
|
1514 | L<AnyEvent::TLS> context creation, and either of these environment |
|
|
1515 | variables exist, they will be used to specify CA certificate locations |
|
|
1516 | instead of a system-dependent default. |
|
|
1517 | |
1447 | =back |
1518 | =back |
1448 | |
1519 | |
1449 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1520 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1450 | |
1521 | |
1451 | This is an advanced topic that you do not normally need to use AnyEvent in |
1522 | This is an advanced topic that you do not normally need to use AnyEvent in |
… | |
… | |
1695 | EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers |
1766 | EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers |
1696 | CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal |
1767 | CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal |
1697 | Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation |
1768 | Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation |
1698 | Event/Event 16000 517 32.20 31.80 0.81 Event native interface |
1769 | Event/Event 16000 517 32.20 31.80 0.81 Event native interface |
1699 | Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers |
1770 | Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers |
|
|
1771 | IOAsync/Any 16000 989 38.10 32.77 11.13 via IO::Async::Loop::IO_Poll |
|
|
1772 | IOAsync/Any 16000 990 37.59 29.50 10.61 via IO::Async::Loop::Epoll |
1700 | Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour |
1773 | Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour |
1701 | Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers |
1774 | Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers |
1702 | POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event |
1775 | POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event |
1703 | POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select |
1776 | POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select |
1704 | |
1777 | |
… | |
… | |
1733 | performance becomes really bad with lots of file descriptors (and few of |
1806 | performance becomes really bad with lots of file descriptors (and few of |
1734 | them active), of course, but this was not subject of this benchmark. |
1807 | them active), of course, but this was not subject of this benchmark. |
1735 | |
1808 | |
1736 | The C<Event> module has a relatively high setup and callback invocation |
1809 | The C<Event> module has a relatively high setup and callback invocation |
1737 | cost, but overall scores in on the third place. |
1810 | cost, but overall scores in on the third place. |
|
|
1811 | |
|
|
1812 | C<IO::Async> performs admirably well, about on par with C<Event>, even |
|
|
1813 | when using its pure perl backend. |
1738 | |
1814 | |
1739 | C<Glib>'s memory usage is quite a bit higher, but it features a |
1815 | C<Glib>'s memory usage is quite a bit higher, but it features a |
1740 | faster callback invocation and overall ends up in the same class as |
1816 | faster callback invocation and overall ends up in the same class as |
1741 | C<Event>. However, Glib scales extremely badly, doubling the number of |
1817 | C<Event>. However, Glib scales extremely badly, doubling the number of |
1742 | watchers increases the processing time by more than a factor of four, |
1818 | watchers increases the processing time by more than a factor of four, |
… | |
… | |
1820 | it to another server. This includes deleting the old timeout and creating |
1896 | it to another server. This includes deleting the old timeout and creating |
1821 | a new one that moves the timeout into the future. |
1897 | a new one that moves the timeout into the future. |
1822 | |
1898 | |
1823 | =head3 Results |
1899 | =head3 Results |
1824 | |
1900 | |
1825 | name sockets create request |
1901 | name sockets create request |
1826 | EV 20000 69.01 11.16 |
1902 | EV 20000 69.01 11.16 |
1827 | Perl 20000 73.32 35.87 |
1903 | Perl 20000 73.32 35.87 |
|
|
1904 | IOAsync 20000 157.00 98.14 epoll |
|
|
1905 | IOAsync 20000 159.31 616.06 poll |
1828 | Event 20000 212.62 257.32 |
1906 | Event 20000 212.62 257.32 |
1829 | Glib 20000 651.16 1896.30 |
1907 | Glib 20000 651.16 1896.30 |
1830 | POE 20000 349.67 12317.24 uses POE::Loop::Event |
1908 | POE 20000 349.67 12317.24 uses POE::Loop::Event |
1831 | |
1909 | |
1832 | =head3 Discussion |
1910 | =head3 Discussion |
1833 | |
1911 | |
1834 | This benchmark I<does> measure scalability and overall performance of the |
1912 | This benchmark I<does> measure scalability and overall performance of the |
1835 | particular event loop. |
1913 | particular event loop. |
… | |
… | |
1837 | EV is again fastest. Since it is using epoll on my system, the setup time |
1915 | EV is again fastest. Since it is using epoll on my system, the setup time |
1838 | is relatively high, though. |
1916 | is relatively high, though. |
1839 | |
1917 | |
1840 | Perl surprisingly comes second. It is much faster than the C-based event |
1918 | Perl surprisingly comes second. It is much faster than the C-based event |
1841 | loops Event and Glib. |
1919 | loops Event and Glib. |
|
|
1920 | |
|
|
1921 | IO::Async performs very well when using its epoll backend, and still quite |
|
|
1922 | good compared to Glib when using its pure perl backend. |
1842 | |
1923 | |
1843 | Event suffers from high setup time as well (look at its code and you will |
1924 | Event suffers from high setup time as well (look at its code and you will |
1844 | understand why). Callback invocation also has a high overhead compared to |
1925 | understand why). Callback invocation also has a high overhead compared to |
1845 | the C<< $_->() for .. >>-style loop that the Perl event loop uses. Event |
1926 | the C<< $_->() for .. >>-style loop that the Perl event loop uses. Event |
1846 | uses select or poll in basically all documented configurations. |
1927 | uses select or poll in basically all documented configurations. |