… | |
… | |
48 | isn't itself. What's worse, all the potential users of your module are |
48 | isn't itself. What's worse, all the potential users of your module are |
49 | I<also> forced to use the same event loop you use. |
49 | I<also> forced to use the same event loop you use. |
50 | |
50 | |
51 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
51 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
52 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
52 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
53 | with the rest: POE + IO::Async? no go. Tk + Event? no go. Again: if |
53 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
54 | your module uses one of those, every user of your module has to use it, |
54 | your module uses one of those, every user of your module has to use it, |
55 | too. But if your module uses AnyEvent, it works transparently with all |
55 | too. But if your module uses AnyEvent, it works transparently with all |
56 | event models it supports (including stuff like POE and IO::Async, as long |
56 | event models it supports (including stuff like POE and IO::Async, as long |
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). |
… | |
… | |
62 | modules, you get an enormous 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, AnyEvent comes with a big (and fully optional!) toolbox |
|
|
68 | of useful functionality, such as an asynchronous DNS resolver, 100% |
|
|
69 | non-blocking connects (even with TLS/SSL, IPv6 and on broken platforms |
|
|
70 | such as Windows) and lots of real-world knowledge and workarounds for |
|
|
71 | platform bugs and differences. |
|
|
72 | |
67 | Of course, if you want lots of policy (this can arguably be somewhat |
73 | Now, if you I<do want> lots of policy (this can arguably be somewhat |
68 | useful) and you want to force your users to use the one and only event |
74 | useful) and you want to force your users to use the one and only event |
69 | model, you should I<not> use this module. |
75 | model, you should I<not> use this module. |
70 | |
76 | |
71 | =head1 DESCRIPTION |
77 | =head1 DESCRIPTION |
72 | |
78 | |
… | |
… | |
102 | starts using it, all bets are off. Maybe you should tell their authors to |
108 | starts using it, all bets are off. Maybe you should tell their authors to |
103 | use AnyEvent so their modules work together with others seamlessly... |
109 | use AnyEvent so their modules work together with others seamlessly... |
104 | |
110 | |
105 | The pure-perl implementation of AnyEvent is called |
111 | The pure-perl implementation of AnyEvent is called |
106 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
112 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
107 | explicitly. |
113 | explicitly and enjoy the high availability of that event loop :) |
108 | |
114 | |
109 | =head1 WATCHERS |
115 | =head1 WATCHERS |
110 | |
116 | |
111 | AnyEvent has the central concept of a I<watcher>, which is an object that |
117 | 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 |
118 | stores relevant data for each kind of event you are waiting for, such as |
… | |
… | |
312 | C<cb>, which specifies a callback to be called when the condition variable |
318 | C<cb>, which specifies a callback to be called when the condition variable |
313 | becomes true. |
319 | becomes true. |
314 | |
320 | |
315 | After creation, the condition variable is "false" until it becomes "true" |
321 | After creation, the condition variable is "false" until it becomes "true" |
316 | by calling the C<send> method (or calling the condition variable as if it |
322 | by calling the C<send> method (or calling the condition variable as if it |
317 | were a callback). |
323 | were a callback, read about the caveats in the description for the C<< |
|
|
324 | ->send >> method). |
318 | |
325 | |
319 | Condition variables are similar to callbacks, except that you can |
326 | Condition variables are similar to callbacks, except that you can |
320 | optionally wait for them. They can also be called merge points - points |
327 | optionally wait for them. They can also be called merge points - points |
321 | in time where multiple outstanding events have been processed. And yet |
328 | in time where multiple outstanding events have been processed. And yet |
322 | another way to call them is transactions - each condition variable can be |
329 | another way to call them is transactions - each condition variable can be |
… | |
… | |
394 | immediately from within send. |
401 | immediately from within send. |
395 | |
402 | |
396 | Any arguments passed to the C<send> call will be returned by all |
403 | Any arguments passed to the C<send> call will be returned by all |
397 | future C<< ->recv >> calls. |
404 | future C<< ->recv >> calls. |
398 | |
405 | |
399 | Condition variables are overloaded so one can call them directly (as a |
406 | Condition variables are overloaded so one can call them directly |
400 | code reference). Calling them directly is the same as calling C<send>. |
407 | (as a code reference). Calling them directly is the same as calling |
|
|
408 | C<send>. Note, however, that many C-based event loops do not handle |
|
|
409 | overloading, so as tempting as it may be, passing a condition variable |
|
|
410 | instead of a callback does not work. Both the pure perl and EV loops |
|
|
411 | support overloading, however, as well as all functions that use perl to |
|
|
412 | invoke a callback (as in L<AnyEvent::Socket> and L<AnyEvent::DNS> for |
|
|
413 | example). |
401 | |
414 | |
402 | =item $cv->croak ($error) |
415 | =item $cv->croak ($error) |
403 | |
416 | |
404 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
417 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
405 | C<Carp::croak> with the given error message/object/scalar. |
418 | C<Carp::croak> with the given error message/object/scalar. |
… | |
… | |
519 | The callback will be called when the condition becomes "true", i.e. when |
532 | The callback will be called when the condition becomes "true", i.e. when |
520 | C<send> or C<croak> are called. Calling C<recv> inside the callback |
533 | C<send> or C<croak> are called. Calling C<recv> inside the callback |
521 | or at any later time is guaranteed not to block. |
534 | or at any later time is guaranteed not to block. |
522 | |
535 | |
523 | =back |
536 | =back |
524 | |
|
|
525 | =head3 MAINLOOP EMULATION |
|
|
526 | |
|
|
527 | Sometimes (often for short test scripts, or even standalone programs |
|
|
528 | who only want to use AnyEvent), you I<do> want your program to block |
|
|
529 | indefinitely in some event loop. |
|
|
530 | |
|
|
531 | In that case, you cna use a condition variable like this: |
|
|
532 | |
|
|
533 | AnyEvent->condvar->recv; |
|
|
534 | |
|
|
535 | This has the effect of entering the event loop and looping forever. |
|
|
536 | |
|
|
537 | Note that usually your program has some exit condition, in which case |
|
|
538 | it is better to use the "traditional" approach of storing a condition |
|
|
539 | variable, waiting for it, and sending it when the program should exit |
|
|
540 | cleanly. |
|
|
541 | |
|
|
542 | |
537 | |
543 | =head1 GLOBAL VARIABLES AND FUNCTIONS |
538 | =head1 GLOBAL VARIABLES AND FUNCTIONS |
544 | |
539 | |
545 | =over 4 |
540 | =over 4 |
546 | |
541 | |
… | |
… | |
630 | |
625 | |
631 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
626 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
632 | do anything special (it does not need to be event-based) and let AnyEvent |
627 | do anything special (it does not need to be event-based) and let AnyEvent |
633 | decide which implementation to chose if some module relies on it. |
628 | decide which implementation to chose if some module relies on it. |
634 | |
629 | |
635 | If the main program relies on a specific event model. For example, in |
630 | If the main program relies on a specific event model - for example, in |
636 | Gtk2 programs you have to rely on the Glib module. You should load the |
631 | Gtk2 programs you have to rely on the Glib module - you should load the |
637 | event module before loading AnyEvent or any module that uses it: generally |
632 | event module before loading AnyEvent or any module that uses it: generally |
638 | speaking, you should load it as early as possible. The reason is that |
633 | speaking, you should load it as early as possible. The reason is that |
639 | modules might create watchers when they are loaded, and AnyEvent will |
634 | modules might create watchers when they are loaded, and AnyEvent will |
640 | decide on the event model to use as soon as it creates watchers, and it |
635 | decide on the event model to use as soon as it creates watchers, and it |
641 | might chose the wrong one unless you load the correct one yourself. |
636 | might chose the wrong one unless you load the correct one yourself. |
642 | |
637 | |
643 | You can chose to use a rather inefficient pure-perl implementation by |
638 | You can chose to use a pure-perl implementation by loading the |
644 | loading the C<AnyEvent::Impl::Perl> module, which gives you similar |
639 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
645 | behaviour everywhere, but letting AnyEvent chose is generally better. |
640 | everywhere, but letting AnyEvent chose the model is generally better. |
|
|
641 | |
|
|
642 | =head2 MAINLOOP EMULATION |
|
|
643 | |
|
|
644 | Sometimes (often for short test scripts, or even standalone programs who |
|
|
645 | only want to use AnyEvent), you do not want to run a specific event loop. |
|
|
646 | |
|
|
647 | In that case, you can use a condition variable like this: |
|
|
648 | |
|
|
649 | AnyEvent->condvar->recv; |
|
|
650 | |
|
|
651 | This has the effect of entering the event loop and looping forever. |
|
|
652 | |
|
|
653 | Note that usually your program has some exit condition, in which case |
|
|
654 | it is better to use the "traditional" approach of storing a condition |
|
|
655 | variable somewhere, waiting for it, and sending it when the program should |
|
|
656 | exit cleanly. |
|
|
657 | |
646 | |
658 | |
647 | =head1 OTHER MODULES |
659 | =head1 OTHER MODULES |
648 | |
660 | |
649 | The following is a non-exhaustive list of additional modules that use |
661 | The following is a non-exhaustive list of additional modules that use |
650 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
662 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
… | |
… | |
666 | |
678 | |
667 | Provides various utility functions for (internet protocol) sockets, |
679 | Provides various utility functions for (internet protocol) sockets, |
668 | addresses and name resolution. Also functions to create non-blocking tcp |
680 | addresses and name resolution. Also functions to create non-blocking tcp |
669 | connections or tcp servers, with IPv6 and SRV record support and more. |
681 | connections or tcp servers, with IPv6 and SRV record support and more. |
670 | |
682 | |
|
|
683 | =item L<AnyEvent::DNS> |
|
|
684 | |
|
|
685 | Provides rich asynchronous DNS resolver capabilities. |
|
|
686 | |
671 | =item L<AnyEvent::HTTPD> |
687 | =item L<AnyEvent::HTTPD> |
672 | |
688 | |
673 | Provides a simple web application server framework. |
689 | Provides a simple web application server framework. |
674 | |
|
|
675 | =item L<AnyEvent::DNS> |
|
|
676 | |
|
|
677 | Provides rich asynchronous DNS resolver capabilities. |
|
|
678 | |
690 | |
679 | =item L<AnyEvent::FastPing> |
691 | =item L<AnyEvent::FastPing> |
680 | |
692 | |
681 | The fastest ping in the west. |
693 | The fastest ping in the west. |
682 | |
694 | |
… | |
… | |
725 | no warnings; |
737 | no warnings; |
726 | use strict; |
738 | use strict; |
727 | |
739 | |
728 | use Carp; |
740 | use Carp; |
729 | |
741 | |
730 | our $VERSION = '4.03'; |
742 | our $VERSION = '4.05'; |
731 | our $MODEL; |
743 | our $MODEL; |
732 | |
744 | |
733 | our $AUTOLOAD; |
745 | our $AUTOLOAD; |
734 | our @ISA; |
746 | our @ISA; |
735 | |
747 | |
|
|
748 | our @REGISTRY; |
|
|
749 | |
|
|
750 | our $WIN32; |
|
|
751 | |
|
|
752 | BEGIN { |
|
|
753 | my $win32 = ! ! ($^O =~ /mswin32/i); |
|
|
754 | eval "sub WIN32(){ $win32 }"; |
|
|
755 | } |
|
|
756 | |
736 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
757 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
737 | |
758 | |
738 | our @REGISTRY; |
759 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
739 | |
|
|
740 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
741 | |
760 | |
742 | { |
761 | { |
743 | my $idx; |
762 | my $idx; |
744 | $PROTOCOL{$_} = ++$idx |
763 | $PROTOCOL{$_} = ++$idx |
|
|
764 | for reverse split /\s*,\s*/, |
745 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
765 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
746 | } |
766 | } |
747 | |
767 | |
748 | my @models = ( |
768 | my @models = ( |
749 | [EV:: => AnyEvent::Impl::EV::], |
769 | [EV:: => AnyEvent::Impl::EV::], |
750 | [Event:: => AnyEvent::Impl::Event::], |
770 | [Event:: => AnyEvent::Impl::Event::], |
751 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
752 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
753 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
754 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
771 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
755 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
772 | # everything below here will not be autoprobed |
756 | [Glib:: => AnyEvent::Impl::Glib::], |
773 | # as the pureperl backend should work everywhere |
|
|
774 | # and is usually faster |
|
|
775 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
776 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
757 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
777 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
758 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
778 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
759 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
779 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
780 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
781 | [Prima:: => AnyEvent::Impl::POE::], |
760 | ); |
782 | ); |
761 | |
783 | |
762 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
784 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
763 | |
785 | |
764 | our @post_detect; |
786 | our @post_detect; |
… | |
… | |
784 | } |
806 | } |
785 | |
807 | |
786 | sub detect() { |
808 | sub detect() { |
787 | unless ($MODEL) { |
809 | unless ($MODEL) { |
788 | no strict 'refs'; |
810 | no strict 'refs'; |
|
|
811 | local $SIG{__DIE__}; |
789 | |
812 | |
790 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
813 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
791 | my $model = "AnyEvent::Impl::$1"; |
814 | my $model = "AnyEvent::Impl::$1"; |
792 | if (eval "require $model") { |
815 | if (eval "require $model") { |
793 | $MODEL = $model; |
816 | $MODEL = $model; |
… | |
… | |
916 | or Carp::croak "required option 'pid' is missing"; |
939 | or Carp::croak "required option 'pid' is missing"; |
917 | |
940 | |
918 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
941 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
919 | |
942 | |
920 | unless ($WNOHANG) { |
943 | unless ($WNOHANG) { |
921 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
944 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
922 | } |
945 | } |
923 | |
946 | |
924 | unless ($CHLD_W) { |
947 | unless ($CHLD_W) { |
925 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
948 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
926 | # child could be a zombie already, so make at least one round |
949 | # child could be a zombie already, so make at least one round |
… | |
… | |
1105 | some (broken) firewalls drop such DNS packets, which is why it is off by |
1128 | some (broken) firewalls drop such DNS packets, which is why it is off by |
1106 | default. |
1129 | default. |
1107 | |
1130 | |
1108 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
1131 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
1109 | EDNS0 in its DNS requests. |
1132 | EDNS0 in its DNS requests. |
|
|
1133 | |
|
|
1134 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1135 | |
|
|
1136 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1137 | will create in parallel. |
1110 | |
1138 | |
1111 | =back |
1139 | =back |
1112 | |
1140 | |
1113 | =head1 EXAMPLE PROGRAM |
1141 | =head1 EXAMPLE PROGRAM |
1114 | |
1142 | |