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.325 by root, Thu May 20 23:56:04 2010 UTC vs.
Revision 1.326 by root, Sun Jun 6 10:11:56 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines