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

Comparing AnyEvent/lib/AnyEvent/Intro.pod (file contents):
Revision 1.26 by root, Thu Dec 24 10:07:06 2009 UTC vs.
Revision 1.27 by root, Fri Jun 18 14:28:50 2010 UTC

23AnyEvent is first of all just a framework to do event-based 23AnyEvent is first of all just a framework to do event-based
24programming. Typically such frameworks are an all-or-nothing thing: If you 24programming. Typically such frameworks are an all-or-nothing thing: If you
25use one such framework, you can't (easily, or even at all) use another in 25use one such framework, you can't (easily, or even at all) use another in
26the same program. 26the same program.
27 27
28AnyEvent is different - it is a thin abstraction layer on top of other of 28AnyEvent is different - it is a thin abstraction layer on top of other
29event loops, just like DBI is an abstraction of many different database 29event loops, just like DBI is an abstraction of many different database
30APIs. Its main purpose is to move the choice of the underlying framework 30APIs. Its main purpose is to move the choice of the underlying framework
31(the event loop) from the module author to the program author using the 31(the event loop) from the module author to the program author using the
32module. 32module.
33 33
124Waiting as done in the first example is also called "blocking" the process 124Waiting as done in the first example is also called "blocking" the process
125because you "block"/keep your process from executing anything else while 125because you "block"/keep your process from executing anything else while
126you do so. 126you do so.
127 127
128The second example avoids blocking by only registering interest in a read 128The second example avoids blocking by only registering interest in a read
129event, which is fast and doesn't block your process. Only when read data 129event, which is fast and doesn't block your process. Only when data is
130is available will the callback be called, which can then proceed to read 130available for reading will the callback be called, which can then proceed
131the data. 131to read the data.
132 132
133The "interest" is represented by an object returned by C<< AnyEvent->io 133The "interest" is represented by an object returned by C<< AnyEvent->io
134>> called a "watcher" object - called like that because it "watches" your 134>> called a "watcher" object - called this because it "watches" your
135file handle (or other event sources) for the event you are interested in. 135file handle (or other event sources) for the event you are interested in.
136 136
137In the example above, we create an I/O watcher by calling the C<< 137In the example above, we create an I/O watcher by calling the C<<
138AnyEvent->io >> method. Disinterest in some event is simply expressed 138AnyEvent->io >> method. Disinterest in some event is simply expressed
139by forgetting about the watcher, for example, by C<undef>'ing the only 139by forgetting about the watcher, for example, by C<undef>'ing the only
218In AnyEvent, this is done using condition variables. Condition variables 218In AnyEvent, this is done using condition variables. Condition variables
219are named "condition variables" because they represent a condition that is 219are named "condition variables" because they represent a condition that is
220initially false and needs to be fulfilled. 220initially false and needs to be fulfilled.
221 221
222You can also call them "merge points", "sync points", "rendezvous ports" 222You can also call them "merge points", "sync points", "rendezvous ports"
223or even callbacks and many other things (and they are often called like 223or even callbacks and many other things (and they are often called these
224this in other frameworks). The important point is that you can create them 224names in other frameworks). The important point is that you can create them
225freely and later wait for them to become true. 225freely and later wait for them to become true.
226 226
227Condition variables have two sides - one side is the "producer" of the 227Condition variables have two sides - one side is the "producer" of the
228condition (whatever code detects and flags the condition), the other side 228condition (whatever code detects and flags the condition), the other side
229is the "consumer" (the code that waits for that condition). 229is the "consumer" (the code that waits for that condition).
347 347
348Instead of waiting for a condition variable, the program enters the Gtk2 348Instead of waiting for a condition variable, the program enters the Gtk2
349main loop by calling C<< Gtk2->main >>, which will block the program and 349main loop by calling C<< Gtk2->main >>, which will block the program and
350wait for events to arrive. 350wait for events to arrive.
351 351
352This also shows that AnyEvent is quite flexible - you didn't have anything 352This also shows that AnyEvent is quite flexible - you didn't have to do
353to do to make the AnyEvent watcher use Gtk2 (actually Glib) - it just 353anything to make the AnyEvent watcher use Gtk2 (actually Glib) - it just
354worked. 354worked.
355 355
356Admittedly, the example is a bit silly - who would want to read names 356Admittedly, the example is a bit silly - who would want to read names
357from standard input in a Gtk+ application. But imagine that instead of 357from standard input in a Gtk+ application. But imagine that instead of
358doing that, you would make a HTTP request in the background and display 358doing that, you would make a HTTP request in the background and display
359it's results. In fact, with event-based programming you can make many 359it's results. In fact, with event-based programming you can make many
360http-requests in parallel in your program and still provide feedback to 360HTTP requests in parallel in your program and still provide feedback to
361the user and stay interactive. 361the user and stay interactive.
362 362
363And in the next part you will see how to do just that - by implementing an 363And in the next part you will see how to do just that - by implementing an
364HTTP request, on our own, with the utility modules AnyEvent comes with. 364HTTP request, on our own, with the utility modules AnyEvent comes with.
365 365
366Before that, however, let's briefly look at how you would write your 366Before that, however, let's briefly look at how you would write your
367program with using only AnyEvent, without ever calling some other event 367program using only AnyEvent, without ever calling some other event
368loop's run function. 368loop's run function.
369 369
370In the example using condition variables, we used those to start waiting 370In the example using condition variables, we used those to start waiting
371for events, and in fact, condition variables are the solution: 371for events, and in fact, condition variables are the solution:
372 372

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines