ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/README
Revision: 1.18
Committed: Thu Apr 24 09:13:26 2008 UTC (16 years ago) by root
Branch: MAIN
CVS Tags: rel-3_2
Changes since 1.17: +63 -19 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.18 EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib, Qt -
5     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.18 Glib, Tk, Event::Lib, Qt. The first one found is used. If none are
82     found, the module tries to load these modules (excluding Event::Lib and
83     Qt) in the order given. The first one that can be successfully loaded
84     will be used. If, after this, still none could be found, AnyEvent will
85     fall back to a pure-perl event loop, which is not very efficient, but
86     should work everywhere.
87 root 1.6
88     Because AnyEvent first checks for modules that are already loaded,
89 root 1.16 loading an event model explicitly before first using AnyEvent will
90 root 1.6 likely make that model the default. For example:
91    
92     use Tk;
93     use AnyEvent;
94    
95     # .. AnyEvent will likely default to Tk
96    
97 root 1.16 The *likely* means that, if any module loads another event model and
98     starts using it, all bets are off. Maybe you should tell their authors
99     to use AnyEvent so their modules work together with others seamlessly...
100    
101 root 1.6 The pure-perl implementation of AnyEvent is called
102     "AnyEvent::Impl::Perl". Like other event modules you can load it
103     explicitly.
104    
105     WATCHERS
106     AnyEvent has the central concept of a *watcher*, which is an object that
107     stores relevant data for each kind of event you are waiting for, such as
108     the callback to call, the filehandle to watch, etc.
109    
110     These watchers are normal Perl objects with normal Perl lifetime. After
111     creating a watcher it will immediately "watch" for events and invoke the
112 root 1.16 callback when the event occurs (of course, only when the event model is
113     in control).
114    
115     To disable the watcher you have to destroy it (e.g. by setting the
116     variable you store it in to "undef" or otherwise deleting all references
117     to it).
118 root 1.6
119     All watchers are created by calling a method on the "AnyEvent" class.
120    
121 root 1.16 Many watchers either are used with "recursion" (repeating timers for
122     example), or need to refer to their watcher object in other ways.
123    
124     An any way to achieve that is this pattern:
125    
126     my $w; $w = AnyEvent->type (arg => value ..., cb => sub {
127     # you can use $w here, for example to undef it
128     undef $w;
129     });
130    
131     Note that "my $w; $w =" combination. This is necessary because in Perl,
132     my variables are only visible after the statement in which they are
133     declared.
134    
135 root 1.6 IO WATCHERS
136 root 1.16 You can create an I/O watcher by calling the "AnyEvent->io" method with
137     the following mandatory key-value pairs as arguments:
138 root 1.6
139 root 1.16 "fh" the Perl *file handle* (*not* file descriptor) to watch for events.
140     "poll" must be a string that is either "r" or "w", which creates a
141     watcher waiting for "r"eadable or "w"ritable events, respectively. "cb"
142     is the callback to invoke each time the file handle becomes ready.
143    
144 root 1.18 As long as the I/O watcher exists it will keep the file descriptor or a
145     copy of it alive/open.
146 root 1.16
147     It is not allowed to close a file handle as long as any watcher is
148     active on the underlying file descriptor.
149    
150     Some event loops issue spurious readyness notifications, so you should
151     always use non-blocking calls when reading/writing from/to your file
152     handles.
153 root 1.6
154     Example:
155    
156     # wait for readability of STDIN, then read a line and disable the watcher
157     my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
158     chomp (my $input = <STDIN>);
159     warn "read: $input\n";
160     undef $w;
161     });
162    
163 root 1.8 TIME WATCHERS
164     You can create a time watcher by calling the "AnyEvent->timer" method
165 root 1.6 with the following mandatory arguments:
166    
167 root 1.16 "after" specifies after how many seconds (fractional values are
168     supported) should the timer activate. "cb" the callback to invoke in
169     that case.
170 root 1.6
171     The timer callback will be invoked at most once: if you want a repeating
172     timer you have to create a new watcher (this is a limitation by both Tk
173     and Glib).
174    
175     Example:
176    
177     # fire an event after 7.7 seconds
178     my $w = AnyEvent->timer (after => 7.7, cb => sub {
179     warn "timeout\n";
180     });
181    
182     # to cancel the timer:
183 root 1.13 undef $w;
184 root 1.6
185 root 1.16 Example 2:
186    
187     # fire an event after 0.5 seconds, then roughly every second
188     my $w;
189    
190     my $cb = sub {
191     # cancel the old timer while creating a new one
192     $w = AnyEvent->timer (after => 1, cb => $cb);
193     };
194    
195     # start the "loop" by creating the first watcher
196     $w = AnyEvent->timer (after => 0.5, cb => $cb);
197    
198     TIMING ISSUES
199     There are two ways to handle timers: based on real time (relative, "fire
200     in 10 seconds") and based on wallclock time (absolute, "fire at 12
201     o'clock").
202    
203     While most event loops expect timers to specified in a relative way,
204     they use absolute time internally. This makes a difference when your
205     clock "jumps", for example, when ntp decides to set your clock backwards
206 root 1.18 from the wrong date of 2014-01-01 to 2008-01-01, a watcher that is
207     supposed to fire "after" a second might actually take six years to
208     finally fire.
209 root 1.16
210     AnyEvent cannot compensate for this. The only event loop that is
211     conscious about these issues is EV, which offers both relative
212 root 1.18 (ev_timer, based on true relative time) and absolute (ev_periodic, based
213     on wallclock time) timers.
214 root 1.16
215     AnyEvent always prefers relative timers, if available, matching the
216     AnyEvent API.
217    
218     SIGNAL WATCHERS
219     You can watch for signals using a signal watcher, "signal" is the signal
220     *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked
221     whenever a signal occurs.
222    
223 root 1.18 Multiple signal occurances can be clumped together into one callback
224 root 1.16 invocation, and callback invocation will be synchronous. synchronous
225     means that it might take a while until the signal gets handled by the
226     process, but it is guarenteed not to interrupt any other callbacks.
227    
228     The main advantage of using these watchers is that you can share a
229     signal between multiple watchers.
230    
231     This watcher might use %SIG, so programs overwriting those signals
232     directly will likely not work correctly.
233    
234     Example: exit on SIGINT
235    
236     my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
237    
238     CHILD PROCESS WATCHERS
239     You can also watch on a child process exit and catch its exit status.
240    
241     The child process is specified by the "pid" argument (if set to 0, it
242     watches for any child process exit). The watcher will trigger as often
243     as status change for the child are received. This works by installing a
244     signal handler for "SIGCHLD". The callback will be called with the pid
245     and exit status (as returned by waitpid).
246    
247     Example: wait for pid 1333
248    
249     my $w = AnyEvent->child (
250     pid => 1333,
251     cb => sub {
252     my ($pid, $status) = @_;
253     warn "pid $pid exited with status $status";
254     },
255     );
256    
257     CONDITION VARIABLES
258     Condition variables can be created by calling the "AnyEvent->condvar"
259 root 1.6 method without any arguments.
260    
261 root 1.16 A condition variable waits for a condition - precisely that the
262 root 1.6 "->broadcast" method has been called.
263    
264 root 1.16 They are very useful to signal that a condition has been fulfilled, for
265     example, if you write a module that does asynchronous http requests,
266     then a condition variable would be the ideal candidate to signal the
267     availability of results.
268    
269     You can also use condition variables to block your main program until an
270     event occurs - for example, you could "->wait" in your main program
271     until the user clicks the Quit button in your app, which would
272     "->broadcast" the "quit" event.
273    
274     Note that condition variables recurse into the event loop - if you have
275     two pirces of code that call "->wait" in a round-robbin fashion, you
276     lose. Therefore, condition variables are good to export to your caller,
277     but you should avoid making a blocking wait yourself, at least in
278     callbacks, as this asks for trouble.
279 root 1.14
280 root 1.16 This object has two methods:
281 root 1.6
282     $cv->wait
283     Wait (blocking if necessary) until the "->broadcast" method has been
284     called on c<$cv>, while servicing other watchers normally.
285    
286     You can only wait once on a condition - additional calls will return
287     immediately.
288    
289 root 1.15 Not all event models support a blocking wait - some die in that case
290 root 1.16 (programs might want to do that to stay interactive), so *if you are
291     using this from a module, never require a blocking wait*, but let
292     the caller decide whether the call will block or not (for example,
293     by coupling condition variables with some kind of request results
294     and supporting callbacks so the caller knows that getting the result
295     will not block, while still suppporting blocking waits if the caller
296     so desires).
297 root 1.15
298     Another reason *never* to "->wait" in a module is that you cannot
299     sensibly have two "->wait"'s in parallel, as that would require
300     multiple interpreters or coroutines/threads, none of which
301 root 1.16 "AnyEvent" can supply (the coroutine-aware backends
302     AnyEvent::Impl::CoroEV and AnyEvent::Impl::CoroEvent explicitly
303     support concurrent "->wait"'s from different coroutines, however).
304 root 1.15
305 root 1.6 $cv->broadcast
306     Flag the condition as ready - a running "->wait" and all further
307 root 1.16 calls to "wait" will (eventually) return after this method has been
308     called. If nobody is waiting the broadcast will be remembered..
309 root 1.6
310 root 1.16 Example:
311 root 1.8
312 root 1.16 # wait till the result is ready
313     my $result_ready = AnyEvent->condvar;
314 root 1.8
315 root 1.16 # do something such as adding a timer
316     # or socket watcher the calls $result_ready->broadcast
317     # when the "result" is ready.
318     # in this case, we simply use a timer:
319     my $w = AnyEvent->timer (
320     after => 1,
321     cb => sub { $result_ready->broadcast },
322     );
323    
324     # this "blocks" (while handling events) till the watcher
325     # calls broadcast
326     $result_ready->wait;
327 root 1.8
328 root 1.16 GLOBAL VARIABLES AND FUNCTIONS
329 root 1.7 $AnyEvent::MODEL
330     Contains "undef" until the first watcher is being created. Then it
331     contains the event model that is being used, which is the name of
332     the Perl class implementing the model. This class is usually one of
333     the "AnyEvent::Impl:xxx" modules, but can be any other class in the
334     case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*).
335    
336     The known classes so far are:
337    
338 root 1.12 AnyEvent::Impl::CoroEV based on Coro::EV, best choice.
339 root 1.15 AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice.
340 root 1.18 AnyEvent::Impl::EV based on EV (an interface to libev, best choice).
341     AnyEvent::Impl::Event based on Event, second best choice.
342 root 1.15 AnyEvent::Impl::Glib based on Glib, third-best choice.
343 root 1.7 AnyEvent::Impl::Tk based on Tk, very bad choice.
344 root 1.15 AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable.
345 root 1.18 AnyEvent::Impl::Qt based on Qt, cannot be autoprobed (see its docs).
346     AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
347 root 1.7
348 root 1.8 AnyEvent::detect
349     Returns $AnyEvent::MODEL, forcing autodetection of the event model
350     if necessary. You should only call this function right before you
351 root 1.16 would have created an AnyEvent watcher anyway, that is, as late as
352     possible at runtime.
353 root 1.8
354 root 1.6 WHAT TO DO IN A MODULE
355     As a module author, you should "use AnyEvent" and call AnyEvent methods
356     freely, but you should not load a specific event module or rely on it.
357    
358 root 1.16 Be careful when you create watchers in the module body - AnyEvent will
359 root 1.6 decide which event module to use as soon as the first method is called,
360     so by calling AnyEvent in your module body you force the user of your
361     module to load the event module first.
362    
363 root 1.16 Never call "->wait" on a condition variable unless you *know* that the
364     "->broadcast" method has been called on it already. This is because it
365     will stall the whole program, and the whole point of using events is to
366     stay interactive.
367    
368     It is fine, however, to call "->wait" when the user of your module
369     requests it (i.e. if you create a http request object ad have a method
370     called "results" that returns the results, it should call "->wait"
371     freely, as the user of your module knows what she is doing. always).
372    
373 root 1.6 WHAT TO DO IN THE MAIN PROGRAM
374     There will always be a single main program - the only place that should
375     dictate which event model to use.
376    
377     If it doesn't care, it can just "use AnyEvent" and use it itself, or not
378 root 1.16 do anything special (it does not need to be event-based) and let
379     AnyEvent decide which implementation to chose if some module relies on
380     it.
381    
382     If the main program relies on a specific event model. For example, in
383     Gtk2 programs you have to rely on the Glib module. You should load the
384     event module before loading AnyEvent or any module that uses it:
385     generally speaking, you should load it as early as possible. The reason
386     is that modules might create watchers when they are loaded, and AnyEvent
387     will decide on the event model to use as soon as it creates watchers,
388     and it might chose the wrong one unless you load the correct one
389     yourself.
390 root 1.6
391     You can chose to use a rather inefficient pure-perl implementation by
392 root 1.16 loading the "AnyEvent::Impl::Perl" module, which gives you similar
393     behaviour everywhere, but letting AnyEvent chose is generally better.
394 root 1.2
395 root 1.5 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
396 root 1.16 This is an advanced topic that you do not normally need to use AnyEvent
397     in a module. This section is only of use to event loop authors who want
398     to provide AnyEvent compatibility.
399    
400 root 1.5 If you need to support another event library which isn't directly
401     supported by AnyEvent, you can supply your own interface to it by
402 root 1.6 pushing, before the first watcher gets created, the package name of the
403 root 1.5 event module and the package name of the interface to use onto
404     @AnyEvent::REGISTRY. You can do that before and even without loading
405 root 1.16 AnyEvent, so it is reasonably cheap.
406 root 1.5
407     Example:
408    
409     push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::];
410    
411 root 1.6 This tells AnyEvent to (literally) use the "urxvt::anyevent::"
412 root 1.16 package/class when it finds the "urxvt" package/module is already
413     loaded.
414    
415     When AnyEvent is loaded and asked to find a suitable event model, it
416     will first check for the presence of urxvt by trying to "use" the
417     "urxvt::anyevent" module.
418    
419     The class should provide implementations for all watcher types. See
420     AnyEvent::Impl::EV (source code), AnyEvent::Impl::Glib (Source code) and
421     so on for actual examples. Use "perldoc -m AnyEvent::Impl::Glib" to see
422     the sources.
423    
424     If you don't provide "signal" and "child" watchers than AnyEvent will
425     provide suitable (hopefully) replacements.
426    
427     The above example isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt)
428     terminal emulator uses the above line as-is. An interface isn't included
429     in AnyEvent because it doesn't make sense outside the embedded
430     interpreter inside *rxvt-unicode*, and it is updated and maintained as
431     part of the *rxvt-unicode* distribution.
432 root 1.5
433 root 1.6 *rxvt-unicode* also cheats a bit by not providing blocking access to
434     condition variables: code blocking while waiting for a condition will
435     "die". This still works with most modules/usages, and blocking calls
436 root 1.16 must not be done in an interactive application, so it makes sense.
437 root 1.6
438 root 1.4 ENVIRONMENT VARIABLES
439     The following environment variables are used by this module:
440    
441 root 1.18 "PERL_ANYEVENT_VERBOSE"
442     When set to 2 or higher, cause AnyEvent to report to STDERR which
443     event model it chooses.
444    
445     "PERL_ANYEVENT_MODEL"
446     This can be used to specify the event model to be used by AnyEvent,
447     before autodetection and -probing kicks in. It must be a string
448     consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
449     gets prepended and the resulting module name is loaded and if the
450     load was successful, used as event model. If it fails to load
451     AnyEvent will proceed with autodetection and -probing.
452    
453     This functionality might change in future versions.
454    
455     For example, to force the pure perl model (AnyEvent::Impl::Perl) you
456     could start your program like this:
457    
458     PERL_ANYEVENT_MODEL=Perl perl ...
459 root 1.4
460 root 1.16 EXAMPLE PROGRAM
461     The following program uses an IO watcher to read data from STDIN, a
462     timer to display a message once per second, and a condition variable to
463     quit the program when the user enters quit:
464 root 1.2
465     use AnyEvent;
466    
467     my $cv = AnyEvent->condvar;
468    
469 root 1.16 my $io_watcher = AnyEvent->io (
470     fh => \*STDIN,
471     poll => 'r',
472     cb => sub {
473     warn "io event <$_[0]>\n"; # will always output <r>
474     chomp (my $input = <STDIN>); # read a line
475     warn "read: $input\n"; # output what has been read
476     $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i
477     },
478     );
479 root 1.2
480     my $time_watcher; # can only be used once
481    
482     sub new_timer {
483     $timer = AnyEvent->timer (after => 1, cb => sub {
484     warn "timeout\n"; # print 'timeout' about every second
485     &new_timer; # and restart the time
486     });
487     }
488    
489     new_timer; # create first timer
490    
491     $cv->wait; # wait until user enters /^q/i
492    
493 root 1.3 REAL-WORLD EXAMPLE
494     Consider the Net::FCP module. It features (among others) the following
495     API calls, which are to freenet what HTTP GET requests are to http:
496    
497     my $data = $fcp->client_get ($url); # blocks
498    
499     my $transaction = $fcp->txn_client_get ($url); # does not block
500     $transaction->cb ( sub { ... } ); # set optional result callback
501     my $data = $transaction->result; # possibly blocks
502    
503     The "client_get" method works like "LWP::Simple::get": it requests the
504     given URL and waits till the data has arrived. It is defined to be:
505    
506     sub client_get { $_[0]->txn_client_get ($_[1])->result }
507    
508     And in fact is automatically generated. This is the blocking API of
509     Net::FCP, and it works as simple as in any other, similar, module.
510    
511     More complicated is "txn_client_get": It only creates a transaction
512     (completion, result, ...) object and initiates the transaction.
513    
514     my $txn = bless { }, Net::FCP::Txn::;
515    
516     It also creates a condition variable that is used to signal the
517     completion of the request:
518    
519     $txn->{finished} = AnyAvent->condvar;
520    
521     It then creates a socket in non-blocking mode.
522    
523     socket $txn->{fh}, ...;
524     fcntl $txn->{fh}, F_SETFL, O_NONBLOCK;
525     connect $txn->{fh}, ...
526     and !$!{EWOULDBLOCK}
527     and !$!{EINPROGRESS}
528     and Carp::croak "unable to connect: $!\n";
529    
530 root 1.4 Then it creates a write-watcher which gets called whenever an error
531 root 1.3 occurs or the connection succeeds:
532    
533     $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w });
534    
535     And returns this transaction object. The "fh_ready_w" callback gets
536     called as soon as the event loop detects that the socket is ready for
537     writing.
538    
539     The "fh_ready_w" method makes the socket blocking again, writes the
540     request data and replaces the watcher by a read watcher (waiting for
541     reply data). The actual code is more complicated, but that doesn't
542     matter for this example:
543    
544     fcntl $txn->{fh}, F_SETFL, 0;
545     syswrite $txn->{fh}, $txn->{request}
546     or die "connection or write error";
547     $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
548    
549     Again, "fh_ready_r" waits till all data has arrived, and then stores the
550     result and signals any possible waiters that the request ahs finished:
551    
552     sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
553    
554     if (end-of-file or data complete) {
555     $txn->{result} = $txn->{buf};
556     $txn->{finished}->broadcast;
557 root 1.4 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
558 root 1.3 }
559    
560     The "result" method, finally, just waits for the finished signal (if the
561     request was already finished, it doesn't wait, of course, and returns
562     the data:
563    
564     $txn->{finished}->wait;
565 root 1.4 return $txn->{result};
566 root 1.3
567     The actual code goes further and collects all errors ("die"s,
568     exceptions) that occured during request processing. The "result" method
569 root 1.16 detects whether an exception as thrown (it is stored inside the $txn
570 root 1.3 object) and just throws the exception, which means connection errors and
571     other problems get reported tot he code that tries to use the result,
572     not in a random callback.
573    
574     All of this enables the following usage styles:
575    
576     1. Blocking:
577    
578     my $data = $fcp->client_get ($url);
579    
580 root 1.15 2. Blocking, but running in parallel:
581 root 1.3
582     my @datas = map $_->result,
583     map $fcp->txn_client_get ($_),
584     @urls;
585    
586     Both blocking examples work without the module user having to know
587     anything about events.
588    
589 root 1.15 3a. Event-based in a main program, using any supported event module:
590 root 1.3
591 root 1.15 use EV;
592 root 1.3
593     $fcp->txn_client_get ($url)->cb (sub {
594     my $txn = shift;
595     my $data = $txn->result;
596     ...
597     });
598    
599 root 1.15 EV::loop;
600 root 1.3
601     3b. The module user could use AnyEvent, too:
602    
603     use AnyEvent;
604    
605     my $quit = AnyEvent->condvar;
606    
607     $fcp->txn_client_get ($url)->cb (sub {
608     ...
609     $quit->broadcast;
610     });
611    
612     $quit->wait;
613    
614 root 1.18 FORK
615     Most event libraries are not fork-safe. The ones who are usually are
616     because they are so inefficient. Only EV is fully fork-aware.
617    
618     If you have to fork, you must either do so *before* creating your first
619     watcher OR you must not use AnyEvent at all in the child.
620    
621     SECURITY CONSIDERATIONS
622     AnyEvent can be forced to load any event model via
623     $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used
624     to execute arbitrary code or directly gain access, it can easily be used
625     to make the program hang or malfunction in subtle ways, as AnyEvent
626     watchers will not be active when the program uses a different event
627     model than specified in the variable.
628    
629     You can make AnyEvent completely ignore this variable by deleting it
630     before the first watcher gets created, e.g. with a "BEGIN" block:
631    
632     BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
633    
634     use AnyEvent;
635    
636 root 1.2 SEE ALSO
637 root 1.15 Event modules: Coro::EV, EV, EV::Glib, Glib::EV, Coro::Event, Event,
638 root 1.18 Glib::Event, Glib, Coro, Tk, Event::Lib, Qt.
639 root 1.3
640 root 1.15 Implementations: AnyEvent::Impl::CoroEV, AnyEvent::Impl::EV,
641     AnyEvent::Impl::CoroEvent, AnyEvent::Impl::Event, AnyEvent::Impl::Glib,
642 root 1.18 AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, AnyEvent::Impl::EventLib,
643     AnyEvent::Impl::Qt.
644 root 1.3
645 root 1.15 Nontrivial usage examples: Net::FCP, Net::XMPP2.
646 root 1.2
647 root 1.17 AUTHOR
648     Marc Lehmann <schmorp@schmorp.de>
649     http://home.schmorp.de/
650 root 1.2