ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.317 by root, Wed Mar 24 21:22:57 2010 UTC vs.
Revision 1.325 by root, Thu May 20 23:56:04 2010 UTC

7 7
8=head1 SYNOPSIS 8=head1 SYNOPSIS
9 9
10 use AnyEvent; 10 use AnyEvent;
11 11
12 # if you prefer function calls, look at the AE manpage for
13 # an alternative API.
14
12 # file descriptor readable 15 # file handle or descriptor readable
13 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); 16 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
14 17
15 # one-shot or repeating timers 18 # one-shot or repeating timers
16 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); 19 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
17 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... 20 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
606eventually calls C<< -> send >>, and the "consumer side", which waits 609eventually calls C<< -> send >>, and the "consumer side", which waits
607for the send to occur. 610for the send to occur.
608 611
609Example: wait for a timer. 612Example: wait for a timer.
610 613
611 # wait till the result is ready 614 # condition: "wait till the timer is fired"
612 my $result_ready = AnyEvent->condvar; 615 my $timer_fired = AnyEvent->condvar;
613 616
614 # do something such as adding a timer 617 # create the timer - we could wait for, say
615 # or socket watcher the calls $result_ready->send 618 # a handle becomign ready, or even an
616 # when the "result" is ready. 619 # AnyEvent::HTTP request to finish, but
617 # in this case, we simply use a timer: 620 # in this case, we simply use a timer:
618 my $w = AnyEvent->timer ( 621 my $w = AnyEvent->timer (
619 after => 1, 622 after => 1,
620 cb => sub { $result_ready->send }, 623 cb => sub { $timer_fired->send },
621 ); 624 );
622 625
623 # this "blocks" (while handling events) till the callback 626 # this "blocks" (while handling events) till the callback
624 # calls ->send 627 # calls ->send
625 $result_ready->recv; 628 $timer_fired->recv;
626 629
627Example: wait for a timer, but take advantage of the fact that condition 630Example: wait for a timer, but take advantage of the fact that condition
628variables are also callable directly. 631variables are also callable directly.
629 632
630 my $done = AnyEvent->condvar; 633 my $done = AnyEvent->condvar;
1054=head1 OTHER MODULES 1057=head1 OTHER MODULES
1055 1058
1056The following is a non-exhaustive list of additional modules that use 1059The following is a non-exhaustive list of additional modules that use
1057AnyEvent as a client and can therefore be mixed easily with other AnyEvent 1060AnyEvent as a client and can therefore be mixed easily with other AnyEvent
1058modules and other event loops in the same program. Some of the modules 1061modules and other event loops in the same program. Some of the modules
1059come with AnyEvent, most are available via CPAN. 1062come as part of AnyEvent, the others are available via CPAN.
1060 1063
1061=over 4 1064=over 4
1062 1065
1063=item L<AnyEvent::Util> 1066=item L<AnyEvent::Util>
1064 1067
1079 1082
1080=item L<AnyEvent::DNS> 1083=item L<AnyEvent::DNS>
1081 1084
1082Provides rich asynchronous DNS resolver capabilities. 1085Provides rich asynchronous DNS resolver capabilities.
1083 1086
1087=item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP>
1088
1089Implement event-based interfaces to the protocols of the same name (for
1090the curious, IGS is the International Go Server and FCP is the Freenet
1091Client Protocol).
1092
1093=item L<AnyEvent::Handle::UDP>
1094
1095Here be danger!
1096
1097As Pauli would put it, "Not only is it not right, it's not even wrong!" -
1098there are so many things wrong with AnyEvent::Handle::UDP, most notably
1099it's use of a stream-based API with a protocol that isn't streamable, that
1100the only way to improve it is to delete it.
1101
1102It features data corruption (but typically only under load) and general
1103confusion. On top, the author is not only clueless about UDP but also
1104fact-resistant - some gems of his understanding: "connect doesn't work
1105with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not
1106packets", "I don't need to implement proper error checking as UDP doesn't
1107support error checking" and so on - he doesn't even understand what's
1108wrong with his module when it is explained to him.
1109
1084=item L<AnyEvent::HTTP> 1110=item L<AnyEvent::DBI>
1085 1111
1086A simple-to-use HTTP library that is capable of making a lot of concurrent 1112Executes L<DBI> requests asynchronously in a proxy process for you,
1087HTTP requests. 1113notifying you in an event-bnased way when the operation is finished.
1114
1115=item L<AnyEvent::AIO>
1116
1117Truly asynchronous (as opposed to non-blocking) I/O, should be in the
1118toolbox of every event programmer. AnyEvent::AIO transparently fuses
1119L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based
1120file I/O, and much more.
1088 1121
1089=item L<AnyEvent::HTTPD> 1122=item L<AnyEvent::HTTPD>
1090 1123
1091Provides a simple web application server framework. 1124A simple embedded webserver.
1092 1125
1093=item L<AnyEvent::FastPing> 1126=item L<AnyEvent::FastPing>
1094 1127
1095The fastest ping in the west. 1128The fastest ping in the west.
1096
1097=item L<AnyEvent::DBI>
1098
1099Executes L<DBI> requests asynchronously in a proxy process.
1100
1101=item L<AnyEvent::AIO>
1102
1103Truly asynchronous I/O, should be in the toolbox of every event
1104programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent
1105together.
1106
1107=item L<AnyEvent::BDB>
1108
1109Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses
1110L<BDB> and AnyEvent together.
1111
1112=item L<AnyEvent::GPSD>
1113
1114A non-blocking interface to gpsd, a daemon delivering GPS information.
1115
1116=item L<AnyEvent::IRC>
1117
1118AnyEvent based IRC client module family (replacing the older Net::IRC3).
1119
1120=item L<AnyEvent::XMPP>
1121
1122AnyEvent based XMPP (Jabber protocol) module family (replacing the older
1123Net::XMPP2>.
1124
1125=item L<AnyEvent::IGS>
1126
1127A non-blocking interface to the Internet Go Server protocol (used by
1128L<App::IGS>).
1129
1130=item L<Net::FCP>
1131
1132AnyEvent-based implementation of the Freenet Client Protocol, birthplace
1133of AnyEvent.
1134
1135=item L<Event::ExecFlow>
1136
1137High level API for event-based execution flow control.
1138 1129
1139=item L<Coro> 1130=item L<Coro>
1140 1131
1141Has special support for AnyEvent via L<Coro::AnyEvent>. 1132Has special support for AnyEvent via L<Coro::AnyEvent>.
1142 1133
1156 1147
1157BEGIN { AnyEvent::common_sense } 1148BEGIN { AnyEvent::common_sense }
1158 1149
1159use Carp (); 1150use Carp ();
1160 1151
1161our $VERSION = '5.251'; 1152our $VERSION = '5.261';
1162our $MODEL; 1153our $MODEL;
1163 1154
1164our $AUTOLOAD; 1155our $AUTOLOAD;
1165our @ISA; 1156our @ISA;
1166 1157
1342 1333
1343=head1 SIMPLIFIED AE API 1334=head1 SIMPLIFIED AE API
1344 1335
1345Starting with version 5.0, AnyEvent officially supports a second, much 1336Starting with version 5.0, AnyEvent officially supports a second, much
1346simpler, API that is designed to reduce the calling, typing and memory 1337simpler, API that is designed to reduce the calling, typing and memory
1347overhead. 1338overhead by using function call syntax and a fixed number of parameters.
1348 1339
1349See the L<AE> manpage for details. 1340See the L<AE> manpage for details.
1350 1341
1351=cut 1342=cut
1352 1343
1353package AE; 1344package AE;
1354 1345
1355our $VERSION = $AnyEvent::VERSION; 1346our $VERSION = $AnyEvent::VERSION;
1347
1348# fall back to the main API by default - backends and AnyEvent::Base
1349# implementations can overwrite these.
1356 1350
1357sub io($$$) { 1351sub io($$$) {
1358 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) 1352 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1359} 1353}
1360 1354
2075 2069
2076The actual code goes further and collects all errors (C<die>s, exceptions) 2070The actual code goes further and collects all errors (C<die>s, exceptions)
2077that occurred during request processing. The C<result> method detects 2071that occurred during request processing. The C<result> method detects
2078whether an exception as thrown (it is stored inside the $txn object) 2072whether an exception as thrown (it is stored inside the $txn object)
2079and just throws the exception, which means connection errors and other 2073and just throws the exception, which means connection errors and other
2080problems get reported tot he code that tries to use the result, not in a 2074problems get reported to the code that tries to use the result, not in a
2081random callback. 2075random callback.
2082 2076
2083All of this enables the following usage styles: 2077All of this enables the following usage styles:
2084 2078
20851. Blocking: 20791. Blocking:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines