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.320 by root, Mon Apr 12 02:50:31 2010 UTC vs.
Revision 1.326 by root, Sun Jun 6 10:11:56 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 L<AE> manpage for 12 # if you prefer function calls, look at the AE manpage for
13 # an alternative API. 13 # an alternative API.
14 14
15 # file handle or descriptor readable 15 # file handle or descriptor readable
16 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); 16 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
17 17
556will actively watch for new events and call your callbacks. 556will actively watch for new events and call your callbacks.
557 557
558AnyEvent is slightly different: it expects somebody else to run the event 558AnyEvent is slightly different: it expects somebody else to run the event
559loop and will only block when necessary (usually when told by the user). 559loop and will only block when necessary (usually when told by the user).
560 560
561The instrument to do that is called a "condition variable", so called 561The tool to do that is called a "condition variable", so called because
562because they represent a condition that must become true. 562they represent a condition that must become true.
563 563
564Now is probably a good time to look at the examples further below. 564Now is probably a good time to look at the examples further below.
565 565
566Condition variables can be created by calling the C<< AnyEvent->condvar 566Condition variables can be created by calling the C<< AnyEvent->condvar
567>> method, usually without arguments. The only argument pair allowed is 567>> method, usually without arguments. The only argument pair allowed is
572After creation, the condition variable is "false" until it becomes "true" 572After creation, the condition variable is "false" until it becomes "true"
573by calling the C<send> method (or calling the condition variable as if it 573by calling the C<send> method (or calling the condition variable as if it
574were a callback, read about the caveats in the description for the C<< 574were a callback, read about the caveats in the description for the C<<
575->send >> method). 575->send >> method).
576 576
577Condition variables are similar to callbacks, except that you can 577Since condition variables are the most complex part of the AnyEvent API, here are
578optionally wait for them. They can also be called merge points - points 578some different mental models of what they are - pick the ones you can connect to:
579in time where multiple outstanding events have been processed. And yet 579
580another way to call them is transactions - each condition variable can be 580=over 4
581used to represent a transaction, which finishes at some point and delivers 581
582a result. And yet some people know them as "futures" - a promise to 582=item * Condition variables are like callbacks - you can call them (and pass them instead
583compute/deliver something that you can wait for. 583of callbacks). Unlike callbacks however, you can also wait for them to be called.
584
585=item * Condition variables are signals - one side can emit or send them,
586the other side can wait for them, or install a handler that is called when
587the signal fires.
588
589=item * Condition variables are like "Merge Points" - points in your program
590where you merge multiple independent results/control flows into one.
591
592=item * Condition variables represent a transaction - function that start
593some kind of transaction can return them, leaving the caller the choice
594between waiting in a blocking fashion, or setting a callback.
595
596=item * Condition variables represent future values, or promises to deliver
597some result, long before the result is available.
598
599=back
584 600
585Condition variables are very useful to signal that something has finished, 601Condition variables are very useful to signal that something has finished,
586for example, if you write a module that does asynchronous http requests, 602for example, if you write a module that does asynchronous http requests,
587then a condition variable would be the ideal candidate to signal the 603then a condition variable would be the ideal candidate to signal the
588availability of results. The user can either act when the callback is 604availability of results. The user can either act when the callback is
1057=head1 OTHER MODULES 1073=head1 OTHER MODULES
1058 1074
1059The following is a non-exhaustive list of additional modules that use 1075The following is a non-exhaustive list of additional modules that use
1060AnyEvent as a client and can therefore be mixed easily with other AnyEvent 1076AnyEvent as a client and can therefore be mixed easily with other AnyEvent
1061modules and other event loops in the same program. Some of the modules 1077modules and other event loops in the same program. Some of the modules
1062come with AnyEvent, most are available via CPAN. 1078come as part of AnyEvent, the others are available via CPAN.
1063 1079
1064=over 4 1080=over 4
1065 1081
1066=item L<AnyEvent::Util> 1082=item L<AnyEvent::Util>
1067 1083
1082 1098
1083=item L<AnyEvent::DNS> 1099=item L<AnyEvent::DNS>
1084 1100
1085Provides rich asynchronous DNS resolver capabilities. 1101Provides rich asynchronous DNS resolver capabilities.
1086 1102
1103=item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP>
1104
1105Implement event-based interfaces to the protocols of the same name (for
1106the curious, IGS is the International Go Server and FCP is the Freenet
1107Client Protocol).
1108
1109=item L<AnyEvent::Handle::UDP>
1110
1111Here be danger!
1112
1113As Pauli would put it, "Not only is it not right, it's not even wrong!" -
1114there are so many things wrong with AnyEvent::Handle::UDP, most notably
1115it's use of a stream-based API with a protocol that isn't streamable, that
1116the only way to improve it is to delete it.
1117
1118It features data corruption (but typically only under load) and general
1119confusion. On top, the author is not only clueless about UDP but also
1120fact-resistant - some gems of his understanding: "connect doesn't work
1121with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not
1122packets", "I don't need to implement proper error checking as UDP doesn't
1123support error checking" and so on - he doesn't even understand what's
1124wrong with his module when it is explained to him.
1125
1087=item L<AnyEvent::HTTP> 1126=item L<AnyEvent::DBI>
1088 1127
1089A simple-to-use HTTP library that is capable of making a lot of concurrent 1128Executes L<DBI> requests asynchronously in a proxy process for you,
1090HTTP requests. 1129notifying you in an event-bnased way when the operation is finished.
1130
1131=item L<AnyEvent::AIO>
1132
1133Truly asynchronous (as opposed to non-blocking) I/O, should be in the
1134toolbox of every event programmer. AnyEvent::AIO transparently fuses
1135L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based
1136file I/O, and much more.
1091 1137
1092=item L<AnyEvent::HTTPD> 1138=item L<AnyEvent::HTTPD>
1093 1139
1094Provides a simple web application server framework. 1140A simple embedded webserver.
1095 1141
1096=item L<AnyEvent::FastPing> 1142=item L<AnyEvent::FastPing>
1097 1143
1098The fastest ping in the west. 1144The fastest ping in the west.
1099
1100=item L<AnyEvent::DBI>
1101
1102Executes L<DBI> requests asynchronously in a proxy process.
1103
1104=item L<AnyEvent::AIO>
1105
1106Truly asynchronous I/O, should be in the toolbox of every event
1107programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent
1108together.
1109
1110=item L<AnyEvent::BDB>
1111
1112Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses
1113L<BDB> and AnyEvent together.
1114
1115=item L<AnyEvent::GPSD>
1116
1117A non-blocking interface to gpsd, a daemon delivering GPS information.
1118
1119=item L<AnyEvent::IRC>
1120
1121AnyEvent based IRC client module family (replacing the older Net::IRC3).
1122
1123=item L<AnyEvent::XMPP>
1124
1125AnyEvent based XMPP (Jabber protocol) module family (replacing the older
1126Net::XMPP2>.
1127
1128=item L<AnyEvent::IGS>
1129
1130A non-blocking interface to the Internet Go Server protocol (used by
1131L<App::IGS>).
1132
1133=item L<Net::FCP>
1134
1135AnyEvent-based implementation of the Freenet Client Protocol, birthplace
1136of AnyEvent.
1137
1138=item L<Event::ExecFlow>
1139
1140High level API for event-based execution flow control.
1141 1145
1142=item L<Coro> 1146=item L<Coro>
1143 1147
1144Has special support for AnyEvent via L<Coro::AnyEvent>. 1148Has special support for AnyEvent via L<Coro::AnyEvent>.
1145 1149
1159 1163
1160BEGIN { AnyEvent::common_sense } 1164BEGIN { AnyEvent::common_sense }
1161 1165
1162use Carp (); 1166use Carp ();
1163 1167
1164our $VERSION = '5.26'; 1168our $VERSION = '5.261';
1165our $MODEL; 1169our $MODEL;
1166 1170
1167our $AUTOLOAD; 1171our $AUTOLOAD;
1168our @ISA; 1172our @ISA;
1169 1173

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines