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

Comparing AnyEvent/README (file contents):
Revision 1.72 by root, Tue Dec 17 16:43:15 2013 UTC vs.
Revision 1.79 by root, Tue Feb 26 02:08:34 2019 UTC

1NAME 1NAME
2 AnyEvent - the DBI of event loop programming 2 AnyEvent - the DBI of event loop programming
3 3
4 EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, 4 EV, Event, Glib, Tk, UV, Perl, Event::Lib, Irssi, rxvt-unicode,
5 Qt, FLTK and POE are various supported event loops/environments. 5 IO::Async, Qt, FLTK and POE are various supported event
6 loops/environments.
6 7
7SYNOPSIS 8SYNOPSIS
8 use AnyEvent; 9 use AnyEvent;
9 10
10 # if you prefer function calls, look at the AE manpage for 11 # if you prefer function calls, look at the AE manpage for
811 $bool = $cv->ready 812 $bool = $cv->ready
812 Returns true when the condition is "true", i.e. whether "send" or 813 Returns true when the condition is "true", i.e. whether "send" or
813 "croak" have been called. 814 "croak" have been called.
814 815
815 $cb = $cv->cb ($cb->($cv)) 816 $cb = $cv->cb ($cb->($cv))
816 This is a mutator function that returns the callback set and 817 This is a mutator function that returns the callback set (or "undef"
817 optionally replaces it before doing so. 818 if not) and optionally replaces it before doing so.
818 819
819 The callback will be called when the condition becomes "true", i.e. 820 The callback will be called when the condition becomes "true", i.e.
820 when "send" or "croak" are called, with the only argument being the 821 when "send" or "croak" are called, with the only argument being the
821 condition variable itself. If the condition is already true, the 822 condition variable itself. If the condition is already true, the
822 callback is called immediately when it is set. Calling "recv" inside 823 callback is called immediately when it is set. Calling "recv" inside
823 the callback or at any later time is guaranteed not to block. 824 the callback or at any later time is guaranteed not to block.
824 825
826 Additionally, when the callback is invoked, it is also removed from
827 the condvar (reset to "undef"), so the condvar does not keep a
828 reference to the callback after invocation.
829
825SUPPORTED EVENT LOOPS/BACKENDS 830SUPPORTED EVENT LOOPS/BACKENDS
826 The available backend classes are (every class has its own manpage): 831 The following backend classes are part of the AnyEvent distribution
832 (every class has its own manpage):
827 833
828 Backends that are autoprobed when no other event loop can be found. 834 Backends that are autoprobed when no other event loop can be found.
829 EV is the preferred backend when no other event loop seems to be in 835 EV is the preferred backend when no other event loop seems to be in
830 use. If EV is not installed, then AnyEvent will fall back to its own 836 use. If EV is not installed, then AnyEvent will fall back to its own
831 pure-perl implementation, which is available everywhere as it comes 837 pure-perl implementation, which is available everywhere as it comes
843 by the main program. 849 by the main program.
844 850
845 AnyEvent::Impl::Event based on Event, very stable, few glitches. 851 AnyEvent::Impl::Event based on Event, very stable, few glitches.
846 AnyEvent::Impl::Glib based on Glib, slow but very stable. 852 AnyEvent::Impl::Glib based on Glib, slow but very stable.
847 AnyEvent::Impl::Tk based on Tk, very broken. 853 AnyEvent::Impl::Tk based on Tk, very broken.
854 AnyEvent::Impl::UV based on UV, innovated square wheels.
848 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 855 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
849 AnyEvent::Impl::POE based on POE, very slow, some limitations. 856 AnyEvent::Impl::POE based on POE, very slow, some limitations.
850 AnyEvent::Impl::Irssi used when running within irssi. 857 AnyEvent::Impl::Irssi used when running within irssi.
851 AnyEvent::Impl::IOAsync based on IO::Async. 858 AnyEvent::Impl::IOAsync based on IO::Async.
852 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. 859 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop.
875 882
876 AnyEvent knows about both Prima and Wx, however, and will try to 883 AnyEvent knows about both Prima and Wx, however, and will try to
877 load POE when detecting them, in the hope that POE will pick them 884 load POE when detecting them, in the hope that POE will pick them
878 up, in which case everything will be automatic. 885 up, in which case everything will be automatic.
879 886
887 Known event loops outside the AnyEvent distribution
888 The following event loops or programs support AnyEvent by providing
889 their own AnyEvent backend. They will be picked up automatically.
890
891 urxvt::anyevent available to rxvt-unicode extensions
892
880GLOBAL VARIABLES AND FUNCTIONS 893GLOBAL VARIABLES AND FUNCTIONS
881 These are not normally required to use AnyEvent, but can be useful to 894 These are not normally required to use AnyEvent, but can be useful to
882 write AnyEvent extension modules. 895 write AnyEvent extension modules.
883 896
884 $AnyEvent::MODEL 897 $AnyEvent::MODEL
908 $guard = AnyEvent::post_detect { BLOCK } 921 $guard = AnyEvent::post_detect { BLOCK }
909 Arranges for the code block to be executed as soon as the event 922 Arranges for the code block to be executed as soon as the event
910 model is autodetected (or immediately if that has already happened). 923 model is autodetected (or immediately if that has already happened).
911 924
912 The block will be executed *after* the actual backend has been 925 The block will be executed *after* the actual backend has been
913 detected ($AnyEvent::MODEL is set), but *before* any watchers have 926 detected ($AnyEvent::MODEL is set), so it is possible to do some
914 been created, so it is possible to e.g. patch @AnyEvent::ISA or do 927 initialisation only when AnyEvent is actually initialised - see the
915 other initialisations - see the sources of AnyEvent::Strict or
916 AnyEvent::AIO to see how this is used. 928 sources of AnyEvent::AIO to see how this is used.
917 929
918 The most common usage is to create some global watchers, without 930 The most common usage is to create some global watchers, without
919 forcing event module detection too early, for example, AnyEvent::AIO 931 forcing event module detection too early. For example, AnyEvent::AIO
920 creates and installs the global IO::AIO watcher in a "post_detect" 932 creates and installs the global IO::AIO watcher in a "post_detect"
921 block to avoid autodetecting the event module at load time. 933 block to avoid autodetecting the event module at load time.
922 934
923 If called in scalar or list context, then it creates and returns an 935 If called in scalar or list context, then it creates and returns an
924 object that automatically removes the callback again when it is 936 object that automatically removes the callback again when it is
940 # able to just C<undef $WATCHER> if the watcher causes them grief. 952 # able to just C<undef $WATCHER> if the watcher causes them grief.
941 953
942 $WATCHER ||= $guard; 954 $WATCHER ||= $guard;
943 955
944 @AnyEvent::post_detect 956 @AnyEvent::post_detect
945 If there are any code references in this array (you can "push" to it 957 This is a lower level interface then "AnyEvent::post_detect" (the
946 before or after loading AnyEvent), then they will be called directly 958 function). This variable is mainly useful for modules that can do
959 something useful when AnyEvent is used and thus want to know when it
960 is initialised, but do not need to even load it by default. This
961 array provides the means to hook into AnyEvent passively, without
962 loading it.
963
964 Here is how it works: If there are any code references in this array
965 (you can "push" to it before or after loading AnyEvent), then they
947 after the event loop has been chosen. 966 will be called directly after the event loop has been chosen.
948 967
949 You should check $AnyEvent::MODEL before adding to this array, 968 You should check $AnyEvent::MODEL before adding to this array,
950 though: if it is defined then the event loop has already been 969 though: if it is defined then the event loop has already been
951 detected, and the array will be ignored. 970 detected, and the array will be ignored.
952 971
953 Best use "AnyEvent::post_detect { BLOCK }" when your application 972 Best use "AnyEvent::post_detect { BLOCK }" when your application
954 allows it, as it takes care of these details. 973 allows it, as it takes care of these details.
955
956 This variable is mainly useful for modules that can do something
957 useful when AnyEvent is used and thus want to know when it is
958 initialised, but do not need to even load it by default. This array
959 provides the means to hook into AnyEvent passively, without loading
960 it.
961 974
962 Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used 975 Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used
963 together, you could put this into Coro (this is the actual code used 976 together, you could put this into Coro (this is the actual code used
964 by Coro to accomplish this): 977 by Coro to accomplish this):
965 978
984 To understand the usefulness of this function, consider a function 997 To understand the usefulness of this function, consider a function
985 that asynchronously does something for you and returns some 998 that asynchronously does something for you and returns some
986 transaction object or guard to let you cancel the operation. For 999 transaction object or guard to let you cancel the operation. For
987 example, "AnyEvent::Socket::tcp_connect": 1000 example, "AnyEvent::Socket::tcp_connect":
988 1001
989 # start a conenction attempt unless one is active 1002 # start a connection attempt unless one is active
990 $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { 1003 $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub {
991 delete $self->{connect_guard}; 1004 delete $self->{connect_guard};
992 ... 1005 ...
993 }; 1006 };
994 1007
1028 If you want to sprinkle loads of logging calls around your code, 1041 If you want to sprinkle loads of logging calls around your code,
1029 consider creating a logger callback with the "AnyEvent::Log::logger" 1042 consider creating a logger callback with the "AnyEvent::Log::logger"
1030 function, which can reduce typing, codesize and can reduce the 1043 function, which can reduce typing, codesize and can reduce the
1031 logging overhead enourmously. 1044 logging overhead enourmously.
1032 1045
1046 AnyEvent::fh_block $filehandle
1047 AnyEvent::fh_unblock $filehandle
1048 Sets blocking or non-blocking behaviour for the given filehandle.
1049
1033WHAT TO DO IN A MODULE 1050WHAT TO DO IN A MODULE
1034 As a module author, you should "use AnyEvent" and call AnyEvent methods 1051 As a module author, you should "use AnyEvent" and call AnyEvent methods
1035 freely, but you should not load a specific event module or rely on it. 1052 freely, but you should not load a specific event module or rely on it.
1036 1053
1037 Be careful when you create watchers in the module body - AnyEvent will 1054 Be careful when you create watchers in the module body - AnyEvent will
1358 mentioned will be used, and preference will be given to protocols 1375 mentioned will be used, and preference will be given to protocols
1359 mentioned earlier in the list. 1376 mentioned earlier in the list.
1360 1377
1361 This variable can effectively be used for denial-of-service attacks 1378 This variable can effectively be used for denial-of-service attacks
1362 against local programs (e.g. when setuid), although the impact is 1379 against local programs (e.g. when setuid), although the impact is
1363 likely small, as the program has to handle conenction and other 1380 likely small, as the program has to handle connection and other
1364 failures anyways. 1381 failures anyways.
1365 1382
1366 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over 1383 Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over
1367 IPv6, but support both and try to use both. 1384 IPv6, but support both and try to use both.
1368 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to 1385 "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to
1602 my $txn = shift; 1619 my $txn = shift;
1603 my $data = $txn->result; 1620 my $data = $txn->result;
1604 ... 1621 ...
1605 }); 1622 });
1606 1623
1607 EV::loop; 1624 EV::run;
1608 1625
1609 3b. The module user could use AnyEvent, too: 1626 3b. The module user could use AnyEvent, too:
1610 1627
1611 use AnyEvent; 1628 use AnyEvent;
1612 1629
2119 2136
2120 Development/Debugging: AnyEvent::Strict (stricter checking), 2137 Development/Debugging: AnyEvent::Strict (stricter checking),
2121 AnyEvent::Debug (interactive shell, watcher tracing). 2138 AnyEvent::Debug (interactive shell, watcher tracing).
2122 2139
2123 Supported event modules: AnyEvent::Loop, EV, EV::Glib, Glib::EV, Event, 2140 Supported event modules: AnyEvent::Loop, EV, EV::Glib, Glib::EV, Event,
2124 Glib::Event, Glib, Tk, Event::Lib, Qt, POE, FLTK. 2141 Glib::Event, Glib, Tk, Event::Lib, Qt, POE, FLTK, Cocoa::EventLoop, UV.
2125 2142
2126 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 2143 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
2127 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 2144 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
2128 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE, 2145 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE,
2129 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi, AnyEvent::Impl::FLTK. 2146 AnyEvent::Impl::IOAsync, AnyEvent::Impl::Irssi, AnyEvent::Impl::FLTK,
2147 AnyEvent::Impl::Cocoa, AnyEvent::Impl::UV.
2130 2148
2131 Non-blocking handles, pipes, stream sockets, TCP clients and servers: 2149 Non-blocking handles, pipes, stream sockets, TCP clients and servers:
2132 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS. 2150 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS.
2133 2151
2134 Asynchronous File I/O: AnyEvent::IO. 2152 Asynchronous File I/O: AnyEvent::IO.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines