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

Comparing AnyEvent/README (file contents):
Revision 1.30 by root, Mon Sep 29 02:08:57 2008 UTC vs.
Revision 1.39 by root, Sun Jun 7 16:48:38 2009 UTC

1NAME 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 and POE are various supported
5 loops 5 event loops.
6 6
7SYNOPSIS 7SYNOPSIS
8 use AnyEvent; 8 use AnyEvent;
9 9
10 # file descriptor readable
10 my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { ... }); 11 my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... });
11 12
13 # one-shot or repeating timers
12 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); 14 my $w = AnyEvent->timer (after => $seconds, cb => sub { ... });
13 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... 15 my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ...
14 16
15 print AnyEvent->now; # prints current event loop time 17 print AnyEvent->now; # prints current event loop time
16 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time. 18 print AnyEvent->time; # think Time::HiRes::time or simply CORE::time.
17 19
20 # POSIX signal
18 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... }); 21 my $w = AnyEvent->signal (signal => "TERM", cb => sub { ... });
19 22
23 # child process exit
20 my $w = AnyEvent->child (pid => $pid, cb => sub { 24 my $w = AnyEvent->child (pid => $pid, cb => sub {
21 my ($pid, $status) = @_; 25 my ($pid, $status) = @_;
22 ... 26 ...
23 }); 27 });
28
29 # called when event loop idle (if applicable)
30 my $w = AnyEvent->idle (cb => sub { ... });
24 31
25 my $w = AnyEvent->condvar; # stores whether a condition was flagged 32 my $w = AnyEvent->condvar; # stores whether a condition was flagged
26 $w->send; # wake up current and all future recv's 33 $w->send; # wake up current and all future recv's
27 $w->recv; # enters "main loop" till $condvar gets ->send 34 $w->recv; # enters "main loop" till $condvar gets ->send
28 # use a condvar in callback mode: 35 # use a condvar in callback mode:
132 These watchers are normal Perl objects with normal Perl lifetime. After 139 These watchers are normal Perl objects with normal Perl lifetime. After
133 creating a watcher it will immediately "watch" for events and invoke the 140 creating a watcher it will immediately "watch" for events and invoke the
134 callback when the event occurs (of course, only when the event model is 141 callback when the event occurs (of course, only when the event model is
135 in control). 142 in control).
136 143
144 Note that callbacks must not permanently change global variables
145 potentially in use by the event loop (such as $_ or $[) and that
146 callbacks must not "die". The former is good programming practise in
147 Perl and the latter stems from the fact that exception handling differs
148 widely between event loops.
149
137 To disable the watcher you have to destroy it (e.g. by setting the 150 To disable the watcher you have to destroy it (e.g. by setting the
138 variable you store it in to "undef" or otherwise deleting all references 151 variable you store it in to "undef" or otherwise deleting all references
139 to it). 152 to it).
140 153
141 All watchers are created by calling a method on the "AnyEvent" class. 154 All watchers are created by calling a method on the "AnyEvent" class.
156 169
157 I/O WATCHERS 170 I/O WATCHERS
158 You can create an I/O watcher by calling the "AnyEvent->io" method with 171 You can create an I/O watcher by calling the "AnyEvent->io" method with
159 the following mandatory key-value pairs as arguments: 172 the following mandatory key-value pairs as arguments:
160 173
161 "fh" the Perl *file handle* (*not* file descriptor) to watch for events 174 "fh" is the Perl *file handle* (*not* file descriptor) to watch for
162 (AnyEvent might or might not keep a reference to this file handle). 175 events (AnyEvent might or might not keep a reference to this file
176 handle). Note that only file handles pointing to things for which
177 non-blocking operation makes sense are allowed. This includes sockets,
178 most character devices, pipes, fifos and so on, but not for example
179 files or block devices.
180
163 "poll" must be a string that is either "r" or "w", which creates a 181 "poll" must be a string that is either "r" or "w", which creates a
164 watcher waiting for "r"eadable or "w"ritable events, respectively. "cb" 182 watcher waiting for "r"eadable or "w"ritable events, respectively.
183
165 is the callback to invoke each time the file handle becomes ready. 184 "cb" is the callback to invoke each time the file handle becomes ready.
166 185
167 Although the callback might get passed parameters, their value and 186 Although the callback might get passed parameters, their value and
168 presence is undefined and you cannot rely on them. Portable AnyEvent 187 presence is undefined and you cannot rely on them. Portable AnyEvent
169 callbacks cannot use arguments passed to I/O watcher callbacks. 188 callbacks cannot use arguments passed to I/O watcher callbacks.
170 189
301 320
302 In either case, if you care (and in most cases, you don't), then you 321 In either case, if you care (and in most cases, you don't), then you
303 can get whatever behaviour you want with any event loop, by taking 322 can get whatever behaviour you want with any event loop, by taking
304 the difference between "AnyEvent->time" and "AnyEvent->now" into 323 the difference between "AnyEvent->time" and "AnyEvent->now" into
305 account. 324 account.
325
326 AnyEvent->now_update
327 Some event loops (such as EV or AnyEvent::Impl::Perl) cache the
328 current time for each loop iteration (see the discussion of
329 AnyEvent->now, above).
330
331 When a callback runs for a long time (or when the process sleeps),
332 then this "current" time will differ substantially from the real
333 time, which might affect timers and time-outs.
334
335 When this is the case, you can call this method, which will update
336 the event loop's idea of "current time".
337
338 Note that updating the time *might* cause some events to be handled.
306 339
307 SIGNAL WATCHERS 340 SIGNAL WATCHERS
308 You can watch for signals using a signal watcher, "signal" is the signal 341 You can watch for signals using a signal watcher, "signal" is the signal
309 *name* in uppercase and without any "SIG" prefix, "cb" is the Perl 342 *name* in uppercase and without any "SIG" prefix, "cb" is the Perl
310 callback to be invoked whenever a signal occurs. 343 callback to be invoked whenever a signal occurs.
374 ); 407 );
375 408
376 # do something else, then wait for process exit 409 # do something else, then wait for process exit
377 $done->recv; 410 $done->recv;
378 411
412 IDLE WATCHERS
413 Sometimes there is a need to do something, but it is not so important to
414 do it instantly, but only when there is nothing better to do. This
415 "nothing better to do" is usually defined to be "no other events need
416 attention by the event loop".
417
418 Idle watchers ideally get invoked when the event loop has nothing better
419 to do, just before it would block the process to wait for new events.
420 Instead of blocking, the idle watcher is invoked.
421
422 Most event loops unfortunately do not really support idle watchers (only
423 EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent
424 will simply call the callback "from time to time".
425
426 Example: read lines from STDIN, but only process them when the program
427 is otherwise idle:
428
429 my @lines; # read data
430 my $idle_w;
431 my $io_w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
432 push @lines, scalar <STDIN>;
433
434 # start an idle watcher, if not already done
435 $idle_w ||= AnyEvent->idle (cb => sub {
436 # handle only one line, when there are lines left
437 if (my $line = shift @lines) {
438 print "handled when idle: $line";
439 } else {
440 # otherwise disable the idle watcher again
441 undef $idle_w;
442 }
443 });
444 });
445
379 CONDITION VARIABLES 446 CONDITION VARIABLES
380 If you are familiar with some event loops you will know that all of them 447 If you are familiar with some event loops you will know that all of them
381 require you to run some blocking "loop", "run" or similar function that 448 require you to run some blocking "loop", "run" or similar function that
382 will actively watch for new events and call your callbacks. 449 will actively watch for new events and call your callbacks.
383 450
777 844
778 AnyEvent::IGS 845 AnyEvent::IGS
779 A non-blocking interface to the Internet Go Server protocol (used by 846 A non-blocking interface to the Internet Go Server protocol (used by
780 App::IGS). 847 App::IGS).
781 848
849 AnyEvent::IRC
850 AnyEvent based IRC client module family (replacing the older
782 Net::IRC3 851 Net::IRC3).
783 AnyEvent based IRC client module family.
784 852
785 Net::XMPP2 853 Net::XMPP2
786 AnyEvent based XMPP (Jabber protocol) module family. 854 AnyEvent based XMPP (Jabber protocol) module family.
787 855
788 Net::FCP 856 Net::FCP
869 mentioned will be used, and preference will be given to protocols 937 mentioned will be used, and preference will be given to protocols
870 mentioned earlier in the list. 938 mentioned earlier in the list.
871 939
872 This variable can effectively be used for denial-of-service attacks 940 This variable can effectively be used for denial-of-service attacks
873 against local programs (e.g. when setuid), although the impact is 941 against local programs (e.g. when setuid), although the impact is
874 likely small, as the program has to handle connection errors 942 likely small, as the program has to handle conenction and other
875 already- 943 failures anyways.
876 944
877 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over 945 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over
878 IPv6, but support both and try to use both. 946 IPv6, but support both and try to use both.
879 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to 947 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to
880 resolve or contact IPv6 addresses. 948 resolve or contact IPv6 addresses.
1130 *destroy* is the time, in microseconds, that it takes to destroy a 1198 *destroy* is the time, in microseconds, that it takes to destroy a
1131 single watcher. 1199 single watcher.
1132 1200
1133 Results 1201 Results
1134 name watchers bytes create invoke destroy comment 1202 name watchers bytes create invoke destroy comment
1135 EV/EV 400000 244 0.56 0.46 0.31 EV native interface 1203 EV/EV 400000 224 0.47 0.35 0.27 EV native interface
1136 EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers 1204 EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers
1137 CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal 1205 CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal
1138 Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation 1206 Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation
1139 Event/Event 16000 516 31.88 31.30 0.85 Event native interface 1207 Event/Event 16000 517 32.20 31.80 0.81 Event native interface
1140 Event/Any 16000 590 35.75 31.42 1.08 Event + AnyEvent watchers 1208 Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers
1141 Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour 1209 Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour
1142 Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers 1210 Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers
1143 POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event 1211 POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event
1144 POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select 1212 POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select
1145 1213
1146 Discussion 1214 Discussion
1147 The benchmark does *not* measure scalability of the event loop very 1215 The benchmark does *not* measure scalability of the event loop very
1148 well. For example, a select-based event loop (such as the pure perl one) 1216 well. For example, a select-based event loop (such as the pure perl one)
1149 can never compete with an event loop that uses epoll when the number of 1217 can never compete with an event loop that uses epoll when the number of
1330 1398
1331 Summary 1399 Summary
1332 * C-based event loops perform very well with small number of watchers, 1400 * C-based event loops perform very well with small number of watchers,
1333 as the management overhead dominates. 1401 as the management overhead dominates.
1334 1402
1403SIGNALS
1404 AnyEvent currently installs handlers for these signals:
1405
1406 SIGCHLD
1407 A handler for "SIGCHLD" is installed by AnyEvent's child watcher
1408 emulation for event loops that do not support them natively. Also,
1409 some event loops install a similar handler.
1410
1411 SIGPIPE
1412 A no-op handler is installed for "SIGPIPE" when $SIG{PIPE} is
1413 "undef" when AnyEvent gets loaded.
1414
1415 The rationale for this is that AnyEvent users usually do not really
1416 depend on SIGPIPE delivery (which is purely an optimisation for
1417 shell use, or badly-written programs), but "SIGPIPE" can cause
1418 spurious and rare program exits as a lot of people do not expect
1419 "SIGPIPE" when writing to some random socket.
1420
1421 The rationale for installing a no-op handler as opposed to ignoring
1422 it is that this way, the handler will be restored to defaults on
1423 exec.
1424
1425 Feel free to install your own handler, or reset it to defaults.
1426
1335FORK 1427FORK
1336 Most event libraries are not fork-safe. The ones who are usually are 1428 Most event libraries are not fork-safe. The ones who are usually are
1337 because they rely on inefficient but fork-safe "select" or "poll" calls. 1429 because they rely on inefficient but fork-safe "select" or "poll" calls.
1338 Only EV is fully fork-aware. 1430 Only EV is fully fork-aware.
1339 1431
1362 1454
1363BUGS 1455BUGS
1364 Perl 5.8 has numerous memleaks that sometimes hit this module and are 1456 Perl 5.8 has numerous memleaks that sometimes hit this module and are
1365 hard to work around. If you suffer from memleaks, first upgrade to Perl 1457 hard to work around. If you suffer from memleaks, first upgrade to Perl
1366 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other 1458 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other
1367 annoying mamleaks, such as leaking on "map" and "grep" but it is usually 1459 annoying memleaks, such as leaking on "map" and "grep" but it is usually
1368 not as pronounced). 1460 not as pronounced).
1369 1461
1370SEE ALSO 1462SEE ALSO
1371 Utility functions: AnyEvent::Util. 1463 Utility functions: AnyEvent::Util.
1372 1464

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines