ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/README
(Generate patch)

Comparing AnyEvent/README (file contents):
Revision 1.22 by root, Sat May 24 17:58:33 2008 UTC vs.
Revision 1.26 by root, Fri Jun 6 11:13:07 2008 UTC

1=> NAME 1NAME
2 AnyEvent - provide framework for multiple event loops 2 AnyEvent - provide framework for multiple event loops
3 3
4 EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event 4 EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event
5 loops 5 loops
6 6
16 }); 16 });
17 17
18 my $w = AnyEvent->condvar; # stores whether a condition was flagged 18 my $w = AnyEvent->condvar; # stores whether a condition was flagged
19 $w->send; # wake up current and all future recv's 19 $w->send; # wake up current and all future recv's
20 $w->recv; # enters "main loop" till $condvar gets ->send 20 $w->recv; # enters "main loop" till $condvar gets ->send
21
22INTRODUCTION/TUTORIAL
23 This manpage is mainly a reference manual. If you are interested in a
24 tutorial or some gentle introduction, have a look at the AnyEvent::Intro
25 manpage.
21 26
22WHY YOU SHOULD USE THIS MODULE (OR NOT) 27WHY YOU SHOULD USE THIS MODULE (OR NOT)
23 Glib, POE, IO::Async, Event... CPAN offers event models by the dozen 28 Glib, POE, IO::Async, Event... CPAN offers event models by the dozen
24 nowadays. So what is different about AnyEvent? 29 nowadays. So what is different about AnyEvent?
25 30
46 that isn't itself. What's worse, all the potential users of your module 51 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. 52 are *also* forced to use the same event loop you use.
48 53
49 AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works 54 AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works
50 fine. AnyEvent + Tk works fine etc. etc. but none of these work together 55 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 56 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. 57 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 58 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 59 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 60 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). 61 event loops to AnyEvent, too, so it is future-proof).
60 modules, you get an enormous amount of code and strict rules you have to 65 modules, you get an enormous amount of code and strict rules you have to
61 follow. AnyEvent, on the other hand, is lean and up to the point, by 66 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 67 only offering the functionality that is necessary, in as thin as a
63 wrapper as technically possible. 68 wrapper as technically possible.
64 69
70 Of course, AnyEvent comes with a big (and fully optional!) toolbox of
71 useful functionality, such as an asynchronous DNS resolver, 100%
72 non-blocking connects (even with TLS/SSL, IPv6 and on broken platforms
73 such as Windows) and lots of real-world knowledge and workarounds for
74 platform bugs and differences.
75
65 Of course, if you want lots of policy (this can arguably be somewhat 76 Now, if you *do 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 77 useful) and you want to force your users to use the one and only event
67 model, you should *not* use this module. 78 model, you should *not* use this module.
68 79
69DESCRIPTION 80DESCRIPTION
70 AnyEvent provides an identical interface to multiple event loops. This 81 AnyEvent provides an identical interface to multiple event loops. This
99 starts using it, all bets are off. Maybe you should tell their authors 110 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... 111 to use AnyEvent so their modules work together with others seamlessly...
101 112
102 The pure-perl implementation of AnyEvent is called 113 The pure-perl implementation of AnyEvent is called
103 "AnyEvent::Impl::Perl". Like other event modules you can load it 114 "AnyEvent::Impl::Perl". Like other event modules you can load it
104 explicitly. 115 explicitly and enjoy the high availability of that event loop :)
105 116
106WATCHERS 117WATCHERS
107 AnyEvent has the central concept of a *watcher*, which is an object that 118 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 119 stores relevant data for each kind of event you are waiting for, such as
109 the callback to call, the file handle to watch, etc. 120 the callback to call, the file handle to watch, etc.
122 Many watchers either are used with "recursion" (repeating timers for 133 Many watchers either are used with "recursion" (repeating timers for
123 example), or need to refer to their watcher object in other ways. 134 example), or need to refer to their watcher object in other ways.
124 135
125 An any way to achieve that is this pattern: 136 An any way to achieve that is this pattern:
126 137
127 my $w; $w = AnyEvent->type (arg => value ..., cb => sub { 138 my $w; $w = AnyEvent->type (arg => value ..., cb => sub {
128 # you can use $w here, for example to undef it 139 # you can use $w here, for example to undef it
129 undef $w; 140 undef $w;
130 }); 141 });
131 142
132 Note that "my $w; $w =" combination. This is necessary because in Perl, 143 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 144 my variables are only visible after the statement in which they are
134 declared. 145 declared.
135 146
220 on wallclock time) timers. 231 on wallclock time) timers.
221 232
222 AnyEvent always prefers relative timers, if available, matching the 233 AnyEvent always prefers relative timers, if available, matching the
223 AnyEvent API. 234 AnyEvent API.
224 235
236 AnyEvent has two additional methods that return the "current time":
237
238 AnyEvent->time
239 This returns the "current wallclock time" as a fractional number of
240 seconds since the Epoch (the same thing as "time" or
241 "Time::HiRes::time" return, and the result is guaranteed to be
242 compatible with those).
243
244 It progresses independently of any event loop processing, i.e. each
245 call will check the system clock, which usually gets updated
246 frequently.
247
248 AnyEvent->now
249 This also returns the "current wallclock time", but unlike "time",
250 above, this value might change only once per event loop iteration,
251 depending on the event loop (most return the same time as "time",
252 above). This is the time that AnyEvent's timers get scheduled
253 against.
254
255 *In almost all cases (in all cases if you don't care), this is the
256 function to call when you want to know the current time.*
257
258 This function is also often faster then "AnyEvent->time", and thus
259 the preferred method if you want some timestamp (for example,
260 AnyEvent::Handle uses this to update it's activity timeouts).
261
262 The rest of this section is only of relevance if you try to be very
263 exact with your timing, you can skip it without bad conscience.
264
265 For a practical example of when these times differ, consider
266 Event::Lib and EV and the following set-up:
267
268 The event loop is running and has just invoked one of your callback
269 at time=500 (assume no other callbacks delay processing). In your
270 callback, you wait a second by executing "sleep 1" (blocking the
271 process for a second) and then (at time=501) you create a relative
272 timer that fires after three seconds.
273
274 With Event::Lib, "AnyEvent->time" and "AnyEvent->now" will both
275 return 501, because that is the current time, and the timer will be
276 scheduled to fire at time=504 (501 + 3).
277
278 With EV, "AnyEvent->time" returns 501 (as that is the current time),
279 but "AnyEvent->now" returns 500, as that is the time the last event
280 processing phase started. With EV, your timer gets scheduled to run
281 at time=503 (500 + 3).
282
283 In one sense, Event::Lib is more exact, as it uses the current time
284 regardless of any delays introduced by event processing. However,
285 most callbacks do not expect large delays in processing, so this
286 causes a higher drift (and a lot more system calls to get the
287 current time).
288
289 In another sense, EV is more exact, as your timer will be scheduled
290 at the same time, regardless of how long event processing actually
291 took.
292
293 In either case, if you care (and in most cases, you don't), then you
294 can get whatever behaviour you want with any event loop, by taking
295 the difference between "AnyEvent->time" and "AnyEvent->now" into
296 account.
297
225 SIGNAL WATCHERS 298 SIGNAL WATCHERS
226 You can watch for signals using a signal watcher, "signal" is the signal 299 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 300 *name* without any "SIG" prefix, "cb" is the Perl callback to be invoked
228 whenever a signal occurs. 301 whenever a signal occurs.
229 302
269 an AnyEvent program, you *have* to create at least one watcher before 342 an AnyEvent program, you *have* to create at least one watcher before
270 you "fork" the child (alternatively, you can call "AnyEvent::detect"). 343 you "fork" the child (alternatively, you can call "AnyEvent::detect").
271 344
272 Example: fork a process and wait for it 345 Example: fork a process and wait for it
273 346
274 my $done = AnyEvent->condvar; 347 my $done = AnyEvent->condvar;
275 348
276 my $pid = fork or exit 5; 349 my $pid = fork or exit 5;
277 350
278 my $w = AnyEvent->child ( 351 my $w = AnyEvent->child (
279 pid => $pid, 352 pid => $pid,
280 cb => sub { 353 cb => sub {
281 my ($pid, $status) = @_; 354 my ($pid, $status) = @_;
282 warn "pid $pid exited with status $status"; 355 warn "pid $pid exited with status $status";
283 $done->send; 356 $done->send;
284 }, 357 },
285 ); 358 );
286 359
287 # do something else, then wait for process exit 360 # do something else, then wait for process exit
288 $done->recv; 361 $done->recv;
289 362
290 CONDITION VARIABLES 363 CONDITION VARIABLES
291 If you are familiar with some event loops you will know that all of them 364 If you are familiar with some event loops you will know that all of them
292 require you to run some blocking "loop", "run" or similar function that 365 require you to run some blocking "loop", "run" or similar function that
293 will actively watch for new events and call your callbacks. 366 will actively watch for new events and call your callbacks.
303 "cb", which specifies a callback to be called when the condition 376 "cb", which specifies a callback to be called when the condition
304 variable becomes true. 377 variable becomes true.
305 378
306 After creation, the condition variable is "false" until it becomes 379 After creation, the condition variable is "false" until it becomes
307 "true" by calling the "send" method (or calling the condition variable 380 "true" by calling the "send" method (or calling the condition variable
308 as if it were a callback). 381 as if it were a callback, read about the caveats in the description for
382 the "->send" method).
309 383
310 Condition variables are similar to callbacks, except that you can 384 Condition variables are similar to callbacks, except that you can
311 optionally wait for them. They can also be called merge points - points 385 optionally wait for them. They can also be called merge points - points
312 in time where multiple outstanding events have been processed. And yet 386 in time where multiple outstanding events have been processed. And yet
313 another way to call them is transactions - each condition variable can 387 another way to call them is transactions - each condition variable can
383 Any arguments passed to the "send" call will be returned by all 457 Any arguments passed to the "send" call will be returned by all
384 future "->recv" calls. 458 future "->recv" calls.
385 459
386 Condition variables are overloaded so one can call them directly (as 460 Condition variables are overloaded so one can call them directly (as
387 a code reference). Calling them directly is the same as calling 461 a code reference). Calling them directly is the same as calling
388 "send". 462 "send". Note, however, that many C-based event loops do not handle
463 overloading, so as tempting as it may be, passing a condition
464 variable instead of a callback does not work. Both the pure perl and
465 EV loops support overloading, however, as well as all functions that
466 use perl to invoke a callback (as in AnyEvent::Socket and
467 AnyEvent::DNS for example).
389 468
390 $cv->croak ($error) 469 $cv->croak ($error)
391 Similar to send, but causes all call's to "->recv" to invoke 470 Similar to send, but causes all call's to "->recv" to invoke
392 "Carp::croak" with the given error message/object/scalar. 471 "Carp::croak" with the given error message/object/scalar.
393 472
493 $cb = $cv->cb ([new callback]) 572 $cb = $cv->cb ([new callback])
494 This is a mutator function that returns the callback set and 573 This is a mutator function that returns the callback set and
495 optionally replaces it before doing so. 574 optionally replaces it before doing so.
496 575
497 The callback will be called when the condition becomes "true", i.e. 576 The callback will be called when the condition becomes "true", i.e.
498 when "send" or "croak" are called. Calling "recv" inside the 577 when "send" or "croak" are called, with the only argument being the
578 condition variable itself. Calling "recv" inside the callback or at
499 callback or at any later time is guaranteed not to block. 579 any later time is guaranteed not to block.
500 580
501GLOBAL VARIABLES AND FUNCTIONS 581GLOBAL VARIABLES AND FUNCTIONS
502 $AnyEvent::MODEL 582 $AnyEvent::MODEL
503 Contains "undef" until the first watcher is being created. Then it 583 Contains "undef" until the first watcher is being created. Then it
504 contains the event model that is being used, which is the name of 584 contains the event model that is being used, which is the name of
578 If it doesn't care, it can just "use AnyEvent" and use it itself, or not 658 If it doesn't care, it can just "use AnyEvent" and use it itself, or not
579 do anything special (it does not need to be event-based) and let 659 do anything special (it does not need to be event-based) and let
580 AnyEvent decide which implementation to chose if some module relies on 660 AnyEvent decide which implementation to chose if some module relies on
581 it. 661 it.
582 662
583 If the main program relies on a specific event model. For example, in 663 If the main program relies on a specific event model - for example, in
584 Gtk2 programs you have to rely on the Glib module. You should load the 664 Gtk2 programs you have to rely on the Glib module - you should load the
585 event module before loading AnyEvent or any module that uses it: 665 event module before loading AnyEvent or any module that uses it:
586 generally speaking, you should load it as early as possible. The reason 666 generally speaking, you should load it as early as possible. The reason
587 is that modules might create watchers when they are loaded, and AnyEvent 667 is that modules might create watchers when they are loaded, and AnyEvent
588 will decide on the event model to use as soon as it creates watchers, 668 will decide on the event model to use as soon as it creates watchers,
589 and it might chose the wrong one unless you load the correct one 669 and it might chose the wrong one unless you load the correct one
590 yourself. 670 yourself.
591 671
592 You can chose to use a rather inefficient pure-perl implementation by 672 You can chose to use a pure-perl implementation by loading the
593 loading the "AnyEvent::Impl::Perl" module, which gives you similar 673 "AnyEvent::Impl::Perl" module, which gives you similar behaviour
594 behaviour everywhere, but letting AnyEvent chose is generally better. 674 everywhere, but letting AnyEvent chose the model is generally better.
675
676 MAINLOOP EMULATION
677 Sometimes (often for short test scripts, or even standalone programs who
678 only want to use AnyEvent), you do not want to run a specific event
679 loop.
680
681 In that case, you can use a condition variable like this:
682
683 AnyEvent->condvar->recv;
684
685 This has the effect of entering the event loop and looping forever.
686
687 Note that usually your program has some exit condition, in which case it
688 is better to use the "traditional" approach of storing a condition
689 variable somewhere, waiting for it, and sending it when the program
690 should exit cleanly.
595 691
596OTHER MODULES 692OTHER MODULES
597 The following is a non-exhaustive list of additional modules that use 693 The following is a non-exhaustive list of additional modules that use
598 AnyEvent and can therefore be mixed easily with other AnyEvent modules 694 AnyEvent and can therefore be mixed easily with other AnyEvent modules
599 in the same program. Some of the modules come with AnyEvent, some are 695 in the same program. Some of the modules come with AnyEvent, some are
612 Provides various utility functions for (internet protocol) sockets, 708 Provides various utility functions for (internet protocol) sockets,
613 addresses and name resolution. Also functions to create non-blocking 709 addresses and name resolution. Also functions to create non-blocking
614 tcp connections or tcp servers, with IPv6 and SRV record support and 710 tcp connections or tcp servers, with IPv6 and SRV record support and
615 more. 711 more.
616 712
713 AnyEvent::DNS
714 Provides rich asynchronous DNS resolver capabilities.
715
716 AnyEvent::HTTP
717 A simple-to-use HTTP library that is capable of making a lot of
718 concurrent HTTP requests.
719
617 AnyEvent::HTTPD 720 AnyEvent::HTTPD
618 Provides a simple web application server framework. 721 Provides a simple web application server framework.
619
620 AnyEvent::DNS
621 Provides rich asynchronous DNS resolver capabilities.
622 722
623 AnyEvent::FastPing 723 AnyEvent::FastPing
624 The fastest ping in the west. 724 The fastest ping in the west.
625 725
626 Net::IRC3 726 Net::IRC3
721 This functionality might change in future versions. 821 This functionality might change in future versions.
722 822
723 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 823 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
724 could start your program like this: 824 could start your program like this:
725 825
726 PERL_ANYEVENT_MODEL=Perl perl ... 826 PERL_ANYEVENT_MODEL=Perl perl ...
727 827
728 "PERL_ANYEVENT_PROTOCOLS" 828 "PERL_ANYEVENT_PROTOCOLS"
729 Used by both AnyEvent::DNS and AnyEvent::Socket to determine 829 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
730 preferences for IPv4 or IPv6. The default is unspecified (and might 830 preferences for IPv4 or IPv6. The default is unspecified (and might
731 change, or be the result of auto probing). 831 change, or be the result of auto probing).
753 but some (broken) firewalls drop such DNS packets, which is why it 853 but some (broken) firewalls drop such DNS packets, which is why it
754 is off by default. 854 is off by default.
755 855
756 Setting this variable to 1 will cause AnyEvent::DNS to announce 856 Setting this variable to 1 will cause AnyEvent::DNS to announce
757 EDNS0 in its DNS requests. 857 EDNS0 in its DNS requests.
858
859 "PERL_ANYEVENT_MAX_FORKS"
860 The maximum number of child processes that
861 "AnyEvent::Util::fork_call" will create in parallel.
758 862
759EXAMPLE PROGRAM 863EXAMPLE PROGRAM
760 The following program uses an I/O watcher to read data from STDIN, a 864 The following program uses an I/O watcher to read data from STDIN, a
761 timer to display a message once per second, and a condition variable to 865 timer to display a message once per second, and a condition variable to
762 quit the program when the user enters quit: 866 quit the program when the user enters quit:
1168 model than specified in the variable. 1272 model than specified in the variable.
1169 1273
1170 You can make AnyEvent completely ignore this variable by deleting it 1274 You can make AnyEvent completely ignore this variable by deleting it
1171 before the first watcher gets created, e.g. with a "BEGIN" block: 1275 before the first watcher gets created, e.g. with a "BEGIN" block:
1172 1276
1173 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } 1277 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
1174 1278
1175 use AnyEvent; 1279 use AnyEvent;
1176 1280
1177 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1281 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1178 be used to probe what backend is used and gain other information (which 1282 be used to probe what backend is used and gain other information (which
1179 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1283 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1180 1284
1285BUGS
1286 Perl 5.8 has numerous memleaks that sometimes hit this module and are
1287 hard to work around. If you suffer from memleaks, first upgrade to Perl
1288 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other
1289 annoying mamleaks, such as leaking on "map" and "grep" but it is usually
1290 not as pronounced).
1291
1181SEE ALSO 1292SEE ALSO
1182 Utility functions: AnyEvent::Util. 1293 Utility functions: AnyEvent::Util.
1183 1294
1184 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1295 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk,
1185 Event::Lib, Qt, POE. 1296 Event::Lib, Qt, POE.
1196 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1307 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1197 1308
1198 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS. 1309 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1199 1310
1200AUTHOR 1311AUTHOR
1201 Marc Lehmann <schmorp@schmorp.de> 1312 Marc Lehmann <schmorp@schmorp.de>
1202 http://home.schmorp.de/ 1313 http://home.schmorp.de/
1203 1314

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines