--- AnyEvent/README 2011/08/26 05:33:53 1.67 +++ AnyEvent/README 2014/09/05 22:24:12 1.73 @@ -1,8 +1,9 @@ NAME AnyEvent - the DBI of event loop programming - EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, - Qt, FLTK and POE are various supported event loops/environments. + EV, Event, Glib, Tk, UV, Perl, Event::Lib, Irssi, rxvt-unicode, + IO::Async, Qt, FLTK and POE are various supported event + loops/environments. SYNOPSIS use AnyEvent; @@ -264,7 +265,7 @@ my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { warn "timeout\n"; - }; + }); TIMING ISSUES There are two ways to handle timers: based on real time (relative, "fire @@ -405,8 +406,8 @@ Safe/Unsafe Signals Perl signals can be either "safe" (synchronous to opcode handling) or - "unsafe" (asynchronous) - the former might get delayed indefinitely, the - latter might corrupt your memory. + "unsafe" (asynchronous) - the former might delay signal delivery + indefinitely, the latter might corrupt your memory. AnyEvent signal handlers are, in addition, synchronous to the event loop, i.e. they will not interrupt your running perl program but will @@ -418,19 +419,15 @@ callbacks to signals in a generic way, which is a pity, as you cannot do race-free signal handling in perl, requiring C libraries for this. AnyEvent will try to do its best, which means in some cases, signals - will be delayed. The maximum time a signal might be delayed is specified - in $AnyEvent::MAX_SIGNAL_LATENCY (default: 10 seconds). This variable - can be changed only before the first signal watcher is created, and - should be left alone otherwise. This variable determines how often - AnyEvent polls for signals (in case a wake-up was missed). Higher values - will cause fewer spurious wake-ups, which is better for power and CPU - saving. + will be delayed. The maximum time a signal might be delayed is 10 + seconds by default, but can be overriden via + $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} or $AnyEvent::MAX_SIGNAL_LATENCY + - see the "ENVIRONMENT VARIABLES" section for details. All these problems can be avoided by installing the optional Async::Interrupt module, which works with most event loops. It will not work with inherently broken event loops such as Event or Event::Lib (and - not with POE currently, as POE does its own workaround with one-second - latency). For those, you just have to suffer the delays. + not with POE currently). For those, you just have to suffer the delays. CHILD PROCESS WATCHERS $w = AnyEvent->child (pid => , cb => ); @@ -475,6 +472,10 @@ my $done = AnyEvent->condvar; + # this forks and immediately calls exit in the child. this + # normally has all sorts of bad consequences for your parent, + # so take this as an example only. always fork and exec, + # or call POSIX::_exit, in real code. my $pid = fork or exit 5; my $w = AnyEvent->child ( @@ -724,7 +725,7 @@ "end" before sending. The ping example mentioned above is slightly more complicated, as - the there are results to be passwd back, and the number of tasks + the there are results to be passed back, and the number of tasks that are begun can potentially be zero: my $cv = AnyEvent->condvar; @@ -742,6 +743,10 @@ $cv->end; + ... + + my $results = $cv->recv; + This code fragment supposedly pings a number of hosts and calls "send" after results for all then have have been gathered - in any order. To achieve this, the code issues a call to "begin" when it @@ -780,10 +785,15 @@ Note that doing a blocking wait in a callback is not supported by any event loop, that is, recursive invocation of a blocking "->recv" - is not allowed, and the "recv" call will "croak" if such a condition - is detected. This condition can be slightly loosened by using - Coro::AnyEvent, which allows you to do a blocking "->recv" from any - thread that doesn't run the event loop itself. + is not allowed and the "recv" call will "croak" if such a condition + is detected. This requirement can be dropped by relying on + Coro::AnyEvent , which allows you to do a blocking "->recv" from any + thread that doesn't run the event loop itself. Coro::AnyEvent is + loaded automatically when Coro is used with AnyEvent, so code does + not need to do anything special to take advantage of that: any code + that would normally block your program because it calls "recv", be + executed in an "async" thread instead without blocking other + threads. Not all event models support a blocking wait - some die in that case (programs might want to do that to stay interactive), so *if you are @@ -836,12 +846,13 @@ AnyEvent::Impl::Event based on Event, very stable, few glitches. AnyEvent::Impl::Glib based on Glib, slow but very stable. AnyEvent::Impl::Tk based on Tk, very broken. + AnyEvent::Impl::UV based on UV, innovated square wheels. AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. AnyEvent::Impl::POE based on POE, very slow, some limitations. AnyEvent::Impl::Irssi used when running within irssi. AnyEvent::Impl::IOAsync based on IO::Async. AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. - AnyEvent::Impl::FLTK2 based on FLTK (fltk 2 binding). + AnyEvent::Impl::FLTK based on FLTK (fltk 2 binding). Backends with special needs. Qt requires the Qt::Application to be instantiated first, but will @@ -977,7 +988,7 @@ transaction object or guard to let you cancel the operation. For example, "AnyEvent::Socket::tcp_connect": - # start a conenction attempt unless one is active + # start a connection attempt unless one is active $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { delete $self->{connect_guard}; ... @@ -1012,7 +1023,9 @@ it will load AnyEvent::Log and call "AnyEvent::Log::log" - consequently, look at the AnyEvent::Log documentation for details. - If the test fails it will simply return. + If the test fails it will simply return. Right now this happens when + a numerical loglevel is used and it is larger than the level + specified via $ENV{PERL_ANYEVENT_VERBOSE}. If you want to sprinkle loads of logging calls around your code, consider creating a logger callback with the "AnyEvent::Log::logger" @@ -1086,22 +1099,22 @@ non-exhaustive list), and the list is heavily biased towards modules of the AnyEvent author himself :) - AnyEvent::Util + AnyEvent::Util (part of the AnyEvent distribution) Contains various utility functions that replace often-used blocking functions such as "inet_aton" with event/callback-based versions. - AnyEvent::Socket + AnyEvent::Socket (part of the AnyEvent distribution) Provides various utility functions for (internet protocol) sockets, addresses and name resolution. Also functions to create non-blocking tcp connections or tcp servers, with IPv6 and SRV record support and more. - AnyEvent::Handle + AnyEvent::Handle (part of the AnyEvent distribution) Provide read and write buffers, manages watchers for reads and writes, supports raw and formatted I/O, I/O queued and fully transparent and non-blocking SSL/TLS (via AnyEvent::TLS). - AnyEvent::DNS + AnyEvent::DNS (part of the AnyEvent distribution) Provides rich asynchronous DNS resolver capabilities. AnyEvent::HTTP, AnyEvent::IRC, AnyEvent::XMPP, AnyEvent::GPSD, @@ -1110,12 +1123,24 @@ (for the curious, IGS is the International Go Server and FCP is the Freenet Client Protocol). - AnyEvent::AIO + AnyEvent::AIO (part of the AnyEvent distribution) Truly asynchronous (as opposed to non-blocking) I/O, should be in the toolbox of every event programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent together, giving AnyEvent access to event-based file I/O, and much more. + AnyEvent::Fork, AnyEvent::Fork::RPC, AnyEvent::Fork::Pool, + AnyEvent::Fork::Remote + These let you safely fork new subprocesses, either locally or + remotely (e.g.v ia ssh), using some RPC protocol or not, without the + limitations normally imposed by fork (AnyEvent works fine for + example). Dynamically-resized worker pools are obviously included as + well. + + And they are quite tiny and fast as well - "abusing" AnyEvent::Fork + just to exec external programs can easily beat using "fork" and + "exec" (or even "system") in most programs. + AnyEvent::Filesys::Notify AnyEvent is good for non-blocking stuff, but it can't detect file or path changes (e.g. "watch this directory for new files", "watch this @@ -1126,16 +1151,13 @@ transparently on other platforms, so it's about as portable as it gets. - (I haven't used it myself, but I haven't heard anybody complaining - about it yet). + (I haven't used it myself, but it seems the biggest problem with it + is it quite bad performance). AnyEvent::DBI Executes DBI requests asynchronously in a proxy process for you, notifying you in an event-based way when the operation is finished. - AnyEvent::HTTPD - A simple embedded webserver. - AnyEvent::FastPing The fastest ping in the west. @@ -1218,19 +1240,24 @@ The following environment variables are currently known to AnyEvent: "PERL_ANYEVENT_VERBOSE" - By default, AnyEvent will be completely silent except in fatal - conditions. You can set this environment variable to make AnyEvent - more talkative. If you want to do more than just set the global - logging level you should have a look at "PERL_ANYEVENT_LOG", which - allows much more complex specifications. - - When set to 5 or higher (warn), causes AnyEvent to warn about - unexpected conditions, such as not being able to load the event - model specified by "PERL_ANYEVENT_MODEL", or a guard callback - throwing an exception - this is the minimum recommended level. + By default, AnyEvent will log messages with loglevel 4 ("error") or + higher (see AnyEvent::Log). You can set this environment variable to + a numerical loglevel to make AnyEvent more (or less) talkative. + + If you want to do more than just set the global logging level you + should have a look at "PERL_ANYEVENT_LOG", which allows much more + complex specifications. + + When set to 0 ("off"), then no messages whatsoever will be logged + with everything else at defaults. + + When set to 5 or higher ("warn"), AnyEvent warns about unexpected + conditions, such as not being able to load the event model specified + by "PERL_ANYEVENT_MODEL", or a guard callback throwing an exception + - this is the minimum recommended level for use during development. - When set to 7 or higher (info), cause AnyEvent to report which event - model it chooses. + When set to 7 or higher (info), AnyEvent reports which event model + it chooses. When set to 8 or higher (debug), then AnyEvent will report extra information on which optional modules it loads and how it implements @@ -1252,7 +1279,8 @@ Note that specifying this environment variable causes the AnyEvent::Log module to be loaded, while "PERL_ANYEVENT_VERBOSE" does not, so only using the latter saves a few hundred kB of memory - until the first message is being logged. + unless a module explicitly needs the extra features of + AnyEvent::Log. "PERL_ANYEVENT_STRICT" AnyEvent does not do much argument checking by default, as thorough @@ -1269,10 +1297,10 @@ programs can be very useful, however. "PERL_ANYEVENT_DEBUG_SHELL" - If this env variable is set, then its contents will be interpreted - by "AnyEvent::Socket::parse_hostport" (after replacing every - occurance of $$ by the process pid) and an "AnyEvent::Debug::shell" - is bound on that port. The shell object is saved in + If this env variable is nonempty, then its contents will be + interpreted by "AnyEvent::Socket::parse_hostport" and + "AnyEvent::Debug::shell" (after replacing every occurance of $$ by + the process pid). The shell object is saved in $AnyEvent::Debug::SHELL. This happens when the first watcher is created. @@ -1281,9 +1309,15 @@ /tmp/debug.sock, you could use this: PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog + # connect with e.g.: socat readline /tmp/debug123.sock + + Or to bind to tcp port 4545 on localhost: - Note that creating sockets in /tmp is very unsafe on multiuser - systems. + PERL_ANYEVENT_DEBUG_SHELL=127.0.0.1:4545 perlprog + # connect with e.g.: telnet localhost 4545 + + Note that creating sockets in /tmp or on localhost is very unsafe on + multiuser systems. "PERL_ANYEVENT_DEBUG_WRAP" Can be set to 0, 1 or 2 and enables wrapping of all watchers for @@ -1309,6 +1343,13 @@ PERL_ANYEVENT_MODEL=Perl perl ... + "PERL_ANYEVENT_IO_MODEL" + The current file I/O model - see AnyEvent::IO for more info. + + At the moment, only "Perl" (small, pure-perl, synchronous) and + "IOAIO" (truly asynchronous) are supported. The default is "IOAIO" + if AnyEvent::AIO can be loaded, otherwise it is "Perl". + "PERL_ANYEVENT_PROTOCOLS" Used by both AnyEvent::DNS and AnyEvent::Socket to determine preferences for IPv4 or IPv6. The default is unspecified (and might @@ -1321,7 +1362,7 @@ This variable can effectively be used for denial-of-service attacks against local programs (e.g. when setuid), although the impact is - likely small, as the program has to handle conenction and other + likely small, as the program has to handle connection and other failures anyways. Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over @@ -1354,6 +1395,27 @@ default DNS resolver - this is the maximum number of parallel DNS requests that are sent to the DNS server. + "PERL_ANYEVENT_MAX_SIGNAL_LATENCY" + Perl has inherently racy signal handling (you can basically choose + between losing signals and memory corruption) - pure perl event + loops (including "AnyEvent::Loop", when "Async::Interrupt" isn't + available) therefore have to poll regularly to avoid losing signals. + + Some event loops are racy, but don't poll regularly, and some event + loops are written in C but are still racy. For those event loops, + AnyEvent installs a timer that regularly wakes up the event loop. + + By default, the interval for this timer is 10 seconds, but you can + override this delay with this environment variable (or by setting + the $AnyEvent::MAX_SIGNAL_LATENCY variable before creating signal + watchers). + + Lower values increase CPU (and energy) usage, higher values can + introduce long delays when reaping children or waiting for signals. + + The AnyEvent::Async module, if available, will be used to avoid this + polling (with most event loops). + "PERL_ANYEVENT_RESOLV_CONF" The absolute path to a resolv.conf-style file to use instead of /etc/resolv.conf (or the OS-specific configuration) in the default @@ -1965,6 +2027,15 @@ its own. The pure-perl event loop (AnyEvent::Loop) will additionally load it to try to use a monotonic clock for timing stability. + AnyEvent::AIO (and IO::AIO) + The default implementation of AnyEvent::IO is to do I/O + synchronously, stopping programs while they access the disk, which + is fine for a lot of programs. + + Installing AnyEvent::AIO (and its IO::AIO dependency) makes it + switch to a true asynchronous implementation, so event processing + can continue even while waiting for disk I/O. + FORK Most event libraries are not fork-safe. The ones who are usually are because they rely on inefficient but fork-safe "select" or "poll" calls @@ -1981,17 +2052,34 @@ If you have to fork, you must either do so *before* creating your first watcher OR you must not use AnyEvent at all in the child OR you must do - something completely out of the scope of AnyEvent. + something completely out of the scope of AnyEvent (see below). The problem of doing event processing in the parent *and* the child is much more complicated: even for backends that *are* fork-aware or fork-safe, their behaviour is not usually what you want: fork clones all watchers, that means all timers, I/O watchers etc. are active in both - parent and child, which is almost never what you want. USing "exec" to - start worker children from some kind of manage rprocess is usually + parent and child, which is almost never what you want. Using "exec" to + start worker children from some kind of manage prrocess is usually preferred, because it is much easier and cleaner, at the expense of having to have another binary. + In addition to logical problems with fork, there are also implementation + problems. For example, on POSIX systems, you cannot fork at all in Perl + code if a thread (I am talking of pthreads here) was ever created in the + process, and this is just the tip of the iceberg. In general, using fork + from Perl is difficult, and attempting to use fork without an exec to + implement some kind of parallel processing is almost certainly doomed. + + To safely fork and exec, you should use a module such as Proc::FastSpawn + that let's you safely fork and exec new processes. + + If you want to do multiprocessing using processes, you can look at the + AnyEvent::Fork module (and some related modules such as + AnyEvent::Fork::RPC, AnyEvent::Fork::Pool and AnyEvent::Fork::Remote). + This module allows you to safely create subprocesses without any + limitations - you can use X11 toolkits or AnyEvent in the children + created by AnyEvent::Fork safely and without any special precautions. + SECURITY CONSIDERATIONS AnyEvent can be forced to load any event model via $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used @@ -2045,6 +2133,8 @@ Non-blocking handles, pipes, stream sockets, TCP clients and servers: AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS. + Asynchronous File I/O: AnyEvent::IO. + Asynchronous DNS: AnyEvent::DNS. Thread support: Coro, Coro::AnyEvent, Coro::EV, Coro::Event. @@ -2054,5 +2144,5 @@ AUTHOR Marc Lehmann - http://home.schmorp.de/ + http://anyevent.schmorp.de