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.65 by root, Sat Aug 13 22:44:05 2011 UTC

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
961WHAT TO DO IN A MODULE 1007WHAT TO DO IN A MODULE
962 As a module author, you should "use AnyEvent" and call AnyEvent methods 1008 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. 1009 freely, but you should not load a specific event module or rely on it.
964 1010
965 Be careful when you create watchers in the module body - AnyEvent will 1011 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, 1041 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 1042 and it might choose the wrong one unless you load the correct one
997 yourself. 1043 yourself.
998 1044
999 You can chose to use a pure-perl implementation by loading the 1045 You can chose to use a pure-perl implementation by loading the
1000 "AnyEvent::Impl::Perl" module, which gives you similar behaviour 1046 "AnyEvent::Loop" module, which gives you similar behaviour everywhere,
1001 everywhere, but letting AnyEvent chose the model is generally better. 1047 but letting AnyEvent chose the model is generally better.
1002 1048
1003 MAINLOOP EMULATION 1049 MAINLOOP EMULATION
1004 Sometimes (often for short test scripts, or even standalone programs who 1050 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 1051 only want to use AnyEvent), you do not want to run a specific event
1006 loop. 1052 loop.
1141 Unlike "use strict" (or its modern cousin, "use common::sense", it 1187 Unlike "use strict" (or its modern cousin, "use common::sense", it
1142 is definitely recommended to keep it off in production. Keeping 1188 is definitely recommended to keep it off in production. Keeping
1143 "PERL_ANYEVENT_STRICT=1" in your environment while developing 1189 "PERL_ANYEVENT_STRICT=1" in your environment while developing
1144 programs can be very useful, however. 1190 programs can be very useful, however.
1145 1191
1192 "PERL_ANYEVENT_DEBUG_SHELL"
1193 If this env variable is set, then its contents will be interpreted
1194 by "AnyEvent::Socket::parse_hostport" (after replacing every
1195 occurance of $$ by the process pid) and an "AnyEvent::Debug::shell"
1196 is bound on that port. The shell object is saved in
1197 $AnyEvent::Debug::SHELL.
1198
1199 This takes place when the first watcher is created.
1200
1201 For example, to bind a debug shell on a unix domain socket in
1202 /tmp/debug<pid>.sock, you could use this:
1203
1204 PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug\$\$.sock perlprog
1205
1206 Note that creating sockets in /tmp is very unsafe on multiuser
1207 systems.
1208
1209 "PERL_ANYEVENT_DEBUG_WRAP"
1210 Can be set to 0, 1 or 2 and enables wrapping of all watchers for
1211 debugging purposes. See "AnyEvent::Debug::wrap" for details.
1212
1146 "PERL_ANYEVENT_MODEL" 1213 "PERL_ANYEVENT_MODEL"
1147 This can be used to specify the event model to be used by AnyEvent, 1214 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 1215 before auto detection and -probing kicks in.
1149 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 1216
1150 gets prepended and the resulting module name is loaded and if the 1217 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 1218 "EV" or "IOAsync"). The string "AnyEvent::Impl::" gets prepended and
1219 the resulting module name is loaded and - if the load was successful
1220 - used as event model backend. If it fails to load then AnyEvent
1152 AnyEvent will proceed with auto detection and -probing. 1221 will proceed with auto detection and -probing.
1153 1222
1154 This functionality might change in future versions. 1223 If the string ends with "::" instead (e.g. "AnyEvent::Impl::EV::")
1224 then nothing gets prepended and the module name is used as-is (hint:
1225 "::" at the end of a string designates a module name and quotes it
1226 appropriately).
1155 1227
1156 For example, to force the pure perl model (AnyEvent::Impl::Perl) you 1228 For example, to force the pure perl model (AnyEvent::Loop::Perl) you
1157 could start your program like this: 1229 could start your program like this:
1158 1230
1159 PERL_ANYEVENT_MODEL=Perl perl ... 1231 PERL_ANYEVENT_MODEL=Perl perl ...
1160 1232
1161 "PERL_ANYEVENT_PROTOCOLS" 1233 "PERL_ANYEVENT_PROTOCOLS"
1804 the help of AnyEvent::TLS), gains the ability to do TLS/SSL. 1876 the help of AnyEvent::TLS), gains the ability to do TLS/SSL.
1805 1877
1806 Time::HiRes 1878 Time::HiRes
1807 This module is part of perl since release 5.008. It will be used 1879 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 1880 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 1881 its own. The pure-perl event loop (AnyEvent::Loop) will additionally
1810 additionally use it to try to use a monotonic clock for timing 1882 load it to try to use a monotonic clock for timing stability.
1811 stability.
1812 1883
1813FORK 1884FORK
1814 Most event libraries are not fork-safe. The ones who are usually are 1885 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 1886 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 1887 - higher performance APIs such as BSD's kqueue or the dreaded Linux
1873 1944
1874 FAQ: AnyEvent::FAQ. 1945 FAQ: AnyEvent::FAQ.
1875 1946
1876 Utility functions: AnyEvent::Util. 1947 Utility functions: AnyEvent::Util.
1877 1948
1878 Event modules: EV, EV::Glib, Glib::EV, Event, Glib::Event, Glib, Tk, 1949 Event modules: AnyEvent::Loop, EV, EV::Glib, Glib::EV, Event,
1879 Event::Lib, Qt, POE. 1950 Glib::Event, Glib, Tk, Event::Lib, Qt, POE.
1880 1951
1881 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1952 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1882 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1953 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1883 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE, 1954 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE,
1884 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi. 1955 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines