ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/README
Revision: 1.19
Committed: Mon Apr 28 08:02:14 2008 UTC (16 years ago) by root
Branch: MAIN
CVS Tags: rel-3_3
Changes since 1.18: +368 -22 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.2 NAME
2     AnyEvent - provide framework for multiple event loops
3    
4 root 1.19 EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib, Qt, POE -
5 root 1.18 various supported event loops
6 root 1.2
7     SYNOPSIS
8 root 1.4 use AnyEvent;
9 root 1.2
10 root 1.6 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub {
11 root 1.2 ...
12     });
13 root 1.3
14     my $w = AnyEvent->timer (after => $seconds, cb => sub {
15 root 1.2 ...
16     });
17    
18 root 1.16 my $w = AnyEvent->condvar; # stores whether a condition was flagged
19 root 1.6 $w->wait; # enters "main loop" till $condvar gets ->broadcast
20 root 1.3 $w->broadcast; # wake up current and all future wait's
21    
22 root 1.14 WHY YOU SHOULD USE THIS MODULE (OR NOT)
23     Glib, POE, IO::Async, Event... CPAN offers event models by the dozen
24     nowadays. So what is different about AnyEvent?
25    
26     Executive Summary: AnyEvent is *compatible*, AnyEvent is *free of
27     policy* and AnyEvent is *small and efficient*.
28    
29     First and foremost, *AnyEvent is not an event model* itself, it only
30     interfaces to whatever event model the main program happens to use in a
31     pragmatic way. For event models and certain classes of immortals alike,
32 root 1.16 the statement "there can only be one" is a bitter reality: In general,
33     only one event loop can be active at the same time in a process.
34     AnyEvent helps hiding the differences between those event loops.
35 root 1.14
36     The goal of AnyEvent is to offer module authors the ability to do event
37     programming (waiting for I/O or timer events) without subscribing to a
38     religion, a way of living, and most importantly: without forcing your
39     module users into the same thing by forcing them to use the same event
40     model you use.
41    
42 root 1.16 For modules like POE or IO::Async (which is a total misnomer as it is
43     actually doing all I/O *synchronously*...), using them in your module is
44     like joining a cult: After you joined, you are dependent on them and you
45     cannot use anything else, as it is simply incompatible to everything
46     that isn't itself. What's worse, all the potential users of your module
47     are *also* forced to use the same event loop you use.
48    
49     AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works
50     fine. AnyEvent + Tk works fine etc. etc. but none of these work together
51     with the rest: POE + IO::Async? no go. Tk + Event? no go. Again: if your
52     module uses one of those, every user of your module has to use it, too.
53     But if your module uses AnyEvent, it works transparently with all event
54     models it supports (including stuff like POE and IO::Async, as long as
55     those use one of the supported event loops. It is trivial to add new
56     event loops to AnyEvent, too, so it is future-proof).
57 root 1.14
58 root 1.16 In addition to being free of having to use *the one and only true event
59 root 1.14 model*, AnyEvent also is free of bloat and policy: with POE or similar
60     modules, you get an enourmous amount of code and strict rules you have
61 root 1.16 to follow. AnyEvent, on the other hand, is lean and up to the point, by
62     only offering the functionality that is necessary, in as thin as a
63     wrapper as technically possible.
64 root 1.14
65     Of course, if you want lots of policy (this can arguably be somewhat
66     useful) and you want to force your users to use the one and only event
67     model, you should *not* use this module.
68    
69 root 1.2 DESCRIPTION
70     AnyEvent provides an identical interface to multiple event loops. This
71 root 1.6 allows module authors to utilise an event loop without forcing module
72 root 1.2 users to use the same event loop (as only a single event loop can
73     coexist peacefully at any one time).
74    
75 root 1.16 The interface itself is vaguely similar, but not identical to the Event
76 root 1.2 module.
77    
78 root 1.16 During the first call of any watcher-creation method, the module tries
79     to detect the currently loaded event loop by probing whether one of the
80     following modules is already loaded: Coro::EV, Coro::Event, EV, Event,
81 root 1.19 Glib, AnyEvent::Impl::Perl, Tk, Event::Lib, Qt, POE. The first one found
82     is used. If none are found, the module tries to load these modules
83     (excluding Tk, Event::Lib, Qt and POE as the pure perl adaptor should
84     always succeed) in the order given. The first one that can be
85     successfully loaded will be used. If, after this, still none could be
86     found, AnyEvent will fall back to a pure-perl event loop, which is not
87     very efficient, but should work everywhere.
88 root 1.6
89     Because AnyEvent first checks for modules that are already loaded,
90 root 1.16 loading an event model explicitly before first using AnyEvent will
91 root 1.6 likely make that model the default. For example:
92    
93     use Tk;
94     use AnyEvent;
95    
96     # .. AnyEvent will likely default to Tk
97    
98 root 1.16 The *likely* means that, if any module loads another event model and
99     starts using it, all bets are off. Maybe you should tell their authors
100     to use AnyEvent so their modules work together with others seamlessly...
101    
102 root 1.6 The pure-perl implementation of AnyEvent is called
103     "AnyEvent::Impl::Perl". Like other event modules you can load it
104     explicitly.
105    
106     WATCHERS
107     AnyEvent has the central concept of a *watcher*, which is an object that
108     stores relevant data for each kind of event you are waiting for, such as
109     the callback to call, the filehandle to watch, etc.
110    
111     These watchers are normal Perl objects with normal Perl lifetime. After
112     creating a watcher it will immediately "watch" for events and invoke the
113 root 1.16 callback when the event occurs (of course, only when the event model is
114     in control).
115    
116     To disable the watcher you have to destroy it (e.g. by setting the
117     variable you store it in to "undef" or otherwise deleting all references
118     to it).
119 root 1.6
120     All watchers are created by calling a method on the "AnyEvent" class.
121    
122 root 1.16 Many watchers either are used with "recursion" (repeating timers for
123     example), or need to refer to their watcher object in other ways.
124    
125     An any way to achieve that is this pattern:
126    
127     my $w; $w = AnyEvent->type (arg => value ..., cb => sub {
128     # you can use $w here, for example to undef it
129     undef $w;
130     });
131    
132     Note that "my $w; $w =" combination. This is necessary because in Perl,
133     my variables are only visible after the statement in which they are
134     declared.
135    
136 root 1.19 I/O WATCHERS
137 root 1.16 You can create an I/O watcher by calling the "AnyEvent->io" method with
138     the following mandatory key-value pairs as arguments:
139 root 1.6
140 root 1.16 "fh" the Perl *file handle* (*not* file descriptor) to watch for events.
141     "poll" must be a string that is either "r" or "w", which creates a
142     watcher waiting for "r"eadable or "w"ritable events, respectively. "cb"
143     is the callback to invoke each time the file handle becomes ready.
144    
145 root 1.19 Although the callback might get passed parameters, their value and
146     presence is undefined and you cannot rely on them. Portable AnyEvent
147     callbacks cannot use arguments passed to I/O watcher callbacks.
148    
149     The I/O watcher might use the underlying file descriptor or a copy of
150     it. You must not close a file handle as long as any watcher is active on
151     the underlying file descriptor.
152 root 1.16
153     Some event loops issue spurious readyness notifications, so you should
154     always use non-blocking calls when reading/writing from/to your file
155     handles.
156 root 1.6
157     Example:
158    
159     # wait for readability of STDIN, then read a line and disable the watcher
160     my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
161     chomp (my $input = <STDIN>);
162     warn "read: $input\n";
163     undef $w;
164     });
165    
166 root 1.8 TIME WATCHERS
167     You can create a time watcher by calling the "AnyEvent->timer" method
168 root 1.6 with the following mandatory arguments:
169    
170 root 1.16 "after" specifies after how many seconds (fractional values are
171 root 1.19 supported) the callback should be invoked. "cb" is the callback to
172     invoke in that case.
173    
174     Although the callback might get passed parameters, their value and
175     presence is undefined and you cannot rely on them. Portable AnyEvent
176     callbacks cannot use arguments passed to time watcher callbacks.
177 root 1.6
178     The timer callback will be invoked at most once: if you want a repeating
179     timer you have to create a new watcher (this is a limitation by both Tk
180     and Glib).
181    
182     Example:
183    
184     # fire an event after 7.7 seconds
185     my $w = AnyEvent->timer (after => 7.7, cb => sub {
186     warn "timeout\n";
187     });
188    
189     # to cancel the timer:
190 root 1.13 undef $w;
191 root 1.6
192 root 1.16 Example 2:
193    
194     # fire an event after 0.5 seconds, then roughly every second
195     my $w;
196    
197     my $cb = sub {
198     # cancel the old timer while creating a new one
199     $w = AnyEvent->timer (after => 1, cb => $cb);
200     };
201    
202     # start the "loop" by creating the first watcher
203     $w = AnyEvent->timer (after => 0.5, cb => $cb);
204    
205     TIMING ISSUES
206     There are two ways to handle timers: based on real time (relative, "fire
207     in 10 seconds") and based on wallclock time (absolute, "fire at 12
208     o'clock").
209    
210     While most event loops expect timers to specified in a relative way,
211     they use absolute time internally. This makes a difference when your
212     clock "jumps", for example, when ntp decides to set your clock backwards
213 root 1.18 from the wrong date of 2014-01-01 to 2008-01-01, a watcher that is
214     supposed to fire "after" a second might actually take six years to
215     finally fire.
216 root 1.16
217     AnyEvent cannot compensate for this. The only event loop that is
218     conscious about these issues is EV, which offers both relative
219 root 1.18 (ev_timer, based on true relative time) and absolute (ev_periodic, based
220     on wallclock time) timers.
221 root 1.16
222     AnyEvent always prefers relative timers, if available, matching the
223     AnyEvent API.
224    
225     SIGNAL WATCHERS
226     You can watch for signals using a signal watcher, "signal" is the signal
227     *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked
228     whenever a signal occurs.
229    
230 root 1.19 Although the callback might get passed parameters, their value and
231     presence is undefined and you cannot rely on them. Portable AnyEvent
232     callbacks cannot use arguments passed to signal watcher callbacks.
233    
234 root 1.18 Multiple signal occurances can be clumped together into one callback
235 root 1.16 invocation, and callback invocation will be synchronous. synchronous
236     means that it might take a while until the signal gets handled by the
237     process, but it is guarenteed not to interrupt any other callbacks.
238    
239     The main advantage of using these watchers is that you can share a
240     signal between multiple watchers.
241    
242     This watcher might use %SIG, so programs overwriting those signals
243     directly will likely not work correctly.
244    
245     Example: exit on SIGINT
246    
247     my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
248    
249     CHILD PROCESS WATCHERS
250     You can also watch on a child process exit and catch its exit status.
251    
252     The child process is specified by the "pid" argument (if set to 0, it
253     watches for any child process exit). The watcher will trigger as often
254     as status change for the child are received. This works by installing a
255     signal handler for "SIGCHLD". The callback will be called with the pid
256 root 1.19 and exit status (as returned by waitpid), so unlike other watcher types,
257     you *can* rely on child watcher callback arguments.
258    
259     There is a slight catch to child watchers, however: you usually start
260     them *after* the child process was created, and this means the process
261     could have exited already (and no SIGCHLD will be sent anymore).
262    
263     Not all event models handle this correctly (POE doesn't), but even for
264     event models that *do* handle this correctly, they usually need to be
265     loaded before the process exits (i.e. before you fork in the first
266     place).
267    
268     This means you cannot create a child watcher as the very first thing in
269     an AnyEvent program, you *have* to create at least one watcher before
270     you "fork" the child (alternatively, you can call "AnyEvent::detect").
271    
272     Example: fork a process and wait for it
273    
274     my $done = AnyEvent->condvar;
275 root 1.16
276 root 1.19 AnyEvent::detect; # force event module to be initialised
277    
278     my $pid = fork or exit 5;
279 root 1.16
280     my $w = AnyEvent->child (
281 root 1.19 pid => $pid,
282 root 1.16 cb => sub {
283     my ($pid, $status) = @_;
284     warn "pid $pid exited with status $status";
285 root 1.19 $done->broadcast;
286 root 1.16 },
287     );
288    
289 root 1.19 # do something else, then wait for process exit
290     $done->wait;
291    
292 root 1.16 CONDITION VARIABLES
293     Condition variables can be created by calling the "AnyEvent->condvar"
294 root 1.6 method without any arguments.
295    
296 root 1.16 A condition variable waits for a condition - precisely that the
297 root 1.6 "->broadcast" method has been called.
298    
299 root 1.16 They are very useful to signal that a condition has been fulfilled, for
300     example, if you write a module that does asynchronous http requests,
301     then a condition variable would be the ideal candidate to signal the
302     availability of results.
303    
304     You can also use condition variables to block your main program until an
305     event occurs - for example, you could "->wait" in your main program
306     until the user clicks the Quit button in your app, which would
307     "->broadcast" the "quit" event.
308    
309     Note that condition variables recurse into the event loop - if you have
310     two pirces of code that call "->wait" in a round-robbin fashion, you
311     lose. Therefore, condition variables are good to export to your caller,
312     but you should avoid making a blocking wait yourself, at least in
313     callbacks, as this asks for trouble.
314 root 1.14
315 root 1.16 This object has two methods:
316 root 1.6
317     $cv->wait
318     Wait (blocking if necessary) until the "->broadcast" method has been
319     called on c<$cv>, while servicing other watchers normally.
320    
321     You can only wait once on a condition - additional calls will return
322     immediately.
323    
324 root 1.15 Not all event models support a blocking wait - some die in that case
325 root 1.16 (programs might want to do that to stay interactive), so *if you are
326     using this from a module, never require a blocking wait*, but let
327     the caller decide whether the call will block or not (for example,
328     by coupling condition variables with some kind of request results
329     and supporting callbacks so the caller knows that getting the result
330     will not block, while still suppporting blocking waits if the caller
331     so desires).
332 root 1.15
333     Another reason *never* to "->wait" in a module is that you cannot
334     sensibly have two "->wait"'s in parallel, as that would require
335     multiple interpreters or coroutines/threads, none of which
336 root 1.16 "AnyEvent" can supply (the coroutine-aware backends
337     AnyEvent::Impl::CoroEV and AnyEvent::Impl::CoroEvent explicitly
338     support concurrent "->wait"'s from different coroutines, however).
339 root 1.15
340 root 1.6 $cv->broadcast
341     Flag the condition as ready - a running "->wait" and all further
342 root 1.16 calls to "wait" will (eventually) return after this method has been
343     called. If nobody is waiting the broadcast will be remembered..
344 root 1.6
345 root 1.16 Example:
346 root 1.8
347 root 1.16 # wait till the result is ready
348     my $result_ready = AnyEvent->condvar;
349 root 1.8
350 root 1.16 # do something such as adding a timer
351     # or socket watcher the calls $result_ready->broadcast
352     # when the "result" is ready.
353     # in this case, we simply use a timer:
354     my $w = AnyEvent->timer (
355     after => 1,
356     cb => sub { $result_ready->broadcast },
357     );
358    
359     # this "blocks" (while handling events) till the watcher
360     # calls broadcast
361     $result_ready->wait;
362 root 1.8
363 root 1.16 GLOBAL VARIABLES AND FUNCTIONS
364 root 1.7 $AnyEvent::MODEL
365     Contains "undef" until the first watcher is being created. Then it
366     contains the event model that is being used, which is the name of
367     the Perl class implementing the model. This class is usually one of
368     the "AnyEvent::Impl:xxx" modules, but can be any other class in the
369     case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*).
370    
371     The known classes so far are:
372    
373 root 1.12 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
374 root 1.15 AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice.
375 root 1.18 AnyEvent::Impl::EV based on EV (an interface to libev, best choice).
376     AnyEvent::Impl::Event based on Event, second best choice.
377 root 1.15 AnyEvent::Impl::Glib based on Glib, third-best choice.
378 root 1.19 AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable.
379 root 1.7 AnyEvent::Impl::Tk based on Tk, very bad choice.
380 root 1.18 AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs).
381     AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
382 root 1.19 AnyEvent::Impl::POE based on POE, not generic enough for full support.
383    
384     There is no support for WxWidgets, as WxWidgets has no support for
385     watching file handles. However, you can use WxWidgets through the
386     POE Adaptor, as POE has a Wx backend that simply polls 20 times per
387     second, which was considered to be too horrible to even consider for
388     AnyEvent. Likewise, other POE backends can be used by AnyEvent by
389     using it's adaptor.
390    
391     AnyEvent knows about Prima and Wx and will try to use POE when
392     autodetecting them.
393 root 1.7
394 root 1.8 AnyEvent::detect
395     Returns $AnyEvent::MODEL, forcing autodetection of the event model
396     if necessary. You should only call this function right before you
397 root 1.16 would have created an AnyEvent watcher anyway, that is, as late as
398     possible at runtime.
399 root 1.8
400 root 1.6 WHAT TO DO IN A MODULE
401     As a module author, you should "use AnyEvent" and call AnyEvent methods
402     freely, but you should not load a specific event module or rely on it.
403    
404 root 1.16 Be careful when you create watchers in the module body - AnyEvent will
405 root 1.6 decide which event module to use as soon as the first method is called,
406     so by calling AnyEvent in your module body you force the user of your
407     module to load the event module first.
408    
409 root 1.16 Never call "->wait" on a condition variable unless you *know* that the
410     "->broadcast" method has been called on it already. This is because it
411     will stall the whole program, and the whole point of using events is to
412     stay interactive.
413    
414     It is fine, however, to call "->wait" when the user of your module
415     requests it (i.e. if you create a http request object ad have a method
416     called "results" that returns the results, it should call "->wait"
417     freely, as the user of your module knows what she is doing. always).
418    
419 root 1.6 WHAT TO DO IN THE MAIN PROGRAM
420     There will always be a single main program - the only place that should
421     dictate which event model to use.
422    
423     If it doesn't care, it can just "use AnyEvent" and use it itself, or not
424 root 1.16 do anything special (it does not need to be event-based) and let
425     AnyEvent decide which implementation to chose if some module relies on
426     it.
427    
428     If the main program relies on a specific event model. For example, in
429     Gtk2 programs you have to rely on the Glib module. You should load the
430     event module before loading AnyEvent or any module that uses it:
431     generally speaking, you should load it as early as possible. The reason
432     is that modules might create watchers when they are loaded, and AnyEvent
433     will decide on the event model to use as soon as it creates watchers,
434     and it might chose the wrong one unless you load the correct one
435     yourself.
436 root 1.6
437     You can chose to use a rather inefficient pure-perl implementation by
438 root 1.16 loading the "AnyEvent::Impl::Perl" module, which gives you similar
439     behaviour everywhere, but letting AnyEvent chose is generally better.
440 root 1.2
441 root 1.19 OTHER MODULES
442     The following is a non-exhaustive list of additional modules that use
443     AnyEvent and can therefore be mixed easily with other AnyEvent modules
444     in the same program. Some of the modules come with AnyEvent, some are
445     available via CPAN.
446    
447     AnyEvent::Util
448     Contains various utility functions that replace often-used but
449     blocking functions such as "inet_aton" by event-/callback-based
450     versions.
451    
452     AnyEvent::Handle
453     Provide read and write buffers and manages watchers for reads and
454     writes.
455    
456     AnyEvent::Socket
457     Provides a means to do non-blocking connects, accepts etc.
458    
459     AnyEvent::HTTPD
460     Provides a simple web application server framework.
461    
462     AnyEvent::DNS
463     Provides asynchronous DNS resolver capabilities, beyond what
464     AnyEvent::Util offers.
465    
466     AnyEvent::FastPing
467     The fastest ping in the west.
468    
469     Net::IRC3
470     AnyEvent based IRC client module family.
471    
472     Net::XMPP2
473     AnyEvent based XMPP (Jabber protocol) module family.
474    
475     Net::FCP
476     AnyEvent-based implementation of the Freenet Client Protocol,
477     birthplace of AnyEvent.
478    
479     Event::ExecFlow
480     High level API for event-based execution flow control.
481    
482     Coro
483     Has special support for AnyEvent.
484    
485     IO::Lambda
486     The lambda approach to I/O - don't ask, look there. Can use
487     AnyEvent.
488    
489     IO::AIO
490     Truly asynchronous I/O, should be in the toolbox of every event
491     programmer. Can be trivially made to use AnyEvent.
492    
493     BDB Truly asynchronous Berkeley DB access. Can be trivially made to use
494     AnyEvent.
495    
496 root 1.5 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
497 root 1.16 This is an advanced topic that you do not normally need to use AnyEvent
498     in a module. This section is only of use to event loop authors who want
499     to provide AnyEvent compatibility.
500    
501 root 1.5 If you need to support another event library which isn't directly
502     supported by AnyEvent, you can supply your own interface to it by
503 root 1.6 pushing, before the first watcher gets created, the package name of the
504 root 1.5 event module and the package name of the interface to use onto
505     @AnyEvent::REGISTRY. You can do that before and even without loading
506 root 1.16 AnyEvent, so it is reasonably cheap.
507 root 1.5
508     Example:
509    
510     push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::];
511    
512 root 1.6 This tells AnyEvent to (literally) use the "urxvt::anyevent::"
513 root 1.16 package/class when it finds the "urxvt" package/module is already
514     loaded.
515    
516     When AnyEvent is loaded and asked to find a suitable event model, it
517     will first check for the presence of urxvt by trying to "use" the
518     "urxvt::anyevent" module.
519    
520     The class should provide implementations for all watcher types. See
521     AnyEvent::Impl::EV (source code), AnyEvent::Impl::Glib (Source code) and
522     so on for actual examples. Use "perldoc -m AnyEvent::Impl::Glib" to see
523     the sources.
524    
525     If you don't provide "signal" and "child" watchers than AnyEvent will
526     provide suitable (hopefully) replacements.
527    
528     The above example isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt)
529     terminal emulator uses the above line as-is. An interface isn't included
530     in AnyEvent because it doesn't make sense outside the embedded
531     interpreter inside *rxvt-unicode*, and it is updated and maintained as
532     part of the *rxvt-unicode* distribution.
533 root 1.5
534 root 1.6 *rxvt-unicode* also cheats a bit by not providing blocking access to
535     condition variables: code blocking while waiting for a condition will
536     "die". This still works with most modules/usages, and blocking calls
537 root 1.16 must not be done in an interactive application, so it makes sense.
538 root 1.6
539 root 1.4 ENVIRONMENT VARIABLES
540     The following environment variables are used by this module:
541    
542 root 1.18 "PERL_ANYEVENT_VERBOSE"
543 root 1.19 By default, AnyEvent will be completely silent except in fatal
544     conditions. You can set this environment variable to make AnyEvent
545     more talkative.
546    
547     When set to 1 or higher, causes AnyEvent to warn about unexpected
548     conditions, such as not being able to load the event model specified
549     by "PERL_ANYEVENT_MODEL".
550    
551 root 1.18 When set to 2 or higher, cause AnyEvent to report to STDERR which
552     event model it chooses.
553    
554     "PERL_ANYEVENT_MODEL"
555     This can be used to specify the event model to be used by AnyEvent,
556     before autodetection and -probing kicks in. It must be a string
557     consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
558     gets prepended and the resulting module name is loaded and if the
559     load was successful, used as event model. If it fails to load
560     AnyEvent will proceed with autodetection and -probing.
561    
562     This functionality might change in future versions.
563    
564     For example, to force the pure perl model (AnyEvent::Impl::Perl) you
565     could start your program like this:
566    
567     PERL_ANYEVENT_MODEL=Perl perl ...
568 root 1.4
569 root 1.16 EXAMPLE PROGRAM
570 root 1.19 The following program uses an I/O watcher to read data from STDIN, a
571 root 1.16 timer to display a message once per second, and a condition variable to
572     quit the program when the user enters quit:
573 root 1.2
574     use AnyEvent;
575    
576     my $cv = AnyEvent->condvar;
577    
578 root 1.16 my $io_watcher = AnyEvent->io (
579     fh => \*STDIN,
580     poll => 'r',
581     cb => sub {
582     warn "io event <$_[0]>\n"; # will always output <r>
583     chomp (my $input = <STDIN>); # read a line
584     warn "read: $input\n"; # output what has been read
585     $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i
586     },
587     );
588 root 1.2
589     my $time_watcher; # can only be used once
590    
591     sub new_timer {
592     $timer = AnyEvent->timer (after => 1, cb => sub {
593     warn "timeout\n"; # print 'timeout' about every second
594     &new_timer; # and restart the time
595     });
596     }
597    
598     new_timer; # create first timer
599    
600     $cv->wait; # wait until user enters /^q/i
601    
602 root 1.3 REAL-WORLD EXAMPLE
603     Consider the Net::FCP module. It features (among others) the following
604     API calls, which are to freenet what HTTP GET requests are to http:
605    
606     my $data = $fcp->client_get ($url); # blocks
607    
608     my $transaction = $fcp->txn_client_get ($url); # does not block
609     $transaction->cb ( sub { ... } ); # set optional result callback
610     my $data = $transaction->result; # possibly blocks
611    
612     The "client_get" method works like "LWP::Simple::get": it requests the
613     given URL and waits till the data has arrived. It is defined to be:
614    
615     sub client_get { $_[0]->txn_client_get ($_[1])->result }
616    
617     And in fact is automatically generated. This is the blocking API of
618     Net::FCP, and it works as simple as in any other, similar, module.
619    
620     More complicated is "txn_client_get": It only creates a transaction
621     (completion, result, ...) object and initiates the transaction.
622    
623     my $txn = bless { }, Net::FCP::Txn::;
624    
625     It also creates a condition variable that is used to signal the
626     completion of the request:
627    
628     $txn->{finished} = AnyAvent->condvar;
629    
630     It then creates a socket in non-blocking mode.
631    
632     socket $txn->{fh}, ...;
633     fcntl $txn->{fh}, F_SETFL, O_NONBLOCK;
634     connect $txn->{fh}, ...
635     and !$!{EWOULDBLOCK}
636     and !$!{EINPROGRESS}
637     and Carp::croak "unable to connect: $!\n";
638    
639 root 1.4 Then it creates a write-watcher which gets called whenever an error
640 root 1.3 occurs or the connection succeeds:
641    
642     $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w });
643    
644     And returns this transaction object. The "fh_ready_w" callback gets
645     called as soon as the event loop detects that the socket is ready for
646     writing.
647    
648     The "fh_ready_w" method makes the socket blocking again, writes the
649     request data and replaces the watcher by a read watcher (waiting for
650     reply data). The actual code is more complicated, but that doesn't
651     matter for this example:
652    
653     fcntl $txn->{fh}, F_SETFL, 0;
654     syswrite $txn->{fh}, $txn->{request}
655     or die "connection or write error";
656     $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
657    
658     Again, "fh_ready_r" waits till all data has arrived, and then stores the
659     result and signals any possible waiters that the request ahs finished:
660    
661     sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
662    
663     if (end-of-file or data complete) {
664     $txn->{result} = $txn->{buf};
665     $txn->{finished}->broadcast;
666 root 1.4 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
667 root 1.3 }
668    
669     The "result" method, finally, just waits for the finished signal (if the
670     request was already finished, it doesn't wait, of course, and returns
671     the data:
672    
673     $txn->{finished}->wait;
674 root 1.4 return $txn->{result};
675 root 1.3
676     The actual code goes further and collects all errors ("die"s,
677     exceptions) that occured during request processing. The "result" method
678 root 1.16 detects whether an exception as thrown (it is stored inside the $txn
679 root 1.3 object) and just throws the exception, which means connection errors and
680     other problems get reported tot he code that tries to use the result,
681     not in a random callback.
682    
683     All of this enables the following usage styles:
684    
685     1. Blocking:
686    
687     my $data = $fcp->client_get ($url);
688    
689 root 1.15 2. Blocking, but running in parallel:
690 root 1.3
691     my @datas = map $_->result,
692     map $fcp->txn_client_get ($_),
693     @urls;
694    
695     Both blocking examples work without the module user having to know
696     anything about events.
697    
698 root 1.15 3a. Event-based in a main program, using any supported event module:
699 root 1.3
700 root 1.15 use EV;
701 root 1.3
702     $fcp->txn_client_get ($url)->cb (sub {
703     my $txn = shift;
704     my $data = $txn->result;
705     ...
706     });
707    
708 root 1.15 EV::loop;
709 root 1.3
710     3b. The module user could use AnyEvent, too:
711    
712     use AnyEvent;
713    
714     my $quit = AnyEvent->condvar;
715    
716     $fcp->txn_client_get ($url)->cb (sub {
717     ...
718     $quit->broadcast;
719     });
720    
721     $quit->wait;
722    
723 root 1.19 BENCHMARKS
724     To give you an idea of the performance and overheads that AnyEvent adds
725     over the event loops themselves and to give you an impression of the
726     speed of various event loops I prepared some benchmarks.
727    
728     BENCHMARKING ANYEVENT OVERHEAD
729     Here is a benchmark of various supported event models used natively and
730     through anyevent. The benchmark creates a lot of timers (with a zero
731     timeout) and I/O watchers (watching STDOUT, a pty, to become writable,
732     which it is), lets them fire exactly once and destroys them again.
733    
734     Source code for this benchmark is found as eg/bench in the AnyEvent
735     distribution.
736    
737     Explanation of the columns
738     *watcher* is the number of event watchers created/destroyed. Since
739     different event models feature vastly different performances, each event
740     loop was given a number of watchers so that overall runtime is
741     acceptable and similar between tested event loop (and keep them from
742     crashing): Glib would probably take thousands of years if asked to
743     process the same number of watchers as EV in this benchmark.
744    
745     *bytes* is the number of bytes (as measured by the resident set size,
746     RSS) consumed by each watcher. This method of measuring captures both C
747     and Perl-based overheads.
748    
749     *create* is the time, in microseconds (millionths of seconds), that it
750     takes to create a single watcher. The callback is a closure shared
751     between all watchers, to avoid adding memory overhead. That means
752     closure creation and memory usage is not included in the figures.
753    
754     *invoke* is the time, in microseconds, used to invoke a simple callback.
755     The callback simply counts down a Perl variable and after it was invoked
756     "watcher" times, it would "->broadcast" a condvar once to signal the end
757     of this phase.
758    
759     *destroy* is the time, in microseconds, that it takes to destroy a
760     single watcher.
761    
762     Results
763     name watchers bytes create invoke destroy comment
764     EV/EV 400000 244 0.56 0.46 0.31 EV native interface
765     EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers
766     CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal
767     Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation
768     Event/Event 16000 516 31.88 31.30 0.85 Event native interface
769     Event/Any 16000 590 35.75 31.42 1.08 Event + AnyEvent watchers
770     Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour
771     Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers
772     POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event
773     POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select
774    
775     Discussion
776     The benchmark does *not* measure scalability of the event loop very
777     well. For example, a select-based event loop (such as the pure perl one)
778     can never compete with an event loop that uses epoll when the number of
779     file descriptors grows high. In this benchmark, all events become ready
780     at the same time, so select/poll-based implementations get an unnatural
781     speed boost.
782    
783     Also, note that the number of watchers usually has a nonlinear effect on
784     overall speed, that is, creating twice as many watchers doesn't take
785     twice the time - usually it takes longer. This puts event loops tested
786     with a higher number of watchers at a disadvantage.
787    
788     To put the range of results into perspective, consider that on the
789     benchmark machine, handling an event takes roughly 1600 CPU cycles with
790     EV, 3100 CPU cycles with AnyEvent's pure perl loop and almost 3000000
791     CPU cycles with POE.
792    
793     "EV" is the sole leader regarding speed and memory use, which are both
794     maximal/minimal, respectively. Even when going through AnyEvent, it uses
795     far less memory than any other event loop and is still faster than Event
796     natively.
797    
798     The pure perl implementation is hit in a few sweet spots (both the
799     constant timeout and the use of a single fd hit optimisations in the
800     perl interpreter and the backend itself). Nevertheless this shows that
801     it adds very little overhead in itself. Like any select-based backend
802     its performance becomes really bad with lots of file descriptors (and
803     few of them active), of course, but this was not subject of this
804     benchmark.
805    
806     The "Event" module has a relatively high setup and callback invocation
807     cost, but overall scores in on the third place.
808    
809     "Glib"'s memory usage is quite a bit higher, but it features a faster
810     callback invocation and overall ends up in the same class as "Event".
811     However, Glib scales extremely badly, doubling the number of watchers
812     increases the processing time by more than a factor of four, making it
813     completely unusable when using larger numbers of watchers (note that
814     only a single file descriptor was used in the benchmark, so
815     inefficiencies of "poll" do not account for this).
816    
817     The "Tk" adaptor works relatively well. The fact that it crashes with
818     more than 2000 watchers is a big setback, however, as correctness takes
819     precedence over speed. Nevertheless, its performance is surprising, as
820     the file descriptor is dup()ed for each watcher. This shows that the
821     dup() employed by some adaptors is not a big performance issue (it does
822     incur a hidden memory cost inside the kernel which is not reflected in
823     the figures above).
824    
825     "POE", regardless of underlying event loop (whether using its pure perl
826     select-based backend or the Event module, the POE-EV backend couldn't be
827     tested because it wasn't working) shows abysmal performance and memory
828     usage: Watchers use almost 30 times as much memory as EV watchers, and
829     10 times as much memory as Event (the high memory requirements are
830     caused by requiring a session for each watcher). Watcher invocation
831     speed is almost 900 times slower than with AnyEvent's pure perl
832     implementation. The design of the POE adaptor class in AnyEvent can not
833     really account for this, as session creation overhead is small compared
834     to execution of the state machine, which is coded pretty optimally
835     within AnyEvent::Impl::POE. POE simply seems to be abysmally slow.
836    
837     Summary
838     * Using EV through AnyEvent is faster than any other event loop (even
839     when used without AnyEvent), but most event loops have acceptable
840     performance with or without AnyEvent.
841    
842     * The overhead AnyEvent adds is usually much smaller than the overhead
843     of the actual event loop, only with extremely fast event loops such
844     as EV adds AnyEvent significant overhead.
845    
846     * You should avoid POE like the plague if you want performance or
847     reasonable memory usage.
848    
849     BENCHMARKING THE LARGE SERVER CASE
850     This benchmark atcually benchmarks the event loop itself. It works by
851     creating a number of "servers": each server consists of a socketpair, a
852     timeout watcher that gets reset on activity (but never fires), and an
853     I/O watcher waiting for input on one side of the socket. Each time the
854     socket watcher reads a byte it will write that byte to a random other
855     "server".
856    
857     The effect is that there will be a lot of I/O watchers, only part of
858     which are active at any one point (so there is a constant number of
859     active fds for each loop iterstaion, but which fds these are is random).
860     The timeout is reset each time something is read because that reflects
861     how most timeouts work (and puts extra pressure on the event loops).
862    
863     In this benchmark, we use 10000 socketpairs (20000 sockets), of which
864     100 (1%) are active. This mirrors the activity of large servers with
865     many connections, most of which are idle at any one point in time.
866    
867     Source code for this benchmark is found as eg/bench2 in the AnyEvent
868     distribution.
869    
870     Explanation of the columns
871     *sockets* is the number of sockets, and twice the number of "servers"
872     (as each server has a read and write socket end).
873    
874     *create* is the time it takes to create a socketpair (which is
875     nontrivial) and two watchers: an I/O watcher and a timeout watcher.
876    
877     *request*, the most important value, is the time it takes to handle a
878     single "request", that is, reading the token from the pipe and
879     forwarding it to another server. This includes deleting the old timeout
880     and creating a new one that moves the timeout into the future.
881    
882     Results
883     name sockets create request
884     EV 20000 69.01 11.16
885     Perl 20000 73.32 35.87
886     Event 20000 212.62 257.32
887     Glib 20000 651.16 1896.30
888     POE 20000 349.67 12317.24 uses POE::Loop::Event
889    
890     Discussion
891     This benchmark *does* measure scalability and overall performance of the
892     particular event loop.
893    
894     EV is again fastest. Since it is using epoll on my system, the setup
895     time is relatively high, though.
896    
897     Perl surprisingly comes second. It is much faster than the C-based event
898     loops Event and Glib.
899    
900     Event suffers from high setup time as well (look at its code and you
901     will understand why). Callback invocation also has a high overhead
902     compared to the "$_->() for .."-style loop that the Perl event loop
903     uses. Event uses select or poll in basically all documented
904     configurations.
905    
906     Glib is hit hard by its quadratic behaviour w.r.t. many watchers. It
907     clearly fails to perform with many filehandles or in busy servers.
908    
909     POE is still completely out of the picture, taking over 1000 times as
910     long as EV, and over 100 times as long as the Perl implementation, even
911     though it uses a C-based event loop in this case.
912    
913     Summary
914     * The pure perl implementation performs extremely well, considering
915     that it uses select.
916    
917     * Avoid Glib or POE in large projects where performance matters.
918    
919     BENCHMARKING SMALL SERVERS
920     While event loops should scale (and select-based ones do not...) even to
921     large servers, most programs we (or I :) actually write have only a few
922     I/O watchers.
923    
924     In this benchmark, I use the same benchmark program as in the large
925     server case, but it uses only eight "servers", of which three are active
926     at any one time. This should reflect performance for a small server
927     relatively well.
928    
929     The columns are identical to the previous table.
930    
931     Results
932     name sockets create request
933     EV 16 20.00 6.54
934     Perl 16 25.75 12.62
935     Event 16 81.27 35.86
936     Glib 16 32.63 15.48
937     POE 16 261.87 276.28 uses POE::Loop::Event
938    
939     Discussion
940     The benchmark tries to test the performance of a typical small server.
941     While knowing how various event loops perform is interesting, keep in
942     mind that their overhead in this case is usually not as important, due
943     to the small absolute number of watchers (that is, you need efficiency
944     and speed most when you have lots of watchers, not when you only have a
945     few of them).
946    
947     EV is again fastest.
948    
949     Perl again comes second. It is noticably faster than the C-based event
950     loops Event and Glib, although the difference is too small to really
951     matter.
952    
953     POE also performs much better in this case, but is is still far behind
954     the others.
955    
956     Summary
957     * C-based event loops perform very well with small number of watchers,
958     as the management overhead dominates.
959    
960 root 1.18 FORK
961     Most event libraries are not fork-safe. The ones who are usually are
962     because they are so inefficient. Only EV is fully fork-aware.
963    
964     If you have to fork, you must either do so *before* creating your first
965     watcher OR you must not use AnyEvent at all in the child.
966    
967     SECURITY CONSIDERATIONS
968     AnyEvent can be forced to load any event model via
969     $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used
970     to execute arbitrary code or directly gain access, it can easily be used
971     to make the program hang or malfunction in subtle ways, as AnyEvent
972     watchers will not be active when the program uses a different event
973     model than specified in the variable.
974    
975     You can make AnyEvent completely ignore this variable by deleting it
976     before the first watcher gets created, e.g. with a "BEGIN" block:
977    
978     BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
979    
980     use AnyEvent;
981    
982 root 1.2 SEE ALSO
983 root 1.15 Event modules: Coro::EV, EV, EV::Glib, Glib::EV, Coro::Event, Event,
984 root 1.19 Glib::Event, Glib, Coro, Tk, Event::Lib, Qt, POE.
985 root 1.3
986 root 1.15 Implementations: AnyEvent::Impl::CoroEV, AnyEvent::Impl::EV,
987     AnyEvent::Impl::CoroEvent, AnyEvent::Impl::Event, AnyEvent::Impl::Glib,
988 root 1.18 AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, AnyEvent::Impl::EventLib,
989 root 1.19 AnyEvent::Impl::Qt, AnyEvent::Impl::POE.
990 root 1.3
991 root 1.15 Nontrivial usage examples: Net::FCP, Net::XMPP2.
992 root 1.2
993 root 1.17 AUTHOR
994     Marc Lehmann <schmorp@schmorp.de>
995     http://home.schmorp.de/
996 root 1.2