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.25 by root, Tue Jun 3 09:02:46 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
743 This functionality might change in future versions. 817 This functionality might change in future versions.
744 818
745 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 819 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
746 could start your program like this: 820 could start your program like this:
747 821
748 PERL_ANYEVENT_MODEL=Perl perl ... 822 PERL_ANYEVENT_MODEL=Perl perl ...
749 823
750 "PERL_ANYEVENT_PROTOCOLS" 824 "PERL_ANYEVENT_PROTOCOLS"
751 Used by both AnyEvent::DNS and AnyEvent::Socket to determine 825 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
752 preferences for IPv4 or IPv6. The default is unspecified (and might 826 preferences for IPv4 or IPv6. The default is unspecified (and might
753 change, or be the result of auto probing). 827 change, or be the result of auto probing).
775 but some (broken) firewalls drop such DNS packets, which is why it 849 but some (broken) firewalls drop such DNS packets, which is why it
776 is off by default. 850 is off by default.
777 851
778 Setting this variable to 1 will cause AnyEvent::DNS to announce 852 Setting this variable to 1 will cause AnyEvent::DNS to announce
779 EDNS0 in its DNS requests. 853 EDNS0 in its DNS requests.
854
855 "PERL_ANYEVENT_MAX_FORKS"
856 The maximum number of child processes that
857 "AnyEvent::Util::fork_call" will create in parallel.
780 858
781EXAMPLE PROGRAM 859EXAMPLE PROGRAM
782 The following program uses an I/O watcher to read data from STDIN, a 860 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 861 timer to display a message once per second, and a condition variable to
784 quit the program when the user enters quit: 862 quit the program when the user enters quit:
1190 model than specified in the variable. 1268 model than specified in the variable.
1191 1269
1192 You can make AnyEvent completely ignore this variable by deleting it 1270 You can make AnyEvent completely ignore this variable by deleting it
1193 before the first watcher gets created, e.g. with a "BEGIN" block: 1271 before the first watcher gets created, e.g. with a "BEGIN" block:
1194 1272
1195 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } 1273 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
1196 1274
1197 use AnyEvent; 1275 use AnyEvent;
1198 1276
1199 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1277 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 1278 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). 1279 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1202 1280
1218 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1296 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1219 1297
1220 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS. 1298 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1221 1299
1222AUTHOR 1300AUTHOR
1223 Marc Lehmann <schmorp@schmorp.de> 1301 Marc Lehmann <schmorp@schmorp.de>
1224 http://home.schmorp.de/ 1302 http://home.schmorp.de/
1225 1303

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines