ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Intro.pod
Revision: 1.2
Committed: Sat May 31 00:35:10 2008 UTC (16 years ago) by root
Branch: MAIN
Changes since 1.1: +374 -35 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 =head1 Introduction to AnyEvent
2 root 1.1
3 root 1.2 This is a tutorial that will introduce you to the features of AnyEvent.
4 root 1.1
5 root 1.2 The first part introduces the core AnyEvent module (after swamping you a
6     bit in evangelism), which might already provide all you ever need.
7 root 1.1
8 root 1.2 The second part focuses on network programming using sockets, for which
9     AnyEvent offers a lot of support you can use.
10    
11    
12     =head1 What is AnyEvent?
13    
14     Skip this section if you want to see code, now!
15    
16     AnyEvent is first of all just a framework to do event-based
17     programming. Typically such frameworks are an all-or-nothing thing: If you
18     use one such framework, you can't (easily, or even at all) use another in
19     the same program.
20    
21     AnyEvent is different - it is a thin abstraction layer above all kinds
22     of event loops. Its main purpose is to move the choice of the underlying
23     framework (the event loop) from the module author to the program author
24     using the module.
25    
26     That means you can write code that uses events to control what it
27     does, without forcing other code in the same program to use the same
28     underlying framework as you do - i.e. you can create a Perl module
29     that is event-based using AnyEvent, and users of that module can still
30     choose between using L<Gtk2>, L<Tk>, L<Event> or no event loop at
31     all: AnyEvent comes with its own event loop implementation, so your
32     code works regardless of other modules that might or might not be
33     installed. The latter is important, as AnyEvent does not have any
34     dependencies to other modules, which makes it easy to install, for
35     example, when you lack a C compiler.
36    
37     A typical problem with Perl modules such as L<Net::IRC> is that they
38     come with their own event loop: In L<Net::IRC>, the program who uses it
39     needs to start the event loop of L<Net::IRC>. That means that one cannot
40     integrate this module into a L<Gtk2> GUI for instance, as that module,
41     too, enforces the use of its own event loop (namely L<Glib>).
42 root 1.1
43     Another example is L<LWP>: it provides no event interface at all. It's a
44     pure blocking HTTP (and FTP etc.) client library, which usually means that
45     you either have to start a thread or have to fork for a HTTP request, or
46     use L<Coro::LWP>, if you want to do something else while waiting for the
47     request to finish.
48    
49     The motivation behind these designs is often that a module doesn't want to
50     depend on some complicated XS-module (Net::IRC), or that it doesn't want
51 root 1.2 to force the user to use some specific event loop at all (LWP).
52 root 1.1
53 root 1.2 L<AnyEvent> solves this dilemma, by B<not> forcing module authors to either
54 root 1.1
55     =over 4
56    
57 root 1.2 =item write their own event loop (because guarantees to offer one
58     everywhere - even on windows).
59 root 1.1
60 root 1.2 =item choose one fixed event loop (because AnyEvent works with all
61     important event loops available for Perl, and adding others is trivial).
62 root 1.1
63     =back
64    
65 root 1.2 If the module author uses L<AnyEvent> for all his event needs (IO events,
66     timers, signals, ...) then all other modules can just use his module and
67     don't have to choose an event loop or adapt to his event loop. The choice
68     of the event loop is ultimately made by the program author who uses all
69     the modules and writes the main program. And even there he doesn't have to
70     choose, he can just let L<AnyEvent> choose the best available event loop
71     for him.
72 root 1.1
73     Read more about this in the main documentation of the L<AnyEvent> module.
74    
75    
76 root 1.2 =head1 Introduction to Event-Based Programming
77    
78     So what exactly is programming using events? It quite simply means that
79     instead of your code actively waiting for something, such as the user
80     entering something on STDIN:
81    
82     $| = 1; print "enter your name> ";
83    
84     my $name = <STDIN>;
85    
86     You instead tell your event framework to notify you in the event of some
87     data being available on STDIN, by using a callback mechanism:
88    
89     use AnyEvent;
90    
91     $| = 1; print "enter your name> ";
92    
93     my $name;
94    
95     my $wait_for_input = AnyEvent->io (
96     fh => \*STDIN, # which file handle to check
97     poll => "r", # which event to wait for ("r"ead data)
98     cb => sub { # what callback to execute
99     $name = <STDIN>; # read it
100     }
101     );
102    
103     # do something else here
104    
105     Looks more complicated, and surely is, but the advantage of using events
106     is that your program can do something else instead of waiting for
107     input. Waiting as in the first example is also called "blocking" because
108     you "block" your process from executing anything else while you do so.
109    
110     The second example avoids blocking, by only registering interest in a read
111     event, which is fast and doesn't block your process. Only when read data
112     is available will the callback be called, which can then proceed to read
113     the data.
114    
115     The "interest" is represented by an object returned by C<< AnyEvent->io
116     >> called a "watcher" object - called like that because it "watches" your
117     file handle (or other event sources) for the event you are interested in.
118    
119     In the example above, we create an I/O watcher by calling the C<<
120     AnyEvent->io >> method. Disinterest in some event is simply expressed by
121     forgetting about the watcher, for example, by C<undef>'ing the variable it
122     is stored in. AnyEvent will automatically clean up the watcher if it is no
123     longer used, much like Perl closes your file handles if you no longer use
124     them anywhere.
125    
126     =head2 Condition Variables
127    
128     However, the above is not a fullly working program, and will not work
129     as-is. The reason is that your callback will not be invoked out of the
130     blue, you have to run the event loop. Also, event-based programs sometimes
131     have to block, too, as when there simply is nothing else to do and
132     everything waits for some events, it needs to block the process as well.
133    
134     In AnyEvent, this is done using condition variables. Condition variables
135     are named "condition variables" because they represent a condition that is
136     initially false and needs to be fulfilled.
137    
138     You can also call them mergepoints, syncpoints, rendezvous ports or even
139     callbacks and many other things (and they are often called like this in
140     other frameworks). The important point is that you can create them freely
141     and later wait for them to become true.
142    
143     Condition variables have two sides - one side is the "producer" of the
144     condition (whatever code detects the condition), the other side is the
145     "consumer" (the code that waits for that condition).
146    
147     In our example in the previous section, the producer is the event callback
148     and there is no consumer yet - let's change that now:
149    
150     use AnyEvent;
151    
152     $| = 1; print "enter your name> ";
153    
154     my $name;
155    
156     my $name_ready = AnyEvent->condvar;
157    
158     my $wait_for_input = AnyEvent->io (
159     fh => \*STDIN,
160     poll => "r",
161     cb => sub {
162     $name = <STDIN>;
163     $name_ready->send;
164     }
165     );
166    
167     # do something else here
168    
169     # now wait until the name is available:
170     $name_ready->recv;
171    
172     undef $wait_for_input; # watche rno longer needed
173    
174     print "your name is $name\n";
175    
176     This program creates an AnyEvent condvar by calling the C<<
177     AnyEvent->condvar >> method. It then creates a watcher as usual, but
178     inside the callback it C<send>'s the C<$name_ready> condition variable,
179     which causes anybody waiting on it to continue.
180    
181     The "anybody" in this case is the code that follows, which calls C<<
182     $name_ready->recv >>: The producer calls C<send>, the consumer calls
183     C<recv>.
184    
185     If there is no C<$name> available yet, then the call to C<<
186     $name_ready->recv >> will halt your program until the condition becomes
187     true.
188    
189     As the names C<send> and C<recv> imply, you can actually send and receive
190     data using this, for example, the above code could also be written like
191     this, without an extra variable to store the name in:
192    
193     use AnyEvent;
194    
195     $| = 1; print "enter your name> ";
196    
197     my $name_ready = AnyEvent->condvar;
198    
199     my $wait_for_input = AnyEvent->io (
200     fh => \*STDIN, poll => "r",
201     cb => sub { $name_ready->send (scalar = <STDIN>) }
202     );
203    
204     # do something else here
205    
206     # now wait and fetch the name
207     my $name = $name_ready->recv;
208    
209     undef $wait_for_input; # watche rno longer needed
210    
211     print "your name is $name\n";
212    
213     You can pass any number of arguments to C<send>, and everybody call to
214     C<recv> will return them.
215    
216     =head2 The "main loop"
217    
218     Most event-based frameworks have something called a "main loop" or "event
219     loop run function" or something similar.
220    
221     Just like in C<recv> AnyEvent, these functions need to be called
222     eventually so that your event loop has a chance of actually looking for
223     those events you are interested in.
224    
225     For example, in a L<Gtk2> program, the above example could also be written
226     like this:
227    
228     use Gtk2 -init;
229     use AnyEvent;
230    
231     ############################################
232     # create a window and some label
233    
234     my $window = new Gtk2::Window "toplevel";
235     $window->add (my $label = new Gtk2::Label "soon replaced by name");
236    
237     $window->show_all;
238    
239     ############################################
240     # do our AnyEvent stuff
241    
242     $| = 1; print "enter your name> ";
243    
244     my $name_ready = AnyEvent->condvar;
245    
246     my $wait_for_input = AnyEvent->io (
247     fh => \*STDIN, poll => "r",
248     cb => sub {
249     # set the label
250     $label->set_text (scalar <STDIN>);
251     print "enter another name> ";
252     }
253     );
254    
255     ############################################
256     # Now enter Gtk2's event loop
257    
258     main Gtk2;
259    
260     No condition variable anywhere in sight - instead, we just read a line
261     from STDIN and replace the text in the label. In fact, since nobody
262     C<undef>'s C<$wait_for_input> you can enter multiple lines.
263    
264     Instead of waiting for a condition variable, the program enters the Gtk2
265     main loop by calling C<< Gtk2->main >>, which will block the program and
266     wait for events to arrive.
267    
268     This also shows that AnyEvent is quite flexible - you didn't have anything
269     to do to make the AnyEvent watcher use Gtk2 (actually Glib) - it just
270     worked.
271    
272     Admittedly, the example is a bit silly - who would want to read names
273     form standard input in a Gtk+ application. But imagine that instead of
274     doing that, you would make a HTTP request in the background and display
275     it's results. In fact, with event-based programming you can make many
276     http-requests in parallel in your program and still provide feedback to
277     the user and stay interactive.
278    
279     In the next part you will see how to do just that - by implementing an
280     HTTP request, on our own, with the utility modules AnyEvent comes with.
281    
282     Before that, however, lets briefly look at how you would write your
283     program with using only AnyEvent, without ever calling some other event
284     loop's run function.
285    
286     In the example using condition variables, we used that, and in fact, this
287     is the solution:
288    
289     my $quit_program = AnyEvent->condvar;
290    
291     # create AnyEvent watchers (or not) here
292    
293     $quit_program->recv;
294    
295     If any of your watcher callbacks decide to quit, they can simply call
296     C<< $quit_program->send >>. Of course, they could also decide not to and
297     simply call C<exit> instead, or they could decide not to quit, ever (e.g.
298     in a long-running daemon program).
299    
300     In that case, you can simply use:
301    
302     AnyEvent->condvar->recv;
303    
304     And this is, in fact, closest to the idea of a main loop run function that
305     AnyEvent offers.
306    
307     =head2 Timers and other event sources
308    
309     So far, we have only used I/O watchers. These are useful mainly to find
310     out wether a Socket has data to read, or space to write more data. On sane
311     operating systems this also works for console windows/terminals (typically
312     on standard input), serial lines, all sorts of other devices, basically
313     almost everything that has a file descriptor but isn't a file itself. (As
314     usual, "sane" excludes windows - on that platform you would need different
315     functions for all of these, complicating code immesely - think "socket
316     only" on windows).
317    
318     However, I/O is not everything - the secondmost important event source is
319     the clock. For example when doing an HTTP request you might want to time
320     out when the server doesn't answre within some predefined amount of time.
321    
322     In AnyEvent, timer event watchers are created by calling the C<<
323     AnyEvent->timer >> method:
324    
325     use AnyEvent;
326    
327     my $cv = AnyEvent->condvar;
328    
329     my $wait_one_and_a_half_seconds = AnyEvent->timer (
330     after => 1.5, # after how many seconds to invoke the cb?
331     cb => sub { # the callback to invoke
332     $cv->send;
333     },
334     );
335    
336     # can do somehting else here
337    
338     # now wait till our time has come
339     $cv->recv;
340    
341     Unlike I/O watchers, timers are only interested in the amount of seconds
342     they have to wait. When that amount of time has passed, AnyEvent will
343     invoke your callback.
344    
345     Unlike I/O watchers, which will call your callback as many times as there
346     is data available, timers are one-shot: after they have "fired" once and
347     invoked your callback, they are dead and no longer do anything.
348    
349     To get a repeating timer, such as a timer firing roughly once per second,
350     you have to recreate it:
351    
352     use AnyEvent;
353    
354     my $time_watcher;
355    
356     sub once_per_second {
357     print "tick\n";
358    
359     # (re-)create the watcher
360     $time_watcher = AnyEvent->timer (
361     after => 1,
362     cb => \&once_per_second,
363     );
364     }
365    
366     # now start the timer
367     once_per_second;
368    
369     Having to recreate your timer is a restriction put on AnyEvent that is
370     present in most event libraries it uses. It is so annoying that some
371     future version might worka round this limitation, but right now, it's the
372     only way to do repeating timers.
373    
374     Fortunately most timers aren't really repeating but specify timeouts of
375     some sort.
376    
377     =head3 More esoteric sources
378    
379     AnyEvent also has some other, more esoteric event sources you can tap
380     into: signal and child watchers.
381    
382     Signal watchers can be used to wait for "signal events", which simply
383     means your process got send a signal (Such as C<SIGTERM> or C<SIGUSR1>).
384    
385     Process watchers wait for a child process to exit. They are useful when
386     you fork a separate process and ened to know when it exits, but you do not
387     wait for that by blocking.
388    
389     Both watcher types are described in detail in the main L<AnyEvent> manual
390     page.
391    
392    
393     =head1 Network programming and AnyEvent
394    
395     AnyEvent is not just a simple abstraction anymore. While the core
396 root 1.1 L<AnyEvent> module is still small and self-contained, the distribution
397     comes with some very useful utility modules such as L<AnyEvent::Handle>,
398     L<AnyEvent::DNS> and L<AnyEvent::Socket>. These can make your life as
399     non-blocking network programmer a lot easier.
400    
401     Here is an introduction into these three submodules:
402    
403 root 1.2 =head2 L<AnyEvent::Handle>
404 root 1.1
405     This module handles non-blocking IO on file handles in an event based
406     manner. It provides a wrapper object around your file handle that provides
407     queueing and buffering of incoming and outgoing data for you.
408    
409     More about this later.
410    
411 root 1.2 =head2 L<AnyEvent::Socket>
412 root 1.1
413     This module provides you with functions that handle socket creation
414     and IP address magic. The two main functions are C<tcp_connect> and
415     C<tcp_server>. The former will connect a (streaming) socket to an internet
416     host for you and the later will make a server socket for you, to accept
417     connections.
418    
419     This module also comes with transparent IPv6 support, this means: If you
420     write your programs with this module, you will be IPv6 ready without doing
421     anything further.
422    
423     It also works around a lot of portability quirks (especially on the
424     windows platform), which makes it even easier to write your programs in a
425     portable way.
426    
427 root 1.2 =head2 L<AnyEvent::DNS>
428 root 1.1
429     This module allows fully asynchronous DNS resolution. It is used mainly
430     by L<AnyEvent::Socket> to resolve hostnames and service ports, but is a
431     great way to do other DNS resolution tasks, such as reverse lookups of IP
432     addresses for log files.
433    
434     =head2 First experiments with AnyEvent::Handle
435    
436     Now let's start with something simple: a program that reads from standard
437     input in a non-blocking way, that is, in a way that lets your program do
438     other things while it is waiting for input.
439    
440     First, the full program listing:
441    
442     #!/usr/bin/perl
443    
444     use AnyEvent;
445     use AnyEvent::Handle;
446    
447     my $end_prog = AnyEvent->condvar;
448    
449     my $handle =
450     AnyEvent::Handle->new (
451     fh => \*STDIN,
452     on_eof => sub {
453     print "received EOF, exiting...\n";
454     $end_prog->broadcast;
455     },
456     on_error => sub {
457     print "error while reading from STDIN: $!\n";
458     $end_prog->broadcast;
459     }
460     );
461    
462     $handle->push_read (sub {
463     my ($handle) = @_;
464    
465     if ($handle->rbuf =~ s/^.*?\bend\b.*$//s) {
466     print "got 'end', existing...\n";
467     $end_prog->broadcast;
468     return 1
469     }
470    
471     0
472     });
473    
474     $end_prog->recv;
475    
476     That's a mouthful, so lets go through it step by step:
477    
478     #!/usr/bin/perl
479    
480     use AnyEvent;
481     use AnyEvent::Handle;
482    
483     Nothing unexpected here, just load AnyEvent for the event functionality
484     and AnyEvent::Handle for your file handling needs.
485    
486     my $end_prog = AnyEvent->condvar;
487    
488     Here the program creates a so-called 'condition variable': Condition
489     variables are a great way to signal the completion of some event, or to
490     state that some condition became true (thus the name).
491    
492     This condition variable represents the condition that the program wants to
493     terminate. Later in the progra, we will 'recv' that condition (call the
494     C<recv> method on it), which will wait until the condition gets signalled
495     (which is done by calling the C<send> method on it).
496    
497     The next step is to create the handle object:
498    
499     my $handle =
500     AnyEvent::Handle->new (
501     fh => \*STDIN,
502     on_eof => sub {
503     print "received EOF, exiting...\n";
504     $end_prog->broadcast;
505     },
506    
507     This handle object will read from standard input. Setting the C<on_eof>
508     callback should be done for every file handle, as that is a condition that
509     we always need to check for when working with file handles, to prevent
510     reading or writing to a closed file handle, or getting stuck indefinitely
511     in case of an error.
512    
513     Speaking of errors:
514    
515     on_error => sub {
516     print "error while reading from STDIN: $!\n";
517     $end_prog->broadcast;
518     }
519     );
520    
521     The C<on_error> callback is also not required, but we set it here in case
522     any error happens when we read from the file handle. It is usually a good
523     idea to set this callback and at least print some diagnostic message: Even
524     in our small example an error can happen. More on this later...
525    
526     $handle->push_read (sub {
527    
528     Next we push a general read callback on the read queue, which
529     will wait until we have received all the data we wanted to
530     receive. L<AnyEvent::Handle> has two queues per file handle, a read and a
531     write queue. The write queue queues pending data that waits to be written
532     to the file handle. And the read queue queues reading callbacks. For more
533     details see the documentation L<AnyEvent::Handle> about the READ QUEUE and
534     WRITE QUEUE.
535    
536     my ($handle) = @_;
537    
538     if ($handle->rbuf =~ s/^.*?\bend\b.*$//s) {
539     print "got 'end', existing...\n";
540     $end_prog->broadcast;
541     return 1
542     }
543    
544     0
545     });
546    
547     The actual callback waits until the word 'end' has been seen in the data
548     received on standard input. Once we encounter the stop word 'end' we
549     remove everything from the read buffer and call the condition variable
550     we setup earlier, that signals our 'end of program' condition. And the
551     callback returns with a true value, that signals we are done with reading
552     all the data we were interested in (all data until the word 'end' has been
553     seen).
554    
555     In all other cases, when the stop word has not been seen yet, we just
556     return a false value, to indicate that we are not finished yet.
557    
558     The C<rbuf> method returns our read buffer, that we can directly modify as
559     lvalue. Alternatively we also could have written:
560    
561     if ($handle->{rbuf} =~ s/^.*?\bend\b.*$//s) {
562    
563     The last line will wait for the condition that our program wants to exit:
564    
565     $end_prog->recv;
566    
567     The call to C<recv> will setup an event loop for us and wait for IO, timer
568     or signal events and will handle them until the condition gets sent (by
569     calling its C<send> method).
570    
571     The key points to learn from this example are:
572    
573     =over 4
574    
575     =item * Condition variables are used to start an event loop.
576    
577     =item * How to registering some basic callbacks on AnyEvent::Handle's.
578    
579     =item * How to process data in the read buffer.
580    
581     =back
582