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

Comparing AnyEvent/README (file contents):
Revision 1.64 by root, Fri Dec 31 04:50:44 2010 UTC vs.
Revision 1.66 by root, Sun Aug 21 03:02:32 2011 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, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async,
5 Qt and POE are various supported event loops/environments. 5 Qt, FLTK and POE are various supported event loops/environments.
6 6
7SYNOPSIS 7SYNOPSIS
8 use AnyEvent; 8 use AnyEvent;
9 9
10 # if you prefer function calls, look at the AE manpage for 10 # if you prefer function calls, look at the AE manpage for
115 The interface itself is vaguely similar, but not identical to the Event 115 The interface itself is vaguely similar, but not identical to the Event
116 module. 116 module.
117 117
118 During the first call of any watcher-creation method, the module tries 118 During the first call of any watcher-creation method, the module tries
119 to detect the currently loaded event loop by probing whether one of the 119 to detect the currently loaded event loop by probing whether one of the
120 following modules is already loaded: EV, AnyEvent::Impl::Perl, Event, 120 following modules is already loaded: EV, AnyEvent::Loop, Event, Glib,
121 Glib, Tk, Event::Lib, Qt, POE. The first one found is used. If none are 121 Tk, Event::Lib, Qt, POE. The first one found is used. If none are
122 detected, the module tries to load the first four modules in the order 122 detected, the module tries to load the first four modules in the order
123 given; but note that if EV is not available, the pure-perl 123 given; but note that if EV is not available, the pure-perl
124 AnyEvent::Impl::Perl should always work, so the other two are not 124 AnyEvent::Loop should always work, so the other two are not normally
125 normally tried. 125 tried.
126 126
127 Because AnyEvent first checks for modules that are already loaded, 127 Because AnyEvent first checks for modules that are already loaded,
128 loading an event model explicitly before first using AnyEvent will 128 loading an event model explicitly before first using AnyEvent will
129 likely make that model the default. For example: 129 likely make that model the default. For example:
130 130
136 The *likely* means that, if any module loads another event model and 136 The *likely* means that, if any module loads another event model and
137 starts using it, all bets are off - this case should be very rare 137 starts using it, all bets are off - this case should be very rare
138 though, as very few modules hardcode event loops without announcing this 138 though, as very few modules hardcode event loops without announcing this
139 very loudly. 139 very loudly.
140 140
141 The pure-perl implementation of AnyEvent is called 141 The pure-perl implementation of AnyEvent is called "AnyEvent::Loop".
142 "AnyEvent::Impl::Perl". Like other event modules you can load it 142 Like other event modules you can load it explicitly and enjoy the high
143 explicitly and enjoy the high availability of that event loop :) 143 availability of that event loop :)
144 144
145WATCHERS 145WATCHERS
146 AnyEvent has the central concept of a *watcher*, which is an object that 146 AnyEvent has the central concept of a *watcher*, which is an object that
147 stores relevant data for each kind of event you are waiting for, such as 147 stores relevant data for each kind of event you are waiting for, such as
148 the callback to call, the file handle to watch, etc. 148 the callback to call, the file handle to watch, etc.
347 can get whatever behaviour you want with any event loop, by taking 347 can get whatever behaviour you want with any event loop, by taking
348 the difference between "AnyEvent->time" and "AnyEvent->now" into 348 the difference between "AnyEvent->time" and "AnyEvent->now" into
349 account. 349 account.
350 350
351 AnyEvent->now_update 351 AnyEvent->now_update
352 Some event loops (such as EV or AnyEvent::Impl::Perl) cache the 352 Some event loops (such as EV or AnyEvent::Loop) cache the current
353 current time for each loop iteration (see the discussion of 353 time for each loop iteration (see the discussion of AnyEvent->now,
354 AnyEvent->now, above). 354 above).
355 355
356 When a callback runs for a long time (or when the process sleeps), 356 When a callback runs for a long time (or when the process sleeps),
357 then this "current" time will differ substantially from the real 357 then this "current" time will differ substantially from the real
358 time, which might affect timers and time-outs. 358 time, which might affect timers and time-outs.
359 359
466 This means you cannot create a child watcher as the very first thing in 466 This means you cannot create a child watcher as the very first thing in
467 an AnyEvent program, you *have* to create at least one watcher before 467 an AnyEvent program, you *have* to create at least one watcher before
468 you "fork" the child (alternatively, you can call "AnyEvent::detect"). 468 you "fork" the child (alternatively, you can call "AnyEvent::detect").
469 469
470 As most event loops do not support waiting for child events, they will 470 As most event loops do not support waiting for child events, they will
471 be emulated by AnyEvent in most cases, in which the latency and race 471 be emulated by AnyEvent in most cases, in which case the latency and
472 problems mentioned in the description of signal watchers apply. 472 race problems mentioned in the description of signal watchers apply.
473 473
474 Example: fork a process and wait for it 474 Example: fork a process and wait for it
475 475
476 my $done = AnyEvent->condvar; 476 my $done = AnyEvent->condvar;
477 477
821 use. If EV is not installed, then AnyEvent will fall back to its own 821 use. If EV is not installed, then AnyEvent will fall back to its own
822 pure-perl implementation, which is available everywhere as it comes 822 pure-perl implementation, which is available everywhere as it comes
823 with AnyEvent itself. 823 with AnyEvent itself.
824 824
825 AnyEvent::Impl::EV based on EV (interface to libev, best choice). 825 AnyEvent::Impl::EV based on EV (interface to libev, best choice).
826 AnyEvent::Impl::Perl pure-perl implementation, fast and portable. 826 AnyEvent::Impl::Perl pure-perl AnyEvent::Loop, fast and portable.
827 827
828 Backends that are transparently being picked up when they are used. 828 Backends that are transparently being picked up when they are used.
829 These will be used if they are already loaded when the first watcher 829 These will be used if they are already loaded when the first watcher
830 is created, in which case it is assumed that the application is 830 is created, in which case it is assumed that the application is
831 using them. This means that AnyEvent will automatically pick the 831 using them. This means that AnyEvent will automatically pick the
839 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 839 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
840 AnyEvent::Impl::POE based on POE, very slow, some limitations. 840 AnyEvent::Impl::POE based on POE, very slow, some limitations.
841 AnyEvent::Impl::Irssi used when running within irssi. 841 AnyEvent::Impl::Irssi used when running within irssi.
842 AnyEvent::Impl::IOAsync based on IO::Async. 842 AnyEvent::Impl::IOAsync based on IO::Async.
843 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. 843 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop.
844 AnyEvent::Impl::FLTK2 based on FLTK (fltk 2 binding).
844 845
845 Backends with special needs. 846 Backends with special needs.
846 Qt requires the Qt::Application to be instantiated first, but will 847 Qt requires the Qt::Application to be instantiated first, but will
847 otherwise be picked up automatically. As long as the main program 848 otherwise be picked up automatically. As long as the main program
848 instantiates the application before any AnyEvent watchers are 849 instantiates the application before any AnyEvent watchers are
885 Returns $AnyEvent::MODEL, forcing autodetection of the event model 886 Returns $AnyEvent::MODEL, forcing autodetection of the event model
886 if necessary. You should only call this function right before you 887 if necessary. You should only call this function right before you
887 would have created an AnyEvent watcher anyway, that is, as late as 888 would have created an AnyEvent watcher anyway, that is, as late as
888 possible at runtime, and not e.g. during initialisation of your 889 possible at runtime, and not e.g. during initialisation of your
889 module. 890 module.
891
892 The effect of calling this function is as if a watcher had been
893 created (specifically, actions that happen "when the first watcher
894 is created" happen when calling detetc as well).
890 895
891 If you need to do some initialisation before AnyEvent watchers are 896 If you need to do some initialisation before AnyEvent watchers are
892 created, use "post_detect". 897 created, use "post_detect".
893 898
894 $guard = AnyEvent::post_detect { BLOCK } 899 $guard = AnyEvent::post_detect { BLOCK }
956 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent 961 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent
957 # as soon as it is 962 # as soon as it is
958 push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; 963 push @AnyEvent::post_detect, sub { require Coro::AnyEvent };
959 } 964 }
960 965
966 AnyEvent::postpone { BLOCK }
967 Arranges for the block to be executed as soon as possible, but not
968 before the call itself returns. In practise, the block will be
969 executed just before the event loop polls for new events, or shortly
970 afterwards.
971
972 This function never returns anything (to make the "return postpone {
973 ... }" idiom more useful.
974
975 To understand the usefulness of this function, consider a function
976 that asynchronously does something for you and returns some
977 transaction object or guard to let you cancel the operation. For
978 example, "AnyEvent::Socket::tcp_connect":
979
980 # start a conenction attempt unless one is active
981 $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub {
982 delete $self->{connect_guard};
983 ...
984 };
985
986 Imagine that this function could instantly call the callback, for
987 example, because it detects an obvious error such as a negative port
988 number. Invoking the callback before the function returns causes
989 problems however: the callback will be called and will try to delete
990 the guard object. But since the function hasn't returned yet, there
991 is nothing to delete. When the function eventually returns it will
992 assign the guard object to "$self->{connect_guard}", where it will
993 likely never be deleted, so the program thinks it is still trying to
994 connect.
995
996 This is where "AnyEvent::postpone" should be used. Instead of
997 calling the callback directly on error:
998
999 $cb->(undef), return # signal error to callback, BAD!
1000 if $some_error_condition;
1001
1002 It should use "postpone":
1003
1004 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later
1005 if $some_error_condition;
1006
1007 AnyEvent::log $level, $msg[, @args]
1008 Log the given $msg at the given $level.
1009
1010 Loads AnyEvent::Log on first use and calls "AnyEvent::Log::log" -
1011 consequently, look at the AnyEvent::Log documentation for details.
1012
1013 If you want to sprinkle loads of logging calls around your code,
1014 consider creating a logger callback with the "AnyEvent::Log::logger"
1015 function.
1016
961WHAT TO DO IN A MODULE 1017WHAT TO DO IN A MODULE
962 As a module author, you should "use AnyEvent" and call AnyEvent methods 1018 As a module author, you should "use AnyEvent" and call AnyEvent methods
963 freely, but you should not load a specific event module or rely on it. 1019 freely, but you should not load a specific event module or rely on it.
964 1020
965 Be careful when you create watchers in the module body - AnyEvent will 1021 Be careful when you create watchers in the module body - AnyEvent will
995 will decide on the event model to use as soon as it creates watchers, 1051 will decide on the event model to use as soon as it creates watchers,
996 and it might choose the wrong one unless you load the correct one 1052 and it might choose the wrong one unless you load the correct one
997 yourself. 1053 yourself.
998 1054
999 You can chose to use a pure-perl implementation by loading the 1055 You can chose to use a pure-perl implementation by loading the
1000 "AnyEvent::Impl::Perl" module, which gives you similar behaviour 1056 "AnyEvent::Loop" module, which gives you similar behaviour everywhere,
1001 everywhere, but letting AnyEvent chose the model is generally better. 1057 but letting AnyEvent chose the model is generally better.
1002 1058
1003 MAINLOOP EMULATION 1059 MAINLOOP EMULATION
1004 Sometimes (often for short test scripts, or even standalone programs who 1060 Sometimes (often for short test scripts, or even standalone programs who
1005 only want to use AnyEvent), you do not want to run a specific event 1061 only want to use AnyEvent), you do not want to run a specific event
1006 loop. 1062 loop.
1018 1074
1019OTHER MODULES 1075OTHER MODULES
1020 The following is a non-exhaustive list of additional modules that use 1076 The following is a non-exhaustive list of additional modules that use
1021 AnyEvent as a client and can therefore be mixed easily with other 1077 AnyEvent as a client and can therefore be mixed easily with other
1022 AnyEvent modules and other event loops in the same program. Some of the 1078 AnyEvent modules and other event loops in the same program. Some of the
1023 modules come as part of AnyEvent, the others are available via CPAN. 1079 modules come as part of AnyEvent, the others are available via CPAN (see
1080 <http://search.cpan.org/search?m=module&q=anyevent%3A%3A*> for a longer
1081 non-exhaustive list), and the list is heavily biased towards modules of
1082 the AnyEvent author himself :)
1024 1083
1025 AnyEvent::Util 1084 AnyEvent::Util
1026 Contains various utility functions that replace often-used blocking 1085 Contains various utility functions that replace often-used blocking
1027 functions such as "inet_aton" with event/callback-based versions. 1086 functions such as "inet_aton" with event/callback-based versions.
1028 1087
1116 "PERL_ANYEVENT_VERBOSE" 1175 "PERL_ANYEVENT_VERBOSE"
1117 By default, AnyEvent will be completely silent except in fatal 1176 By default, AnyEvent will be completely silent except in fatal
1118 conditions. You can set this environment variable to make AnyEvent 1177 conditions. You can set this environment variable to make AnyEvent
1119 more talkative. 1178 more talkative.
1120 1179
1121 When set to 1 or higher, causes AnyEvent to warn about unexpected 1180 When set to 5 or higher, causes AnyEvent to warn about unexpected
1122 conditions, such as not being able to load the event model specified 1181 conditions, such as not being able to load the event model specified
1123 by "PERL_ANYEVENT_MODEL". 1182 by "PERL_ANYEVENT_MODEL".
1124 1183
1125 When set to 2 or higher, cause AnyEvent to report to STDERR which 1184 When set to 7 or higher, cause AnyEvent to report to STDERR which
1126 event model it chooses. 1185 event model it chooses.
1127 1186
1128 When set to 8 or higher, then AnyEvent will report extra information 1187 When set to 8 or higher, then AnyEvent will report extra information
1129 on which optional modules it loads and how it implements certain 1188 on which optional modules it loads and how it implements certain
1130 features. 1189 features.
1141 Unlike "use strict" (or its modern cousin, "use common::sense", it 1200 Unlike "use strict" (or its modern cousin, "use common::sense", it
1142 is definitely recommended to keep it off in production. Keeping 1201 is definitely recommended to keep it off in production. Keeping
1143 "PERL_ANYEVENT_STRICT=1" in your environment while developing 1202 "PERL_ANYEVENT_STRICT=1" in your environment while developing
1144 programs can be very useful, however. 1203 programs can be very useful, however.
1145 1204
1205 "PERL_ANYEVENT_DEBUG_SHELL"
1206 If this env variable is set, then its contents will be interpreted
1207 by "AnyEvent::Socket::parse_hostport" (after replacing every
1208 occurance of $$ by the process pid) and an "AnyEvent::Debug::shell"
1209 is bound on that port. The shell object is saved in
1210 $AnyEvent::Debug::SHELL.
1211
1212 This takes place when the first watcher is created.
1213
1214 For example, to bind a debug shell on a unix domain socket in
1215 /tmp/debug<pid>.sock, you could use this:
1216
1217 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
1218
1219 Note that creating sockets in /tmp is very unsafe on multiuser
1220 systems.
1221
1222 "PERL_ANYEVENT_DEBUG_WRAP"
1223 Can be set to 0, 1 or 2 and enables wrapping of all watchers for
1224 debugging purposes. See "AnyEvent::Debug::wrap" for details.
1225
1146 "PERL_ANYEVENT_MODEL" 1226 "PERL_ANYEVENT_MODEL"
1147 This can be used to specify the event model to be used by AnyEvent, 1227 This can be used to specify the event model to be used by AnyEvent,
1148 before auto detection and -probing kicks in. It must be a string 1228 before auto detection and -probing kicks in.
1149 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 1229
1150 gets prepended and the resulting module name is loaded and if the 1230 It normally is a string consisting entirely of ASCII letters (e.g.
1151 load was successful, used as event model. If it fails to load 1231 "EV" or "IOAsync"). The string "AnyEvent::Impl::" gets prepended and
1232 the resulting module name is loaded and - if the load was successful
1233 - used as event model backend. If it fails to load then AnyEvent
1152 AnyEvent will proceed with auto detection and -probing. 1234 will proceed with auto detection and -probing.
1153 1235
1154 This functionality might change in future versions. 1236 If the string ends with "::" instead (e.g. "AnyEvent::Impl::EV::")
1237 then nothing gets prepended and the module name is used as-is (hint:
1238 "::" at the end of a string designates a module name and quotes it
1239 appropriately).
1155 1240
1156 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 1241 For example, to force the pure perl model (AnyEvent::Loop::Perl) you
1157 could start your program like this: 1242 could start your program like this:
1158 1243
1159 PERL_ANYEVENT_MODEL=Perl perl ... 1244 PERL_ANYEVENT_MODEL=Perl perl ...
1160 1245
1161 "PERL_ANYEVENT_PROTOCOLS" 1246 "PERL_ANYEVENT_PROTOCOLS"
1534 when used without AnyEvent), but most event loops have acceptable 1619 when used without AnyEvent), but most event loops have acceptable
1535 performance with or without AnyEvent. 1620 performance with or without AnyEvent.
1536 1621
1537 * The overhead AnyEvent adds is usually much smaller than the overhead 1622 * The overhead AnyEvent adds is usually much smaller than the overhead
1538 of the actual event loop, only with extremely fast event loops such 1623 of the actual event loop, only with extremely fast event loops such
1539 as EV adds AnyEvent significant overhead. 1624 as EV does AnyEvent add significant overhead.
1540 1625
1541 * You should avoid POE like the plague if you want performance or 1626 * You should avoid POE like the plague if you want performance or
1542 reasonable memory usage. 1627 reasonable memory usage.
1543 1628
1544 BENCHMARKING THE LARGE SERVER CASE 1629 BENCHMARKING THE LARGE SERVER CASE
1804 the help of AnyEvent::TLS), gains the ability to do TLS/SSL. 1889 the help of AnyEvent::TLS), gains the ability to do TLS/SSL.
1805 1890
1806 Time::HiRes 1891 Time::HiRes
1807 This module is part of perl since release 5.008. It will be used 1892 This module is part of perl since release 5.008. It will be used
1808 when the chosen event library does not come with a timing source of 1893 when the chosen event library does not come with a timing source of
1809 its own. The pure-perl event loop (AnyEvent::Impl::Perl) will 1894 its own. The pure-perl event loop (AnyEvent::Loop) will additionally
1810 additionally use it to try to use a monotonic clock for timing 1895 load it to try to use a monotonic clock for timing stability.
1811 stability.
1812 1896
1813FORK 1897FORK
1814 Most event libraries are not fork-safe. The ones who are usually are 1898 Most event libraries are not fork-safe. The ones who are usually are
1815 because they rely on inefficient but fork-safe "select" or "poll" calls 1899 because they rely on inefficient but fork-safe "select" or "poll" calls
1816 - higher performance APIs such as BSD's kqueue or the dreaded Linux 1900 - higher performance APIs such as BSD's kqueue or the dreaded Linux
1871SEE ALSO 1955SEE ALSO
1872 Tutorial/Introduction: AnyEvent::Intro. 1956 Tutorial/Introduction: AnyEvent::Intro.
1873 1957
1874 FAQ: AnyEvent::FAQ. 1958 FAQ: AnyEvent::FAQ.
1875 1959
1876 Utility functions: AnyEvent::Util. 1960 Utility functions: AnyEvent::Util (misc. grab-bag), AnyEvent::Log
1961 (simply logging).
1877 1962
1878 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1963 Development/Debugging: AnyEvent::Strict (stricter checking),
1879 Event::Lib, Qt, POE. 1964 AnyEvent::Debug (interactive shell, watcher tracing).
1965
1966 Supported event modules: AnyEvent::Loop, EV, EV::Glib, Glib::EV, Event,
1967 Glib::Event, Glib, Tk, Event::Lib, Qt, POE, FLTK.
1880 1968
1881 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1969 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1882 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1970 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1883 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE, 1971 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE,
1884 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi. 1972 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi, AnyEvent::Impl::FLTK.
1885 1973
1886 Non-blocking file handles, sockets, TCP clients and servers: 1974 Non-blocking handles, pipes, stream sockets, TCP clients and servers:
1887 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS. 1975 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS.
1888 1976
1889 Asynchronous DNS: AnyEvent::DNS. 1977 Asynchronous DNS: AnyEvent::DNS.
1890 1978
1891 Thread support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event. 1979 Thread support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines