… | |
… | |
57 | as those use one of the supported event loops. It is trivial to add new |
57 | as those use one of the supported event loops. It is trivial to add new |
58 | event loops to AnyEvent, too, so it is future-proof). |
58 | event loops to AnyEvent, too, so it is future-proof). |
59 | |
59 | |
60 | In addition to being free of having to use I<the one and only true event |
60 | In addition to being free of having to use I<the one and only true event |
61 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
61 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
62 | modules, you get an enourmous amount of code and strict rules you have to |
62 | modules, you get an enormous amount of code and strict rules you have to |
63 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
63 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
64 | offering the functionality that is necessary, in as thin as a wrapper as |
64 | offering the functionality that is necessary, in as thin as a wrapper as |
65 | technically possible. |
65 | technically possible. |
66 | |
66 | |
67 | Of course, if you want lots of policy (this can arguably be somewhat |
67 | Of course, if you want lots of policy (this can arguably be somewhat |
… | |
… | |
108 | |
108 | |
109 | =head1 WATCHERS |
109 | =head1 WATCHERS |
110 | |
110 | |
111 | AnyEvent has the central concept of a I<watcher>, which is an object that |
111 | AnyEvent has the central concept of a I<watcher>, which is an object that |
112 | stores relevant data for each kind of event you are waiting for, such as |
112 | stores relevant data for each kind of event you are waiting for, such as |
113 | the callback to call, the filehandle to watch, etc. |
113 | the callback to call, the file handle to watch, etc. |
114 | |
114 | |
115 | These watchers are normal Perl objects with normal Perl lifetime. After |
115 | These watchers are normal Perl objects with normal Perl lifetime. After |
116 | creating a watcher it will immediately "watch" for events and invoke the |
116 | creating a watcher it will immediately "watch" for events and invoke the |
117 | callback when the event occurs (of course, only when the event model |
117 | callback when the event occurs (of course, only when the event model |
118 | is in control). |
118 | is in control). |
… | |
… | |
237 | |
237 | |
238 | Although the callback might get passed parameters, their value and |
238 | Although the callback might get passed parameters, their value and |
239 | presence is undefined and you cannot rely on them. Portable AnyEvent |
239 | presence is undefined and you cannot rely on them. Portable AnyEvent |
240 | callbacks cannot use arguments passed to signal watcher callbacks. |
240 | callbacks cannot use arguments passed to signal watcher callbacks. |
241 | |
241 | |
242 | Multiple signal occurances can be clumped together into one callback |
242 | Multiple signal occurrences can be clumped together into one callback |
243 | invocation, and callback invocation will be synchronous. synchronous means |
243 | invocation, and callback invocation will be synchronous. Synchronous means |
244 | that it might take a while until the signal gets handled by the process, |
244 | that it might take a while until the signal gets handled by the process, |
245 | but it is guarenteed not to interrupt any other callbacks. |
245 | but it is guaranteed not to interrupt any other callbacks. |
246 | |
246 | |
247 | The main advantage of using these watchers is that you can share a signal |
247 | The main advantage of using these watchers is that you can share a signal |
248 | between multiple watchers. |
248 | between multiple watchers. |
249 | |
249 | |
250 | This watcher might use C<%SIG>, so programs overwriting those signals |
250 | This watcher might use C<%SIG>, so programs overwriting those signals |
… | |
… | |
310 | Condition variables can be created by calling the C<< AnyEvent->condvar |
310 | Condition variables can be created by calling the C<< AnyEvent->condvar |
311 | >> method, usually without arguments. The only argument pair allowed is |
311 | >> method, usually without arguments. The only argument pair allowed is |
312 | C<cb>, which specifies a callback to be called when the condition variable |
312 | C<cb>, which specifies a callback to be called when the condition variable |
313 | becomes true. |
313 | becomes true. |
314 | |
314 | |
315 | After creation, the conditon variable is "false" until it becomes "true" |
315 | After creation, the condition variable is "false" until it becomes "true" |
316 | by calling the C<send> method. |
316 | by calling the C<send> method (or calling the condition variable as if it |
|
|
317 | were a callback, read about the caveats in the description for the C<< |
|
|
318 | ->send >> method). |
317 | |
319 | |
318 | Condition variables are similar to callbacks, except that you can |
320 | Condition variables are similar to callbacks, except that you can |
319 | optionally wait for them. They can also be called merge points - points |
321 | optionally wait for them. They can also be called merge points - points |
320 | in time where multiple outstandign events have been processed. And yet |
322 | in time where multiple outstanding events have been processed. And yet |
321 | another way to call them is transations - each condition variable can be |
323 | another way to call them is transactions - each condition variable can be |
322 | used to represent a transaction, which finishes at some point and delivers |
324 | used to represent a transaction, which finishes at some point and delivers |
323 | a result. |
325 | a result. |
324 | |
326 | |
325 | Condition variables are very useful to signal that something has finished, |
327 | Condition variables are very useful to signal that something has finished, |
326 | for example, if you write a module that does asynchronous http requests, |
328 | for example, if you write a module that does asynchronous http requests, |
… | |
… | |
332 | you can block your main program until an event occurs - for example, you |
334 | you can block your main program until an event occurs - for example, you |
333 | could C<< ->recv >> in your main program until the user clicks the Quit |
335 | could C<< ->recv >> in your main program until the user clicks the Quit |
334 | button of your app, which would C<< ->send >> the "quit" event. |
336 | button of your app, which would C<< ->send >> the "quit" event. |
335 | |
337 | |
336 | Note that condition variables recurse into the event loop - if you have |
338 | Note that condition variables recurse into the event loop - if you have |
337 | two pieces of code that call C<< ->recv >> in a round-robbin fashion, you |
339 | two pieces of code that call C<< ->recv >> in a round-robin fashion, you |
338 | lose. Therefore, condition variables are good to export to your caller, but |
340 | lose. Therefore, condition variables are good to export to your caller, but |
339 | you should avoid making a blocking wait yourself, at least in callbacks, |
341 | you should avoid making a blocking wait yourself, at least in callbacks, |
340 | as this asks for trouble. |
342 | as this asks for trouble. |
341 | |
343 | |
342 | Condition variables are represented by hash refs in perl, and the keys |
344 | Condition variables are represented by hash refs in perl, and the keys |
… | |
… | |
347 | |
349 | |
348 | There are two "sides" to a condition variable - the "producer side" which |
350 | There are two "sides" to a condition variable - the "producer side" which |
349 | eventually calls C<< -> send >>, and the "consumer side", which waits |
351 | eventually calls C<< -> send >>, and the "consumer side", which waits |
350 | for the send to occur. |
352 | for the send to occur. |
351 | |
353 | |
352 | Example: |
354 | Example: wait for a timer. |
353 | |
355 | |
354 | # wait till the result is ready |
356 | # wait till the result is ready |
355 | my $result_ready = AnyEvent->condvar; |
357 | my $result_ready = AnyEvent->condvar; |
356 | |
358 | |
357 | # do something such as adding a timer |
359 | # do something such as adding a timer |
… | |
… | |
365 | |
367 | |
366 | # this "blocks" (while handling events) till the callback |
368 | # this "blocks" (while handling events) till the callback |
367 | # calls send |
369 | # calls send |
368 | $result_ready->recv; |
370 | $result_ready->recv; |
369 | |
371 | |
|
|
372 | Example: wait for a timer, but take advantage of the fact that |
|
|
373 | condition variables are also code references. |
|
|
374 | |
|
|
375 | my $done = AnyEvent->condvar; |
|
|
376 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
|
|
377 | $done->recv; |
|
|
378 | |
370 | =head3 METHODS FOR PRODUCERS |
379 | =head3 METHODS FOR PRODUCERS |
371 | |
380 | |
372 | These methods should only be used by the producing side, i.e. the |
381 | These methods should only be used by the producing side, i.e. the |
373 | code/module that eventually sends the signal. Note that it is also |
382 | code/module that eventually sends the signal. Note that it is also |
374 | the producer side which creates the condvar in most cases, but it isn't |
383 | the producer side which creates the condvar in most cases, but it isn't |
… | |
… | |
385 | If a callback has been set on the condition variable, it is called |
394 | If a callback has been set on the condition variable, it is called |
386 | immediately from within send. |
395 | immediately from within send. |
387 | |
396 | |
388 | Any arguments passed to the C<send> call will be returned by all |
397 | Any arguments passed to the C<send> call will be returned by all |
389 | future C<< ->recv >> calls. |
398 | future C<< ->recv >> calls. |
|
|
399 | |
|
|
400 | Condition variables are overloaded so one can call them directly |
|
|
401 | (as a code reference). Calling them directly is the same as calling |
|
|
402 | C<send>. Note, however, that many C-based event loops do not handle |
|
|
403 | overloading, so as tempting as it may be, passing a condition variable |
|
|
404 | instead of a callback does not work. Both the pure perl and EV loops |
|
|
405 | support overloading, however, as well as all functions that use perl to |
|
|
406 | invoke a callback (as in L<AnyEvent::Socket> and L<AnyEvent::DNS> for |
|
|
407 | example). |
390 | |
408 | |
391 | =item $cv->croak ($error) |
409 | =item $cv->croak ($error) |
392 | |
410 | |
393 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
411 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
394 | C<Carp::croak> with the given error message/object/scalar. |
412 | C<Carp::croak> with the given error message/object/scalar. |
… | |
… | |
443 | doesn't execute once). |
461 | doesn't execute once). |
444 | |
462 | |
445 | This is the general pattern when you "fan out" into multiple subrequests: |
463 | This is the general pattern when you "fan out" into multiple subrequests: |
446 | use an outer C<begin>/C<end> pair to set the callback and ensure C<end> |
464 | use an outer C<begin>/C<end> pair to set the callback and ensure C<end> |
447 | is called at least once, and then, for each subrequest you start, call |
465 | is called at least once, and then, for each subrequest you start, call |
448 | C<begin> and for eahc subrequest you finish, call C<end>. |
466 | C<begin> and for each subrequest you finish, call C<end>. |
449 | |
467 | |
450 | =back |
468 | =back |
451 | |
469 | |
452 | =head3 METHODS FOR CONSUMERS |
470 | =head3 METHODS FOR CONSUMERS |
453 | |
471 | |
… | |
… | |
475 | (programs might want to do that to stay interactive), so I<if you are |
493 | (programs might want to do that to stay interactive), so I<if you are |
476 | using this from a module, never require a blocking wait>, but let the |
494 | using this from a module, never require a blocking wait>, but let the |
477 | caller decide whether the call will block or not (for example, by coupling |
495 | caller decide whether the call will block or not (for example, by coupling |
478 | condition variables with some kind of request results and supporting |
496 | condition variables with some kind of request results and supporting |
479 | callbacks so the caller knows that getting the result will not block, |
497 | callbacks so the caller knows that getting the result will not block, |
480 | while still suppporting blocking waits if the caller so desires). |
498 | while still supporting blocking waits if the caller so desires). |
481 | |
499 | |
482 | Another reason I<never> to C<< ->recv >> in a module is that you cannot |
500 | Another reason I<never> to C<< ->recv >> in a module is that you cannot |
483 | sensibly have two C<< ->recv >>'s in parallel, as that would require |
501 | sensibly have two C<< ->recv >>'s in parallel, as that would require |
484 | multiple interpreters or coroutines/threads, none of which C<AnyEvent> |
502 | multiple interpreters or coroutines/threads, none of which C<AnyEvent> |
485 | can supply. |
503 | can supply. |
… | |
… | |
601 | |
619 | |
602 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
620 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
603 | do anything special (it does not need to be event-based) and let AnyEvent |
621 | do anything special (it does not need to be event-based) and let AnyEvent |
604 | decide which implementation to chose if some module relies on it. |
622 | decide which implementation to chose if some module relies on it. |
605 | |
623 | |
606 | If the main program relies on a specific event model. For example, in |
624 | If the main program relies on a specific event model - for example, in |
607 | Gtk2 programs you have to rely on the Glib module. You should load the |
625 | Gtk2 programs you have to rely on the Glib module - you should load the |
608 | event module before loading AnyEvent or any module that uses it: generally |
626 | event module before loading AnyEvent or any module that uses it: generally |
609 | speaking, you should load it as early as possible. The reason is that |
627 | speaking, you should load it as early as possible. The reason is that |
610 | modules might create watchers when they are loaded, and AnyEvent will |
628 | modules might create watchers when they are loaded, and AnyEvent will |
611 | decide on the event model to use as soon as it creates watchers, and it |
629 | decide on the event model to use as soon as it creates watchers, and it |
612 | might chose the wrong one unless you load the correct one yourself. |
630 | might chose the wrong one unless you load the correct one yourself. |
613 | |
631 | |
614 | You can chose to use a rather inefficient pure-perl implementation by |
632 | You can chose to use a pure-perl implementation by loading the |
615 | loading the C<AnyEvent::Impl::Perl> module, which gives you similar |
633 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
616 | behaviour everywhere, but letting AnyEvent chose is generally better. |
634 | everywhere, but letting AnyEvent chose the model is generally better. |
|
|
635 | |
|
|
636 | =head2 MAINLOOP EMULATION |
|
|
637 | |
|
|
638 | Sometimes (often for short test scripts, or even standalone programs who |
|
|
639 | only want to use AnyEvent), you do not want to run a specific event loop. |
|
|
640 | |
|
|
641 | In that case, you can use a condition variable like this: |
|
|
642 | |
|
|
643 | AnyEvent->condvar->recv; |
|
|
644 | |
|
|
645 | This has the effect of entering the event loop and looping forever. |
|
|
646 | |
|
|
647 | Note that usually your program has some exit condition, in which case |
|
|
648 | it is better to use the "traditional" approach of storing a condition |
|
|
649 | variable somewhere, waiting for it, and sending it when the program should |
|
|
650 | exit cleanly. |
|
|
651 | |
617 | |
652 | |
618 | =head1 OTHER MODULES |
653 | =head1 OTHER MODULES |
619 | |
654 | |
620 | The following is a non-exhaustive list of additional modules that use |
655 | The following is a non-exhaustive list of additional modules that use |
621 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
656 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
… | |
… | |
637 | |
672 | |
638 | Provides various utility functions for (internet protocol) sockets, |
673 | Provides various utility functions for (internet protocol) sockets, |
639 | addresses and name resolution. Also functions to create non-blocking tcp |
674 | addresses and name resolution. Also functions to create non-blocking tcp |
640 | connections or tcp servers, with IPv6 and SRV record support and more. |
675 | connections or tcp servers, with IPv6 and SRV record support and more. |
641 | |
676 | |
|
|
677 | =item L<AnyEvent::DNS> |
|
|
678 | |
|
|
679 | Provides rich asynchronous DNS resolver capabilities. |
|
|
680 | |
642 | =item L<AnyEvent::HTTPD> |
681 | =item L<AnyEvent::HTTPD> |
643 | |
682 | |
644 | Provides a simple web application server framework. |
683 | Provides a simple web application server framework. |
645 | |
|
|
646 | =item L<AnyEvent::DNS> |
|
|
647 | |
|
|
648 | Provides rich asynchronous DNS resolver capabilities. |
|
|
649 | |
684 | |
650 | =item L<AnyEvent::FastPing> |
685 | =item L<AnyEvent::FastPing> |
651 | |
686 | |
652 | The fastest ping in the west. |
687 | The fastest ping in the west. |
653 | |
688 | |
… | |
… | |
696 | no warnings; |
731 | no warnings; |
697 | use strict; |
732 | use strict; |
698 | |
733 | |
699 | use Carp; |
734 | use Carp; |
700 | |
735 | |
701 | our $VERSION = '3.6'; |
736 | our $VERSION = '4.03'; |
702 | our $MODEL; |
737 | our $MODEL; |
703 | |
738 | |
704 | our $AUTOLOAD; |
739 | our $AUTOLOAD; |
705 | our @ISA; |
740 | our @ISA; |
706 | |
741 | |
|
|
742 | our @REGISTRY; |
|
|
743 | |
|
|
744 | our $WIN32; |
|
|
745 | |
|
|
746 | BEGIN { |
|
|
747 | my $win32 = ! ! ($^O =~ /mswin32/i); |
|
|
748 | eval "sub WIN32(){ $win32 }"; |
|
|
749 | } |
|
|
750 | |
707 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
751 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
708 | |
752 | |
709 | our @REGISTRY; |
753 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
710 | |
|
|
711 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
712 | |
754 | |
713 | { |
755 | { |
714 | my $idx; |
756 | my $idx; |
715 | $PROTOCOL{$_} = ++$idx |
757 | $PROTOCOL{$_} = ++$idx |
|
|
758 | for reverse split /\s*,\s*/, |
716 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
759 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
|
|
760 | } |
|
|
761 | |
|
|
762 | sub import { |
|
|
763 | shift; |
|
|
764 | return unless @_; |
|
|
765 | |
|
|
766 | my $pkg = caller; |
|
|
767 | |
|
|
768 | no strict 'refs'; |
|
|
769 | |
|
|
770 | for (@_) { |
|
|
771 | *{"$pkg\::WIN32"} = *WIN32 if $_ eq "WIN32"; |
|
|
772 | } |
717 | } |
773 | } |
718 | |
774 | |
719 | my @models = ( |
775 | my @models = ( |
720 | [EV:: => AnyEvent::Impl::EV::], |
776 | [EV:: => AnyEvent::Impl::EV::], |
721 | [Event:: => AnyEvent::Impl::Event::], |
777 | [Event:: => AnyEvent::Impl::Event::], |
722 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
723 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
724 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
725 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
778 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
726 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
779 | # everything below here will not be autoprobed |
727 | [Glib:: => AnyEvent::Impl::Glib::], |
780 | # as the pureperl backend should work everywhere |
|
|
781 | # and is usually faster |
|
|
782 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
783 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
728 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
784 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
729 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
785 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
730 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
786 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
787 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
788 | [Prima:: => AnyEvent::Impl::POE::], |
731 | ); |
789 | ); |
732 | |
790 | |
733 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
791 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
734 | |
792 | |
735 | our @post_detect; |
793 | our @post_detect; |
… | |
… | |
755 | } |
813 | } |
756 | |
814 | |
757 | sub detect() { |
815 | sub detect() { |
758 | unless ($MODEL) { |
816 | unless ($MODEL) { |
759 | no strict 'refs'; |
817 | no strict 'refs'; |
|
|
818 | local $SIG{__DIE__}; |
760 | |
819 | |
761 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
820 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
762 | my $model = "AnyEvent::Impl::$1"; |
821 | my $model = "AnyEvent::Impl::$1"; |
763 | if (eval "require $model") { |
822 | if (eval "require $model") { |
764 | $MODEL = $model; |
823 | $MODEL = $model; |
… | |
… | |
887 | or Carp::croak "required option 'pid' is missing"; |
946 | or Carp::croak "required option 'pid' is missing"; |
888 | |
947 | |
889 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
948 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
890 | |
949 | |
891 | unless ($WNOHANG) { |
950 | unless ($WNOHANG) { |
892 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
951 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
893 | } |
952 | } |
894 | |
953 | |
895 | unless ($CHLD_W) { |
954 | unless ($CHLD_W) { |
896 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
955 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
897 | # child could be a zombie already, so make at least one round |
956 | # child could be a zombie already, so make at least one round |
… | |
… | |
913 | package AnyEvent::CondVar; |
972 | package AnyEvent::CondVar; |
914 | |
973 | |
915 | our @ISA = AnyEvent::CondVar::Base::; |
974 | our @ISA = AnyEvent::CondVar::Base::; |
916 | |
975 | |
917 | package AnyEvent::CondVar::Base; |
976 | package AnyEvent::CondVar::Base; |
|
|
977 | |
|
|
978 | use overload |
|
|
979 | '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
|
|
980 | fallback => 1; |
918 | |
981 | |
919 | sub _send { |
982 | sub _send { |
920 | # nop |
983 | # nop |
921 | } |
984 | } |
922 | |
985 | |
… | |
… | |
1029 | model it chooses. |
1092 | model it chooses. |
1030 | |
1093 | |
1031 | =item C<PERL_ANYEVENT_MODEL> |
1094 | =item C<PERL_ANYEVENT_MODEL> |
1032 | |
1095 | |
1033 | This can be used to specify the event model to be used by AnyEvent, before |
1096 | This can be used to specify the event model to be used by AnyEvent, before |
1034 | autodetection and -probing kicks in. It must be a string consisting |
1097 | auto detection and -probing kicks in. It must be a string consisting |
1035 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
1098 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
1036 | and the resulting module name is loaded and if the load was successful, |
1099 | and the resulting module name is loaded and if the load was successful, |
1037 | used as event model. If it fails to load AnyEvent will proceed with |
1100 | used as event model. If it fails to load AnyEvent will proceed with |
1038 | autodetection and -probing. |
1101 | auto detection and -probing. |
1039 | |
1102 | |
1040 | This functionality might change in future versions. |
1103 | This functionality might change in future versions. |
1041 | |
1104 | |
1042 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1105 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1043 | could start your program like this: |
1106 | could start your program like this: |
… | |
… | |
1046 | |
1109 | |
1047 | =item C<PERL_ANYEVENT_PROTOCOLS> |
1110 | =item C<PERL_ANYEVENT_PROTOCOLS> |
1048 | |
1111 | |
1049 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
1112 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
1050 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
1113 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
1051 | of autoprobing). |
1114 | of auto probing). |
1052 | |
1115 | |
1053 | Must be set to a comma-separated list of protocols or address families, |
1116 | Must be set to a comma-separated list of protocols or address families, |
1054 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
1117 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
1055 | used, and preference will be given to protocols mentioned earlier in the |
1118 | used, and preference will be given to protocols mentioned earlier in the |
1056 | list. |
1119 | list. |
1057 | |
1120 | |
|
|
1121 | This variable can effectively be used for denial-of-service attacks |
|
|
1122 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1123 | small, as the program has to handle connection errors already- |
|
|
1124 | |
1058 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
1125 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
1059 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
1126 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
1060 | - only support IPv4, never try to resolve or contact IPv6 |
1127 | - only support IPv4, never try to resolve or contact IPv6 |
1061 | addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
1128 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
1062 | IPv6, but prefer IPv6 over IPv4. |
1129 | IPv6, but prefer IPv6 over IPv4. |
|
|
1130 | |
|
|
1131 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1132 | |
|
|
1133 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1134 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1135 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1136 | default. |
|
|
1137 | |
|
|
1138 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1139 | EDNS0 in its DNS requests. |
1063 | |
1140 | |
1064 | =back |
1141 | =back |
1065 | |
1142 | |
1066 | =head1 EXAMPLE PROGRAM |
1143 | =head1 EXAMPLE PROGRAM |
1067 | |
1144 | |
… | |
… | |
1153 | syswrite $txn->{fh}, $txn->{request} |
1230 | syswrite $txn->{fh}, $txn->{request} |
1154 | or die "connection or write error"; |
1231 | or die "connection or write error"; |
1155 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
1232 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
1156 | |
1233 | |
1157 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
1234 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
1158 | result and signals any possible waiters that the request ahs finished: |
1235 | result and signals any possible waiters that the request has finished: |
1159 | |
1236 | |
1160 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1237 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1161 | |
1238 | |
1162 | if (end-of-file or data complete) { |
1239 | if (end-of-file or data complete) { |
1163 | $txn->{result} = $txn->{buf}; |
1240 | $txn->{result} = $txn->{buf}; |
… | |
… | |
1171 | |
1248 | |
1172 | $txn->{finished}->recv; |
1249 | $txn->{finished}->recv; |
1173 | return $txn->{result}; |
1250 | return $txn->{result}; |
1174 | |
1251 | |
1175 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1252 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1176 | that occured during request processing. The C<result> method detects |
1253 | that occurred during request processing. The C<result> method detects |
1177 | whether an exception as thrown (it is stored inside the $txn object) |
1254 | whether an exception as thrown (it is stored inside the $txn object) |
1178 | and just throws the exception, which means connection errors and other |
1255 | and just throws the exception, which means connection errors and other |
1179 | problems get reported tot he code that tries to use the result, not in a |
1256 | problems get reported tot he code that tries to use the result, not in a |
1180 | random callback. |
1257 | random callback. |
1181 | |
1258 | |
… | |
… | |
1227 | of various event loops I prepared some benchmarks. |
1304 | of various event loops I prepared some benchmarks. |
1228 | |
1305 | |
1229 | =head2 BENCHMARKING ANYEVENT OVERHEAD |
1306 | =head2 BENCHMARKING ANYEVENT OVERHEAD |
1230 | |
1307 | |
1231 | Here is a benchmark of various supported event models used natively and |
1308 | Here is a benchmark of various supported event models used natively and |
1232 | through anyevent. The benchmark creates a lot of timers (with a zero |
1309 | through AnyEvent. The benchmark creates a lot of timers (with a zero |
1233 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
1310 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
1234 | which it is), lets them fire exactly once and destroys them again. |
1311 | which it is), lets them fire exactly once and destroys them again. |
1235 | |
1312 | |
1236 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
1313 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
1237 | distribution. |
1314 | distribution. |
… | |
… | |
1360 | |
1437 | |
1361 | =back |
1438 | =back |
1362 | |
1439 | |
1363 | =head2 BENCHMARKING THE LARGE SERVER CASE |
1440 | =head2 BENCHMARKING THE LARGE SERVER CASE |
1364 | |
1441 | |
1365 | This benchmark atcually benchmarks the event loop itself. It works by |
1442 | This benchmark actually benchmarks the event loop itself. It works by |
1366 | creating a number of "servers": each server consists of a socketpair, a |
1443 | creating a number of "servers": each server consists of a socket pair, a |
1367 | timeout watcher that gets reset on activity (but never fires), and an I/O |
1444 | timeout watcher that gets reset on activity (but never fires), and an I/O |
1368 | watcher waiting for input on one side of the socket. Each time the socket |
1445 | watcher waiting for input on one side of the socket. Each time the socket |
1369 | watcher reads a byte it will write that byte to a random other "server". |
1446 | watcher reads a byte it will write that byte to a random other "server". |
1370 | |
1447 | |
1371 | The effect is that there will be a lot of I/O watchers, only part of which |
1448 | The effect is that there will be a lot of I/O watchers, only part of which |
1372 | are active at any one point (so there is a constant number of active |
1449 | are active at any one point (so there is a constant number of active |
1373 | fds for each loop iterstaion, but which fds these are is random). The |
1450 | fds for each loop iteration, but which fds these are is random). The |
1374 | timeout is reset each time something is read because that reflects how |
1451 | timeout is reset each time something is read because that reflects how |
1375 | most timeouts work (and puts extra pressure on the event loops). |
1452 | most timeouts work (and puts extra pressure on the event loops). |
1376 | |
1453 | |
1377 | In this benchmark, we use 10000 socketpairs (20000 sockets), of which 100 |
1454 | In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100 |
1378 | (1%) are active. This mirrors the activity of large servers with many |
1455 | (1%) are active. This mirrors the activity of large servers with many |
1379 | connections, most of which are idle at any one point in time. |
1456 | connections, most of which are idle at any one point in time. |
1380 | |
1457 | |
1381 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
1458 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
1382 | distribution. |
1459 | distribution. |
… | |
… | |
1384 | =head3 Explanation of the columns |
1461 | =head3 Explanation of the columns |
1385 | |
1462 | |
1386 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
1463 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
1387 | each server has a read and write socket end). |
1464 | each server has a read and write socket end). |
1388 | |
1465 | |
1389 | I<create> is the time it takes to create a socketpair (which is |
1466 | I<create> is the time it takes to create a socket pair (which is |
1390 | nontrivial) and two watchers: an I/O watcher and a timeout watcher. |
1467 | nontrivial) and two watchers: an I/O watcher and a timeout watcher. |
1391 | |
1468 | |
1392 | I<request>, the most important value, is the time it takes to handle a |
1469 | I<request>, the most important value, is the time it takes to handle a |
1393 | single "request", that is, reading the token from the pipe and forwarding |
1470 | single "request", that is, reading the token from the pipe and forwarding |
1394 | it to another server. This includes deleting the old timeout and creating |
1471 | it to another server. This includes deleting the old timeout and creating |
… | |
… | |
1467 | speed most when you have lots of watchers, not when you only have a few of |
1544 | speed most when you have lots of watchers, not when you only have a few of |
1468 | them). |
1545 | them). |
1469 | |
1546 | |
1470 | EV is again fastest. |
1547 | EV is again fastest. |
1471 | |
1548 | |
1472 | Perl again comes second. It is noticably faster than the C-based event |
1549 | Perl again comes second. It is noticeably faster than the C-based event |
1473 | loops Event and Glib, although the difference is too small to really |
1550 | loops Event and Glib, although the difference is too small to really |
1474 | matter. |
1551 | matter. |
1475 | |
1552 | |
1476 | POE also performs much better in this case, but is is still far behind the |
1553 | POE also performs much better in this case, but is is still far behind the |
1477 | others. |
1554 | others. |