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

Comparing AnyEvent/README (file contents):
Revision 1.20 by root, Sat May 10 22:30:28 2008 UTC vs.
Revision 1.23 by root, Mon May 26 06:04:38 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 "true" by calling the "send" method (or calling the condition variable
308 as if it were a callback, read about the caveats in the description for
307 by calling the "send" method. 309 the "->send" method).
308 310
309 Condition variables are similar to callbacks, except that you can 311 Condition variables are similar to callbacks, except that you can
310 optionally wait for them. They can also be called merge points - points 312 optionally wait for them. They can also be called merge points - points
311 in time where multiple outstandign events have been processed. And yet 313 in time where multiple outstanding events have been processed. And yet
312 another way to call them is transations - each condition variable can be 314 another way to call them is transactions - each condition variable can
313 used to represent a transaction, which finishes at some point and 315 be used to represent a transaction, which finishes at some point and
314 delivers a result. 316 delivers a result.
315 317
316 Condition variables are very useful to signal that something has 318 Condition variables are very useful to signal that something has
317 finished, for example, if you write a module that does asynchronous http 319 finished, for example, if you write a module that does asynchronous http
318 requests, then a condition variable would be the ideal candidate to 320 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 325 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 326 could "->recv" in your main program until the user clicks the Quit
325 button of your app, which would "->send" the "quit" event. 327 button of your app, which would "->send" the "quit" event.
326 328
327 Note that condition variables recurse into the event loop - if you have 329 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 330 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, 331 lose. Therefore, condition variables are good to export to your caller,
330 but you should avoid making a blocking wait yourself, at least in 332 but you should avoid making a blocking wait yourself, at least in
331 callbacks, as this asks for trouble. 333 callbacks, as this asks for trouble.
332 334
333 Condition variables are represented by hash refs in perl, and the keys 335 Condition variables are represented by hash refs in perl, and the keys
338 340
339 There are two "sides" to a condition variable - the "producer side" 341 There are two "sides" to a condition variable - the "producer side"
340 which eventually calls "-> send", and the "consumer side", which waits 342 which eventually calls "-> send", and the "consumer side", which waits
341 for the send to occur. 343 for the send to occur.
342 344
343 Example: 345 Example: wait for a timer.
344 346
345 # wait till the result is ready 347 # wait till the result is ready
346 my $result_ready = AnyEvent->condvar; 348 my $result_ready = AnyEvent->condvar;
347 349
348 # do something such as adding a timer 350 # do something such as adding a timer
356 358
357 # this "blocks" (while handling events) till the callback 359 # this "blocks" (while handling events) till the callback
358 # calls send 360 # calls send
359 $result_ready->recv; 361 $result_ready->recv;
360 362
363 Example: wait for a timer, but take advantage of the fact that condition
364 variables are also code references.
365
366 my $done = AnyEvent->condvar;
367 my $delay = AnyEvent->timer (after => 5, cb => $done);
368 $done->recv;
369
361 METHODS FOR PRODUCERS 370 METHODS FOR PRODUCERS
362 These methods should only be used by the producing side, i.e. the 371 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 372 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 373 producer side which creates the condvar in most cases, but it isn't
365 uncommon for the consumer to create it as well. 374 uncommon for the consumer to create it as well.
372 If a callback has been set on the condition variable, it is called 381 If a callback has been set on the condition variable, it is called
373 immediately from within send. 382 immediately from within send.
374 383
375 Any arguments passed to the "send" call will be returned by all 384 Any arguments passed to the "send" call will be returned by all
376 future "->recv" calls. 385 future "->recv" calls.
386
387 Condition variables are overloaded so one can call them directly (as
388 a code reference). Calling them directly is the same as calling
389 "send". Note, however, that many C-based event loops do not handle
390 overloading, so as tempting as it may be, passing a condition
391 variable instead of a callback does not work. Both the pure perl and
392 EV loops support overloading, however, as well as all functions that
393 use perl to invoke a callback (as in AnyEvent::Socket and
394 AnyEvent::DNS for example).
377 395
378 $cv->croak ($error) 396 $cv->croak ($error)
379 Similar to send, but causes all call's to "->recv" to invoke 397 Similar to send, but causes all call's to "->recv" to invoke
380 "Carp::croak" with the given error message/object/scalar. 398 "Carp::croak" with the given error message/object/scalar.
381 399
427 (the loop doesn't execute once). 445 (the loop doesn't execute once).
428 446
429 This is the general pattern when you "fan out" into multiple 447 This is the general pattern when you "fan out" into multiple
430 subrequests: use an outer "begin"/"end" pair to set the callback and 448 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 449 ensure "end" is called at least once, and then, for each subrequest
432 you start, call "begin" and for eahc subrequest you finish, call 450 you start, call "begin" and for each subrequest you finish, call
433 "end". 451 "end".
434 452
435 METHODS FOR CONSUMERS 453 METHODS FOR CONSUMERS
436 These methods should only be used by the consuming side, i.e. the code 454 These methods should only be used by the consuming side, i.e. the code
437 awaits the condition. 455 awaits the condition.
453 (programs might want to do that to stay interactive), so *if you are 471 (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 472 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, 473 the caller decide whether the call will block or not (for example,
456 by coupling condition variables with some kind of request results 474 by coupling condition variables with some kind of request results
457 and supporting callbacks so the caller knows that getting the result 475 and supporting callbacks so the caller knows that getting the result
458 will not block, while still suppporting blocking waits if the caller 476 will not block, while still supporting blocking waits if the caller
459 so desires). 477 so desires).
460 478
461 Another reason *never* to "->recv" in a module is that you cannot 479 Another reason *never* to "->recv" in a module is that you cannot
462 sensibly have two "->recv"'s in parallel, as that would require 480 sensibly have two "->recv"'s in parallel, as that would require
463 multiple interpreters or coroutines/threads, none of which 481 multiple interpreters or coroutines/threads, none of which
566 If it doesn't care, it can just "use AnyEvent" and use it itself, or not 584 If it doesn't care, it can just "use AnyEvent" and use it itself, or not
567 do anything special (it does not need to be event-based) and let 585 do anything special (it does not need to be event-based) and let
568 AnyEvent decide which implementation to chose if some module relies on 586 AnyEvent decide which implementation to chose if some module relies on
569 it. 587 it.
570 588
571 If the main program relies on a specific event model. For example, in 589 If the main program relies on a specific event model - for example, in
572 Gtk2 programs you have to rely on the Glib module. You should load the 590 Gtk2 programs you have to rely on the Glib module - you should load the
573 event module before loading AnyEvent or any module that uses it: 591 event module before loading AnyEvent or any module that uses it:
574 generally speaking, you should load it as early as possible. The reason 592 generally speaking, you should load it as early as possible. The reason
575 is that modules might create watchers when they are loaded, and AnyEvent 593 is that modules might create watchers when they are loaded, and AnyEvent
576 will decide on the event model to use as soon as it creates watchers, 594 will decide on the event model to use as soon as it creates watchers,
577 and it might chose the wrong one unless you load the correct one 595 and it might chose the wrong one unless you load the correct one
578 yourself. 596 yourself.
579 597
580 You can chose to use a rather inefficient pure-perl implementation by 598 You can chose to use a pure-perl implementation by loading the
581 loading the "AnyEvent::Impl::Perl" module, which gives you similar 599 "AnyEvent::Impl::Perl" module, which gives you similar behaviour
582 behaviour everywhere, but letting AnyEvent chose is generally better. 600 everywhere, but letting AnyEvent chose the model is generally better.
601
602 MAINLOOP EMULATION
603 Sometimes (often for short test scripts, or even standalone programs who
604 only want to use AnyEvent), you do not want to run a specific event
605 loop.
606
607 In that case, you can use a condition variable like this:
608
609 AnyEvent->condvar->recv;
610
611 This has the effect of entering the event loop and looping forever.
612
613 Note that usually your program has some exit condition, in which case it
614 is better to use the "traditional" approach of storing a condition
615 variable somewhere, waiting for it, and sending it when the program
616 should exit cleanly.
583 617
584OTHER MODULES 618OTHER MODULES
585 The following is a non-exhaustive list of additional modules that use 619 The following is a non-exhaustive list of additional modules that use
586 AnyEvent and can therefore be mixed easily with other AnyEvent modules 620 AnyEvent and can therefore be mixed easily with other AnyEvent modules
587 in the same program. Some of the modules come with AnyEvent, some are 621 in the same program. Some of the modules come with AnyEvent, some are
594 628
595 AnyEvent::Handle 629 AnyEvent::Handle
596 Provide read and write buffers and manages watchers for reads and 630 Provide read and write buffers and manages watchers for reads and
597 writes. 631 writes.
598 632
633 AnyEvent::Socket
634 Provides various utility functions for (internet protocol) sockets,
635 addresses and name resolution. Also functions to create non-blocking
636 tcp connections or tcp servers, with IPv6 and SRV record support and
637 more.
638
639 AnyEvent::DNS
640 Provides rich asynchronous DNS resolver capabilities.
641
599 AnyEvent::HTTPD 642 AnyEvent::HTTPD
600 Provides a simple web application server framework. 643 Provides a simple web application server framework.
601
602 AnyEvent::DNS
603 Provides asynchronous DNS resolver capabilities, beyond what
604 AnyEvent::Util offers.
605 644
606 AnyEvent::FastPing 645 AnyEvent::FastPing
607 The fastest ping in the west. 646 The fastest ping in the west.
608 647
609 Net::IRC3 648 Net::IRC3
693 When set to 2 or higher, cause AnyEvent to report to STDERR which 732 When set to 2 or higher, cause AnyEvent to report to STDERR which
694 event model it chooses. 733 event model it chooses.
695 734
696 "PERL_ANYEVENT_MODEL" 735 "PERL_ANYEVENT_MODEL"
697 This can be used to specify the event model to be used by AnyEvent, 736 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 737 before auto detection and -probing kicks in. It must be a string
699 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 738 consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
700 gets prepended and the resulting module name is loaded and if the 739 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 740 load was successful, used as event model. If it fails to load
702 AnyEvent will proceed with autodetection and -probing. 741 AnyEvent will proceed with auto detection and -probing.
703 742
704 This functionality might change in future versions. 743 This functionality might change in future versions.
705 744
706 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 745 For example, to force the pure perl model (AnyEvent::Impl::Perl) you
707 could start your program like this: 746 could start your program like this:
708 747
709 PERL_ANYEVENT_MODEL=Perl perl ... 748 PERL_ANYEVENT_MODEL=Perl perl ...
749
750 "PERL_ANYEVENT_PROTOCOLS"
751 Used by both AnyEvent::DNS and AnyEvent::Socket to determine
752 preferences for IPv4 or IPv6. The default is unspecified (and might
753 change, or be the result of auto probing).
754
755 Must be set to a comma-separated list of protocols or address
756 families, current supported: "ipv4" and "ipv6". Only protocols
757 mentioned will be used, and preference will be given to protocols
758 mentioned earlier in the list.
759
760 This variable can effectively be used for denial-of-service attacks
761 against local programs (e.g. when setuid), although the impact is
762 likely small, as the program has to handle connection errors
763 already-
764
765 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over
766 IPv6, but support both and try to use both.
767 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to
768 resolve or contact IPv6 addresses.
769 "PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4" support either IPv4 or IPv6, but
770 prefer IPv6 over IPv4.
771
772 "PERL_ANYEVENT_EDNS0"
773 Used by AnyEvent::DNS to decide whether to use the EDNS0 extension
774 for DNS. This extension is generally useful to reduce DNS traffic,
775 but some (broken) firewalls drop such DNS packets, which is why it
776 is off by default.
777
778 Setting this variable to 1 will cause AnyEvent::DNS to announce
779 EDNS0 in its DNS requests.
710 780
711EXAMPLE PROGRAM 781EXAMPLE PROGRAM
712 The following program uses an I/O watcher to read data from STDIN, a 782 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 783 timer to display a message once per second, and a condition variable to
714 quit the program when the user enters quit: 784 quit the program when the user enters quit:
722 poll => 'r', 792 poll => 'r',
723 cb => sub { 793 cb => sub {
724 warn "io event <$_[0]>\n"; # will always output <r> 794 warn "io event <$_[0]>\n"; # will always output <r>
725 chomp (my $input = <STDIN>); # read a line 795 chomp (my $input = <STDIN>); # read a line
726 warn "read: $input\n"; # output what has been read 796 warn "read: $input\n"; # output what has been read
727 $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i 797 $cv->send if $input =~ /^q/i; # quit program if /^q/i
728 }, 798 },
729 ); 799 );
730 800
731 my $time_watcher; # can only be used once 801 my $time_watcher; # can only be used once
732 802
737 }); 807 });
738 } 808 }
739 809
740 new_timer; # create first timer 810 new_timer; # create first timer
741 811
742 $cv->wait; # wait until user enters /^q/i 812 $cv->recv; # wait until user enters /^q/i
743 813
744REAL-WORLD EXAMPLE 814REAL-WORLD EXAMPLE
745 Consider the Net::FCP module. It features (among others) the following 815 Consider the Net::FCP module. It features (among others) the following
746 API calls, which are to freenet what HTTP GET requests are to http: 816 API calls, which are to freenet what HTTP GET requests are to http:
747 817
796 syswrite $txn->{fh}, $txn->{request} 866 syswrite $txn->{fh}, $txn->{request}
797 or die "connection or write error"; 867 or die "connection or write error";
798 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); 868 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r });
799 869
800 Again, "fh_ready_r" waits till all data has arrived, and then stores the 870 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: 871 result and signals any possible waiters that the request has finished:
802 872
803 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 873 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
804 874
805 if (end-of-file or data complete) { 875 if (end-of-file or data complete) {
806 $txn->{result} = $txn->{buf}; 876 $txn->{result} = $txn->{buf};
807 $txn->{finished}->broadcast; 877 $txn->{finished}->send;
808 $txb->{cb}->($txn) of $txn->{cb}; # also call callback 878 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
809 } 879 }
810 880
811 The "result" method, finally, just waits for the finished signal (if the 881 The "result" method, finally, just waits for the finished signal (if the
812 request was already finished, it doesn't wait, of course, and returns 882 request was already finished, it doesn't wait, of course, and returns
813 the data: 883 the data:
814 884
815 $txn->{finished}->wait; 885 $txn->{finished}->recv;
816 return $txn->{result}; 886 return $txn->{result};
817 887
818 The actual code goes further and collects all errors ("die"s, 888 The actual code goes further and collects all errors ("die"s,
819 exceptions) that occured during request processing. The "result" method 889 exceptions) that occurred during request processing. The "result" method
820 detects whether an exception as thrown (it is stored inside the $txn 890 detects whether an exception as thrown (it is stored inside the $txn
821 object) and just throws the exception, which means connection errors and 891 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, 892 other problems get reported tot he code that tries to use the result,
823 not in a random callback. 893 not in a random callback.
824 894
855 925
856 my $quit = AnyEvent->condvar; 926 my $quit = AnyEvent->condvar;
857 927
858 $fcp->txn_client_get ($url)->cb (sub { 928 $fcp->txn_client_get ($url)->cb (sub {
859 ... 929 ...
860 $quit->broadcast; 930 $quit->send;
861 }); 931 });
862 932
863 $quit->wait; 933 $quit->recv;
864 934
865BENCHMARKS 935BENCHMARKS
866 To give you an idea of the performance and overheads that AnyEvent adds 936 To give you an idea of the performance and overheads that AnyEvent adds
867 over the event loops themselves and to give you an impression of the 937 over the event loops themselves and to give you an impression of the
868 speed of various event loops I prepared some benchmarks. 938 speed of various event loops I prepared some benchmarks.
869 939
870 BENCHMARKING ANYEVENT OVERHEAD 940 BENCHMARKING ANYEVENT OVERHEAD
871 Here is a benchmark of various supported event models used natively and 941 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 942 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, 943 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. 944 which it is), lets them fire exactly once and destroys them again.
875 945
876 Source code for this benchmark is found as eg/bench in the AnyEvent 946 Source code for this benchmark is found as eg/bench in the AnyEvent
877 distribution. 947 distribution.
893 between all watchers, to avoid adding memory overhead. That means 963 between all watchers, to avoid adding memory overhead. That means
894 closure creation and memory usage is not included in the figures. 964 closure creation and memory usage is not included in the figures.
895 965
896 *invoke* is the time, in microseconds, used to invoke a simple callback. 966 *invoke* is the time, in microseconds, used to invoke a simple callback.
897 The callback simply counts down a Perl variable and after it was invoked 967 The callback simply counts down a Perl variable and after it was invoked
898 "watcher" times, it would "->broadcast" a condvar once to signal the end 968 "watcher" times, it would "->send" a condvar once to signal the end of
899 of this phase. 969 this phase.
900 970
901 *destroy* is the time, in microseconds, that it takes to destroy a 971 *destroy* is the time, in microseconds, that it takes to destroy a
902 single watcher. 972 single watcher.
903 973
904 Results 974 Results
992 1062
993 * You should avoid POE like the plague if you want performance or 1063 * You should avoid POE like the plague if you want performance or
994 reasonable memory usage. 1064 reasonable memory usage.
995 1065
996 BENCHMARKING THE LARGE SERVER CASE 1066 BENCHMARKING THE LARGE SERVER CASE
997 This benchmark atcually benchmarks the event loop itself. It works by 1067 This benchmark actually benchmarks the event loop itself. It works by
998 creating a number of "servers": each server consists of a socketpair, a 1068 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 1069 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 1070 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 1071 socket watcher reads a byte it will write that byte to a random other
1002 "server". 1072 "server".
1003 1073
1004 The effect is that there will be a lot of I/O watchers, only part of 1074 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 1075 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). 1076 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 1077 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). 1078 how most timeouts work (and puts extra pressure on the event loops).
1009 1079
1010 In this benchmark, we use 10000 socketpairs (20000 sockets), of which 1080 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 1081 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. 1082 many connections, most of which are idle at any one point in time.
1013 1083
1014 Source code for this benchmark is found as eg/bench2 in the AnyEvent 1084 Source code for this benchmark is found as eg/bench2 in the AnyEvent
1015 distribution. 1085 distribution.
1016 1086
1017 Explanation of the columns 1087 Explanation of the columns
1018 *sockets* is the number of sockets, and twice the number of "servers" 1088 *sockets* is the number of sockets, and twice the number of "servers"
1019 (as each server has a read and write socket end). 1089 (as each server has a read and write socket end).
1020 1090
1021 *create* is the time it takes to create a socketpair (which is 1091 *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. 1092 nontrivial) and two watchers: an I/O watcher and a timeout watcher.
1023 1093
1024 *request*, the most important value, is the time it takes to handle a 1094 *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 1095 single "request", that is, reading the token from the pipe and
1026 forwarding it to another server. This includes deleting the old timeout 1096 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 1160 and speed most when you have lots of watchers, not when you only have a
1091 few of them). 1161 few of them).
1092 1162
1093 EV is again fastest. 1163 EV is again fastest.
1094 1164
1095 Perl again comes second. It is noticably faster than the C-based event 1165 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 1166 loops Event and Glib, although the difference is too small to really
1097 matter. 1167 matter.
1098 1168
1099 POE also performs much better in this case, but is is still far behind 1169 POE also performs much better in this case, but is is still far behind
1100 the others. 1170 the others.
1129 Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1199 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 1200 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). 1201 is probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1132 1202
1133SEE ALSO 1203SEE ALSO
1204 Utility functions: AnyEvent::Util.
1205
1134 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1206 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk,
1135 Event::Lib, Qt, POE. 1207 Event::Lib, Qt, POE.
1136 1208
1137 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1209 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1138 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1210 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1139 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE. 1211 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE.
1140 1212
1213 Non-blocking file handles, sockets, TCP clients and servers:
1214 AnyEvent::Handle, AnyEvent::Socket.
1215
1216 Asynchronous DNS: AnyEvent::DNS.
1217
1141 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event, 1218 Coroutine support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event,
1142 1219
1143 Nontrivial usage examples: Net::FCP, Net::XMPP2. 1220 Nontrivial usage examples: Net::FCP, Net::XMPP2, AnyEvent::DNS.
1144 1221
1145AUTHOR 1222AUTHOR
1146 Marc Lehmann <schmorp@schmorp.de> 1223 Marc Lehmann <schmorp@schmorp.de>
1147 http://home.schmorp.de/ 1224 http://home.schmorp.de/
1148 1225

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines