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

Comparing AnyEvent/README (file contents):
Revision 1.23 by root, Mon May 26 06:04:38 2008 UTC vs.
Revision 1.27 by root, Sun Jun 22 12:17:47 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.
499 $cb = $cv->cb ([new callback]) 572 $cb = $cv->cb ([new callback])
500 This is a mutator function that returns the callback set and 573 This is a mutator function that returns the callback set and
501 optionally replaces it before doing so. 574 optionally replaces it before doing so.
502 575
503 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.
504 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
505 callback or at any later time is guaranteed not to block. 579 any later time is guaranteed not to block.
506 580
507GLOBAL VARIABLES AND FUNCTIONS 581GLOBAL VARIABLES AND FUNCTIONS
508 $AnyEvent::MODEL 582 $AnyEvent::MODEL
509 Contains "undef" until the first watcher is being created. Then it 583 Contains "undef" until the first watcher is being created. Then it
510 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
637 more. 711 more.
638 712
639 AnyEvent::DNS 713 AnyEvent::DNS
640 Provides rich asynchronous DNS resolver capabilities. 714 Provides rich asynchronous DNS resolver capabilities.
641 715
716 AnyEvent::HTTP
717 A simple-to-use HTTP library that is capable of making a lot of
718 concurrent HTTP requests.
719
642 AnyEvent::HTTPD 720 AnyEvent::HTTPD
643 Provides a simple web application server framework. 721 Provides a simple web application server framework.
644 722
645 AnyEvent::FastPing 723 AnyEvent::FastPing
646 The fastest ping in the west. 724 The fastest ping in the west.
725
726 AnyEvent::DBI
727 Executes DBI requests asynchronously in a proxy process.
647 728
648 Net::IRC3 729 Net::IRC3
649 AnyEvent based IRC client module family. 730 AnyEvent based IRC client module family.
650 731
651 Net::XMPP2 732 Net::XMPP2
743 This functionality might change in future versions. 824 This functionality might change in future versions.
744 825
745 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 826 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
746 could start your program like this: 827 could start your program like this:
747 828
748 PERL_ANYEVENT_MODEL=Perl perl ... 829 PERL_ANYEVENT_MODEL=Perl perl ...
749 830
750 "PERL_ANYEVENT_PROTOCOLS" 831 "PERL_ANYEVENT_PROTOCOLS"
751 Used by both AnyEvent::DNS and AnyEvent::Socket to determine 832 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
752 preferences for IPv4 or IPv6. The default is unspecified (and might 833 preferences for IPv4 or IPv6. The default is unspecified (and might
753 change, or be the result of auto probing). 834 change, or be the result of auto probing).
775 but some (broken) firewalls drop such DNS packets, which is why it 856 but some (broken) firewalls drop such DNS packets, which is why it
776 is off by default. 857 is off by default.
777 858
778 Setting this variable to 1 will cause AnyEvent::DNS to announce 859 Setting this variable to 1 will cause AnyEvent::DNS to announce
779 EDNS0 in its DNS requests. 860 EDNS0 in its DNS requests.
861
862 "PERL_ANYEVENT_MAX_FORKS"
863 The maximum number of child processes that
864 "AnyEvent::Util::fork_call" will create in parallel.
780 865
781EXAMPLE PROGRAM 866EXAMPLE PROGRAM
782 The following program uses an I/O watcher to read data from STDIN, a 867 The following program uses an I/O watcher to read data from STDIN, a
783 timer to display a message once per second, and a condition variable to 868 timer to display a message once per second, and a condition variable to
784 quit the program when the user enters quit: 869 quit the program when the user enters quit:
1190 model than specified in the variable. 1275 model than specified in the variable.
1191 1276
1192 You can make AnyEvent completely ignore this variable by deleting it 1277 You can make AnyEvent completely ignore this variable by deleting it
1193 before the first watcher gets created, e.g. with a "BEGIN" block: 1278 before the first watcher gets created, e.g. with a "BEGIN" block:
1194 1279
1195 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } 1280 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
1196 1281
1197 use AnyEvent; 1282 use AnyEvent;
1198 1283
1199 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1284 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1200 be used to probe what backend is used and gain other information (which 1285 be used to probe what backend is used and gain other information (which
1201 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1286 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1202 1287
1288BUGS
1289 Perl 5.8 has numerous memleaks that sometimes hit this module and are
1290 hard to work around. If you suffer from memleaks, first upgrade to Perl
1291 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other
1292 annoying mamleaks, such as leaking on "map" and "grep" but it is usually
1293 not as pronounced).
1294
1203SEE ALSO 1295SEE ALSO
1204 Utility functions: AnyEvent::Util. 1296 Utility functions: AnyEvent::Util.
1205 1297
1206 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1298 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk,
1207 Event::Lib, Qt, POE. 1299 Event::Lib, Qt, POE.
1218 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1310 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1219 1311
1220 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS. 1312 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1221 1313
1222AUTHOR 1314AUTHOR
1223 Marc Lehmann <schmorp@schmorp.de> 1315 Marc Lehmann <schmorp@schmorp.de>
1224 http://home.schmorp.de/ 1316 http://home.schmorp.de/
1225 1317

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines