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

Comparing AnyEvent/README (file contents):
Revision 1.21 by root, Sat May 17 21:34:15 2008 UTC vs.
Revision 1.22 by root, Sat May 24 17:58:33 2008 UTC

1NAME 1=> NAME
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
55 those use one of the supported event loops. It is trivial to add new 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). 56 event loops to AnyEvent, too, so it is future-proof).
57 57
58 In addition to being free of having to use *the one and only true event 58 In addition to being free of having to use *the one and only true event
59 model*, AnyEvent also is free of bloat and policy: with POE or similar 59 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 60 modules, you get an enormous amount of code and strict rules you have to
61 to follow. AnyEvent, on the other hand, is lean and up to the point, by 61 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 62 only offering the functionality that is necessary, in as thin as a
63 wrapper as technically possible. 63 wrapper as technically possible.
64 64
65 Of course, if you want lots of policy (this can arguably be somewhat 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 66 useful) and you want to force your users to use the one and only event
104 explicitly. 104 explicitly.
105 105
106WATCHERS 106WATCHERS
107 AnyEvent has the central concept of a *watcher*, which is an object that 107 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 108 stores relevant data for each kind of event you are waiting for, such as
109 the callback to call, the filehandle to watch, etc. 109 the callback to call, the file handle to watch, etc.
110 110
111 These watchers are normal Perl objects with normal Perl lifetime. After 111 These watchers are normal Perl objects with normal Perl lifetime. After
112 creating a watcher it will immediately "watch" for events and invoke the 112 creating a watcher it will immediately "watch" for events and invoke the
113 callback when the event occurs (of course, only when the event model is 113 callback when the event occurs (of course, only when the event model is
114 in control). 114 in control).
229 229
230 Although the callback might get passed parameters, their value and 230 Although the callback might get passed parameters, their value and
231 presence is undefined and you cannot rely on them. Portable AnyEvent 231 presence is undefined and you cannot rely on them. Portable AnyEvent
232 callbacks cannot use arguments passed to signal watcher callbacks. 232 callbacks cannot use arguments passed to signal watcher callbacks.
233 233
234 Multiple signal occurances can be clumped together into one callback 234 Multiple signal occurrences can be clumped together into one callback
235 invocation, and callback invocation will be synchronous. synchronous 235 invocation, and callback invocation will be synchronous. Synchronous
236 means that it might take a while until the signal gets handled by the 236 means that it might take a while until the signal gets handled by the
237 process, but it is guarenteed not to interrupt any other callbacks. 237 process, but it is guaranteed not to interrupt any other callbacks.
238 238
239 The main advantage of using these watchers is that you can share a 239 The main advantage of using these watchers is that you can share a
240 signal between multiple watchers. 240 signal between multiple watchers.
241 241
242 This watcher might use %SIG, so programs overwriting those signals 242 This watcher might use %SIG, so programs overwriting those signals
301 Condition variables can be created by calling the "AnyEvent->condvar" 301 Condition variables can be created by calling the "AnyEvent->condvar"
302 method, usually without arguments. The only argument pair allowed is 302 method, usually without arguments. The only argument pair allowed is
303 "cb", which specifies a callback to be called when the condition 303 "cb", which specifies a callback to be called when the condition
304 variable becomes true. 304 variable becomes true.
305 305
306 After creation, the conditon variable is "false" until it becomes "true" 306 After creation, the condition variable is "false" until it becomes
307 by calling the "send" method. 307 "true" by calling the "send" method (or calling the condition variable
308 as if it were a callback).
308 309
309 Condition variables are similar to callbacks, except that you can 310 Condition variables are similar to callbacks, except that you can
310 optionally wait for them. They can also be called merge points - points 311 optionally wait for them. They can also be called merge points - points
311 in time where multiple outstandign events have been processed. And yet 312 in time where multiple outstanding events have been processed. And yet
312 another way to call them is transations - each condition variable can be 313 another way to call them is transactions - each condition variable can
313 used to represent a transaction, which finishes at some point and 314 be used to represent a transaction, which finishes at some point and
314 delivers a result. 315 delivers a result.
315 316
316 Condition variables are very useful to signal that something has 317 Condition variables are very useful to signal that something has
317 finished, for example, if you write a module that does asynchronous http 318 finished, for example, if you write a module that does asynchronous http
318 requests, then a condition variable would be the ideal candidate to 319 requests, then a condition variable would be the ideal candidate to
323 you can block your main program until an event occurs - for example, you 324 you can block your main program until an event occurs - for example, you
324 could "->recv" in your main program until the user clicks the Quit 325 could "->recv" in your main program until the user clicks the Quit
325 button of your app, which would "->send" the "quit" event. 326 button of your app, which would "->send" the "quit" event.
326 327
327 Note that condition variables recurse into the event loop - if you have 328 Note that condition variables recurse into the event loop - if you have
328 two pieces of code that call "->recv" in a round-robbin fashion, you 329 two pieces of code that call "->recv" in a round-robin fashion, you
329 lose. Therefore, condition variables are good to export to your caller, 330 lose. Therefore, condition variables are good to export to your caller,
330 but you should avoid making a blocking wait yourself, at least in 331 but you should avoid making a blocking wait yourself, at least in
331 callbacks, as this asks for trouble. 332 callbacks, as this asks for trouble.
332 333
333 Condition variables are represented by hash refs in perl, and the keys 334 Condition variables are represented by hash refs in perl, and the keys
338 339
339 There are two "sides" to a condition variable - the "producer side" 340 There are two "sides" to a condition variable - the "producer side"
340 which eventually calls "-> send", and the "consumer side", which waits 341 which eventually calls "-> send", and the "consumer side", which waits
341 for the send to occur. 342 for the send to occur.
342 343
343 Example: 344 Example: wait for a timer.
344 345
345 # wait till the result is ready 346 # wait till the result is ready
346 my $result_ready = AnyEvent->condvar; 347 my $result_ready = AnyEvent->condvar;
347 348
348 # do something such as adding a timer 349 # do something such as adding a timer
356 357
357 # this "blocks" (while handling events) till the callback 358 # this "blocks" (while handling events) till the callback
358 # calls send 359 # calls send
359 $result_ready->recv; 360 $result_ready->recv;
360 361
362 Example: wait for a timer, but take advantage of the fact that condition
363 variables are also code references.
364
365 my $done = AnyEvent->condvar;
366 my $delay = AnyEvent->timer (after => 5, cb => $done);
367 $done->recv;
368
361 METHODS FOR PRODUCERS 369 METHODS FOR PRODUCERS
362 These methods should only be used by the producing side, i.e. the 370 These methods should only be used by the producing side, i.e. the
363 code/module that eventually sends the signal. Note that it is also the 371 code/module that eventually sends the signal. Note that it is also the
364 producer side which creates the condvar in most cases, but it isn't 372 producer side which creates the condvar in most cases, but it isn't
365 uncommon for the consumer to create it as well. 373 uncommon for the consumer to create it as well.
372 If a callback has been set on the condition variable, it is called 380 If a callback has been set on the condition variable, it is called
373 immediately from within send. 381 immediately from within send.
374 382
375 Any arguments passed to the "send" call will be returned by all 383 Any arguments passed to the "send" call will be returned by all
376 future "->recv" calls. 384 future "->recv" calls.
385
386 Condition variables are overloaded so one can call them directly (as
387 a code reference). Calling them directly is the same as calling
388 "send".
377 389
378 $cv->croak ($error) 390 $cv->croak ($error)
379 Similar to send, but causes all call's to "->recv" to invoke 391 Similar to send, but causes all call's to "->recv" to invoke
380 "Carp::croak" with the given error message/object/scalar. 392 "Carp::croak" with the given error message/object/scalar.
381 393
427 (the loop doesn't execute once). 439 (the loop doesn't execute once).
428 440
429 This is the general pattern when you "fan out" into multiple 441 This is the general pattern when you "fan out" into multiple
430 subrequests: use an outer "begin"/"end" pair to set the callback and 442 subrequests: use an outer "begin"/"end" pair to set the callback and
431 ensure "end" is called at least once, and then, for each subrequest 443 ensure "end" is called at least once, and then, for each subrequest
432 you start, call "begin" and for eahc subrequest you finish, call 444 you start, call "begin" and for each subrequest you finish, call
433 "end". 445 "end".
434 446
435 METHODS FOR CONSUMERS 447 METHODS FOR CONSUMERS
436 These methods should only be used by the consuming side, i.e. the code 448 These methods should only be used by the consuming side, i.e. the code
437 awaits the condition. 449 awaits the condition.
453 (programs might want to do that to stay interactive), so *if you are 465 (programs might want to do that to stay interactive), so *if you are
454 using this from a module, never require a blocking wait*, but let 466 using this from a module, never require a blocking wait*, but let
455 the caller decide whether the call will block or not (for example, 467 the caller decide whether the call will block or not (for example,
456 by coupling condition variables with some kind of request results 468 by coupling condition variables with some kind of request results
457 and supporting callbacks so the caller knows that getting the result 469 and supporting callbacks so the caller knows that getting the result
458 will not block, while still suppporting blocking waits if the caller 470 will not block, while still supporting blocking waits if the caller
459 so desires). 471 so desires).
460 472
461 Another reason *never* to "->recv" in a module is that you cannot 473 Another reason *never* to "->recv" in a module is that you cannot
462 sensibly have two "->recv"'s in parallel, as that would require 474 sensibly have two "->recv"'s in parallel, as that would require
463 multiple interpreters or coroutines/threads, none of which 475 multiple interpreters or coroutines/threads, none of which
594 606
595 AnyEvent::Handle 607 AnyEvent::Handle
596 Provide read and write buffers and manages watchers for reads and 608 Provide read and write buffers and manages watchers for reads and
597 writes. 609 writes.
598 610
611 AnyEvent::Socket
612 Provides various utility functions for (internet protocol) sockets,
613 addresses and name resolution. Also functions to create non-blocking
614 tcp connections or tcp servers, with IPv6 and SRV record support and
615 more.
616
599 AnyEvent::HTTPD 617 AnyEvent::HTTPD
600 Provides a simple web application server framework. 618 Provides a simple web application server framework.
601 619
602 AnyEvent::DNS 620 AnyEvent::DNS
603 Provides asynchronous DNS resolver capabilities, beyond what 621 Provides rich asynchronous DNS resolver capabilities.
604 AnyEvent::Util offers.
605 622
606 AnyEvent::FastPing 623 AnyEvent::FastPing
607 The fastest ping in the west. 624 The fastest ping in the west.
608 625
609 Net::IRC3 626 Net::IRC3
693 When set to 2 or higher, cause AnyEvent to report to STDERR which 710 When set to 2 or higher, cause AnyEvent to report to STDERR which
694 event model it chooses. 711 event model it chooses.
695 712
696 "PERL_ANYEVENT_MODEL" 713 "PERL_ANYEVENT_MODEL"
697 This can be used to specify the event model to be used by AnyEvent, 714 This can be used to specify the event model to be used by AnyEvent,
698 before autodetection and -probing kicks in. It must be a string 715 before auto detection and -probing kicks in. It must be a string
699 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 716 consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
700 gets prepended and the resulting module name is loaded and if the 717 gets prepended and the resulting module name is loaded and if the
701 load was successful, used as event model. If it fails to load 718 load was successful, used as event model. If it fails to load
702 AnyEvent will proceed with autodetection and -probing. 719 AnyEvent will proceed with auto detection and -probing.
703 720
704 This functionality might change in future versions. 721 This functionality might change in future versions.
705 722
706 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 723 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
707 could start your program like this: 724 could start your program like this:
708 725
709 PERL_ANYEVENT_MODEL=Perl perl ... 726 PERL_ANYEVENT_MODEL=Perl perl ...
727
728 "PERL_ANYEVENT_PROTOCOLS"
729 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
730 preferences for IPv4 or IPv6. The default is unspecified (and might
731 change, or be the result of auto probing).
732
733 Must be set to a comma-separated list of protocols or address
734 families, current supported: "ipv4" and "ipv6". Only protocols
735 mentioned will be used, and preference will be given to protocols
736 mentioned earlier in the list.
737
738 This variable can effectively be used for denial-of-service attacks
739 against local programs (e.g. when setuid), although the impact is
740 likely small, as the program has to handle connection errors
741 already-
742
743 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over
744 IPv6, but support both and try to use both.
745 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to
746 resolve or contact IPv6 addresses.
747 "PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4" support either IPv4 or IPv6, but
748 prefer IPv6 over IPv4.
749
750 "PERL_ANYEVENT_EDNS0"
751 Used by AnyEvent::DNS to decide whether to use the EDNS0 extension
752 for DNS. This extension is generally useful to reduce DNS traffic,
753 but some (broken) firewalls drop such DNS packets, which is why it
754 is off by default.
755
756 Setting this variable to 1 will cause AnyEvent::DNS to announce
757 EDNS0 in its DNS requests.
710 758
711EXAMPLE PROGRAM 759EXAMPLE PROGRAM
712 The following program uses an I/O watcher to read data from STDIN, a 760 The following program uses an I/O watcher to read data from STDIN, a
713 timer to display a message once per second, and a condition variable to 761 timer to display a message once per second, and a condition variable to
714 quit the program when the user enters quit: 762 quit the program when the user enters quit:
796 syswrite $txn->{fh}, $txn->{request} 844 syswrite $txn->{fh}, $txn->{request}
797 or die "connection or write error"; 845 or die "connection or write error";
798 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); 846 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
799 847
800 Again, "fh_ready_r" waits till all data has arrived, and then stores the 848 Again, "fh_ready_r" waits till all data has arrived, and then stores the
801 result and signals any possible waiters that the request ahs finished: 849 result and signals any possible waiters that the request has finished:
802 850
803 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 851 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
804 852
805 if (end-of-file or data complete) { 853 if (end-of-file or data complete) {
806 $txn->{result} = $txn->{buf}; 854 $txn->{result} = $txn->{buf};
814 862
815 $txn->{finished}->recv; 863 $txn->{finished}->recv;
816 return $txn->{result}; 864 return $txn->{result};
817 865
818 The actual code goes further and collects all errors ("die"s, 866 The actual code goes further and collects all errors ("die"s,
819 exceptions) that occured during request processing. The "result" method 867 exceptions) that occurred during request processing. The "result" method
820 detects whether an exception as thrown (it is stored inside the $txn 868 detects whether an exception as thrown (it is stored inside the $txn
821 object) and just throws the exception, which means connection errors and 869 object) and just throws the exception, which means connection errors and
822 other problems get reported tot he code that tries to use the result, 870 other problems get reported tot he code that tries to use the result,
823 not in a random callback. 871 not in a random callback.
824 872
867 over the event loops themselves and to give you an impression of the 915 over the event loops themselves and to give you an impression of the
868 speed of various event loops I prepared some benchmarks. 916 speed of various event loops I prepared some benchmarks.
869 917
870 BENCHMARKING ANYEVENT OVERHEAD 918 BENCHMARKING ANYEVENT OVERHEAD
871 Here is a benchmark of various supported event models used natively and 919 Here is a benchmark of various supported event models used natively and
872 through anyevent. The benchmark creates a lot of timers (with a zero 920 through AnyEvent. The benchmark creates a lot of timers (with a zero
873 timeout) and I/O watchers (watching STDOUT, a pty, to become writable, 921 timeout) and I/O watchers (watching STDOUT, a pty, to become writable,
874 which it is), lets them fire exactly once and destroys them again. 922 which it is), lets them fire exactly once and destroys them again.
875 923
876 Source code for this benchmark is found as eg/bench in the AnyEvent 924 Source code for this benchmark is found as eg/bench in the AnyEvent
877 distribution. 925 distribution.
992 1040
993 * You should avoid POE like the plague if you want performance or 1041 * You should avoid POE like the plague if you want performance or
994 reasonable memory usage. 1042 reasonable memory usage.
995 1043
996 BENCHMARKING THE LARGE SERVER CASE 1044 BENCHMARKING THE LARGE SERVER CASE
997 This benchmark atcually benchmarks the event loop itself. It works by 1045 This benchmark actually benchmarks the event loop itself. It works by
998 creating a number of "servers": each server consists of a socketpair, a 1046 creating a number of "servers": each server consists of a socket pair, a
999 timeout watcher that gets reset on activity (but never fires), and an 1047 timeout watcher that gets reset on activity (but never fires), and an
1000 I/O watcher waiting for input on one side of the socket. Each time the 1048 I/O watcher waiting for input on one side of the socket. Each time the
1001 socket watcher reads a byte it will write that byte to a random other 1049 socket watcher reads a byte it will write that byte to a random other
1002 "server". 1050 "server".
1003 1051
1004 The effect is that there will be a lot of I/O watchers, only part of 1052 The effect is that there will be a lot of I/O watchers, only part of
1005 which are active at any one point (so there is a constant number of 1053 which are active at any one point (so there is a constant number of
1006 active fds for each loop iterstaion, but which fds these are is random). 1054 active fds for each loop iteration, but which fds these are is random).
1007 The timeout is reset each time something is read because that reflects 1055 The timeout is reset each time something is read because that reflects
1008 how most timeouts work (and puts extra pressure on the event loops). 1056 how most timeouts work (and puts extra pressure on the event loops).
1009 1057
1010 In this benchmark, we use 10000 socketpairs (20000 sockets), of which 1058 In this benchmark, we use 10000 socket pairs (20000 sockets), of which
1011 100 (1%) are active. This mirrors the activity of large servers with 1059 100 (1%) are active. This mirrors the activity of large servers with
1012 many connections, most of which are idle at any one point in time. 1060 many connections, most of which are idle at any one point in time.
1013 1061
1014 Source code for this benchmark is found as eg/bench2 in the AnyEvent 1062 Source code for this benchmark is found as eg/bench2 in the AnyEvent
1015 distribution. 1063 distribution.
1016 1064
1017 Explanation of the columns 1065 Explanation of the columns
1018 *sockets* is the number of sockets, and twice the number of "servers" 1066 *sockets* is the number of sockets, and twice the number of "servers"
1019 (as each server has a read and write socket end). 1067 (as each server has a read and write socket end).
1020 1068
1021 *create* is the time it takes to create a socketpair (which is 1069 *create* is the time it takes to create a socket pair (which is
1022 nontrivial) and two watchers: an I/O watcher and a timeout watcher. 1070 nontrivial) and two watchers: an I/O watcher and a timeout watcher.
1023 1071
1024 *request*, the most important value, is the time it takes to handle a 1072 *request*, the most important value, is the time it takes to handle a
1025 single "request", that is, reading the token from the pipe and 1073 single "request", that is, reading the token from the pipe and
1026 forwarding it to another server. This includes deleting the old timeout 1074 forwarding it to another server. This includes deleting the old timeout
1090 and speed most when you have lots of watchers, not when you only have a 1138 and speed most when you have lots of watchers, not when you only have a
1091 few of them). 1139 few of them).
1092 1140
1093 EV is again fastest. 1141 EV is again fastest.
1094 1142
1095 Perl again comes second. It is noticably faster than the C-based event 1143 Perl again comes second. It is noticeably faster than the C-based event
1096 loops Event and Glib, although the difference is too small to really 1144 loops Event and Glib, although the difference is too small to really
1097 matter. 1145 matter.
1098 1146
1099 POE also performs much better in this case, but is is still far behind 1147 POE also performs much better in this case, but is is still far behind
1100 the others. 1148 the others.
1129 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1177 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1130 be used to probe what backend is used and gain other information (which 1178 be used to probe what backend is used and gain other information (which
1131 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1179 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1132 1180
1133SEE ALSO 1181SEE ALSO
1182 Utility functions: AnyEvent::Util.
1183
1134 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1184 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk,
1135 Event::Lib, Qt, POE. 1185 Event::Lib, Qt, POE.
1136 1186
1137 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1187 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1138 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1188 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1139 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE. 1189 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE.
1140 1190
1191 Non-blocking file handles, sockets, TCP clients and servers:
1192 AnyEvent::Handle, AnyEvent::Socket.
1193
1194 Asynchronous DNS: AnyEvent::DNS.
1195
1141 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1196 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1142 1197
1143 Nontrivial usage examples: Net::FCP, Net::XMPP2. 1198 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1144 1199
1145AUTHOR 1200AUTHOR
1146 Marc Lehmann <schmorp@schmorp.de> 1201 Marc Lehmann <schmorp@schmorp.de>
1147 http://home.schmorp.de/ 1202 http://home.schmorp.de/
1148 1203

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines