… | |
… | |
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> (or a naked file descriptor) to watch |
182 | for events (AnyEvent might or might not keep a reference to this file |
182 | for events (AnyEvent might or might not keep a reference to this file |
183 | handle). Note that only file handles pointing to things for which |
183 | 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. |
… | |
… | |
599 | |
599 | |
600 | =item $cv->begin ([group callback]) |
600 | =item $cv->begin ([group callback]) |
601 | |
601 | |
602 | =item $cv->end |
602 | =item $cv->end |
603 | |
603 | |
604 | These two methods are EXPERIMENTAL and MIGHT CHANGE. |
|
|
605 | |
|
|
606 | These two methods can be used to combine many transactions/events into |
604 | 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 |
605 | one. For example, a function that pings many hosts in parallel might want |
608 | to use a condition variable for the whole process. |
606 | to use a condition variable for the whole process. |
609 | |
607 | |
610 | Every call to C<< ->begin >> will increment a counter, and every call to |
608 | 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 |
609 | 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 |
610 | >>, 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 |
611 | 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. |
612 | callback was set, C<send> will be called without any arguments. |
615 | |
613 | |
616 | Let's clarify this with the ping example: |
614 | You can think of C<< $cv->send >> giving you an OR condition (one call |
|
|
615 | sends), while C<< $cv->begin >> and C<< $cv->end >> giving you an AND |
|
|
616 | condition (all C<begin> calls must be C<end>'ed before the condvar sends). |
|
|
617 | |
|
|
618 | Let's start with a simple example: you have two I/O watchers (for example, |
|
|
619 | STDOUT and STDERR for a program), and you want to wait for both streams to |
|
|
620 | close before activating a condvar: |
|
|
621 | |
|
|
622 | my $cv = AnyEvent->condvar; |
|
|
623 | |
|
|
624 | $cv->begin; # first watcher |
|
|
625 | my $w1 = AnyEvent->io (fh => $fh1, cb => sub { |
|
|
626 | defined sysread $fh1, my $buf, 4096 |
|
|
627 | or $cv->end; |
|
|
628 | }); |
|
|
629 | |
|
|
630 | $cv->begin; # second watcher |
|
|
631 | my $w2 = AnyEvent->io (fh => $fh2, cb => sub { |
|
|
632 | defined sysread $fh2, my $buf, 4096 |
|
|
633 | or $cv->end; |
|
|
634 | }); |
|
|
635 | |
|
|
636 | $cv->recv; |
|
|
637 | |
|
|
638 | This works because for every event source (EOF on file handle), there is |
|
|
639 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
|
|
640 | sending. |
|
|
641 | |
|
|
642 | The ping example mentioned above is slightly more complicated, as the |
|
|
643 | there are results to be passwd back, and the number of tasks that are |
|
|
644 | begung can potentially be zero: |
617 | |
645 | |
618 | my $cv = AnyEvent->condvar; |
646 | my $cv = AnyEvent->condvar; |
619 | |
647 | |
620 | my %result; |
648 | my %result; |
621 | $cv->begin (sub { $cv->send (\%result) }); |
649 | $cv->begin (sub { $cv->send (\%result) }); |
… | |
… | |
641 | loop, which serves two important purposes: first, it sets the callback |
669 | 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 |
670 | 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 |
671 | C<send> is called even when C<no> hosts are being pinged (the loop |
644 | doesn't execute once). |
672 | doesn't execute once). |
645 | |
673 | |
646 | This is the general pattern when you "fan out" into multiple subrequests: |
674 | 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> |
675 | 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 |
676 | 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>. |
677 | subrequest you start, call C<begin> and for each subrequest you finish, |
|
|
678 | call C<end>. |
650 | |
679 | |
651 | =back |
680 | =back |
652 | |
681 | |
653 | =head3 METHODS FOR CONSUMERS |
682 | =head3 METHODS FOR CONSUMERS |
654 | |
683 | |
… | |
… | |
939 | no warnings; |
968 | no warnings; |
940 | use strict qw(vars subs); |
969 | use strict qw(vars subs); |
941 | |
970 | |
942 | use Carp; |
971 | use Carp; |
943 | |
972 | |
944 | our $VERSION = 4.42; |
973 | our $VERSION = 4.8; |
945 | our $MODEL; |
974 | our $MODEL; |
946 | |
975 | |
947 | our $AUTOLOAD; |
976 | our $AUTOLOAD; |
948 | our @ISA; |
977 | our @ISA; |
949 | |
978 | |
… | |
… | |
1094 | # allow only one watcher per fd, so we dup it to get a different one). |
1123 | # allow only one watcher per fd, so we dup it to get a different one). |
1095 | sub _dupfh($$;$$) { |
1124 | sub _dupfh($$;$$) { |
1096 | my ($poll, $fh, $r, $w) = @_; |
1125 | my ($poll, $fh, $r, $w) = @_; |
1097 | |
1126 | |
1098 | # cygwin requires the fh mode to be matching, unix doesn't |
1127 | # cygwin requires the fh mode to be matching, unix doesn't |
1099 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1128 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") : ($w, ">"); |
1100 | : $poll eq "w" ? ($w, ">") |
|
|
1101 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
|
|
1102 | |
1129 | |
1103 | open my $fh2, "$mode&" . fileno $fh |
1130 | open my $fh2, "$mode&", $fh |
1104 | or die "cannot dup() filehandle: $!,"; |
1131 | or die "AnyEvent->io: cannot dup() filehandle in mode '$poll': $!,"; |
1105 | |
1132 | |
1106 | # we assume CLOEXEC is already set by perl in all important cases |
1133 | # we assume CLOEXEC is already set by perl in all important cases |
1107 | |
1134 | |
1108 | ($fh2, $rw) |
1135 | ($fh2, $rw) |
1109 | } |
1136 | } |
… | |
… | |
1441 | |
1468 | |
1442 | =item C<PERL_ANYEVENT_MAX_FORKS> |
1469 | =item C<PERL_ANYEVENT_MAX_FORKS> |
1443 | |
1470 | |
1444 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
1471 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
1445 | will create in parallel. |
1472 | will create in parallel. |
|
|
1473 | |
|
|
1474 | =item C<PERL_ANYEVENT_MAX_OUTSTANDING_DNS> |
|
|
1475 | |
|
|
1476 | The default value for the C<max_outstanding> parameter for the default DNS |
|
|
1477 | resolver - this is the maximum number of parallel DNS requests that are |
|
|
1478 | sent to the DNS server. |
|
|
1479 | |
|
|
1480 | =item C<PERL_ANYEVENT_RESOLV_CONF> |
|
|
1481 | |
|
|
1482 | The file to use instead of F</etc/resolv.conf> (or OS-specific |
|
|
1483 | configuration) in the default resolver. When set to the empty string, no |
|
|
1484 | default config will be used. |
|
|
1485 | |
|
|
1486 | =item C<PERL_ANYEVENT_CA_FILE>, C<PERL_ANYEVENT_CA_PATH>. |
|
|
1487 | |
|
|
1488 | When neither C<ca_file> nor C<ca_path> was specified during |
|
|
1489 | L<AnyEvent::TLS> context creation, and either of these environment |
|
|
1490 | variables exist, they will be used to specify CA certificate locations |
|
|
1491 | instead of a system-dependent default. |
1446 | |
1492 | |
1447 | =back |
1493 | =back |
1448 | |
1494 | |
1449 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1495 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1450 | |
1496 | |