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

Comparing AnyEvent/README (file contents):
Revision 1.45 by root, Fri Jul 17 14:57:03 2009 UTC vs.
Revision 1.49 by root, Tue Jul 28 11:02:19 2009 UTC

1NAME 1NAME
2 AnyEvent - provide framework for multiple event loops 2 AnyEvent - the DBI of event loop programming
3 3
4 EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported 4 EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async,
5 event loops. 5 Qt and POE are various supported event loops/environments.
6 6
7SYNOPSIS 7SYNOPSIS
8 use AnyEvent; 8 use AnyEvent;
9 9
10 # file descriptor readable 10 # file descriptor readable
37 37
38INTRODUCTION/TUTORIAL 38INTRODUCTION/TUTORIAL
39 This manpage is mainly a reference manual. If you are interested in a 39 This manpage is mainly a reference manual. If you are interested in a
40 tutorial or some gentle introduction, have a look at the AnyEvent::Intro 40 tutorial or some gentle introduction, have a look at the AnyEvent::Intro
41 manpage. 41 manpage.
42
43SUPPORT
44 There is a mailinglist for discussing all things AnyEvent, and an IRC
45 channel, too.
46
47 See the AnyEvent project page at the Schmorpforge Ta-Sa Software
48 Repository, at <http://anyevent.schmorp.de>, for more info.
42 49
43WHY YOU SHOULD USE THIS MODULE (OR NOT) 50WHY YOU SHOULD USE THIS MODULE (OR NOT)
44 Glib, POE, IO::Async, Event... CPAN offers event models by the dozen 51 Glib, POE, IO::Async, Event... CPAN offers event models by the dozen
45 nowadays. So what is different about AnyEvent? 52 nowadays. So what is different about AnyEvent?
46 53
350 invocation, and callback invocation will be synchronous. Synchronous 357 invocation, and callback invocation will be synchronous. Synchronous
351 means that it might take a while until the signal gets handled by the 358 means that it might take a while until the signal gets handled by the
352 process, but it is guaranteed not to interrupt any other callbacks. 359 process, but it is guaranteed not to interrupt any other callbacks.
353 360
354 The main advantage of using these watchers is that you can share a 361 The main advantage of using these watchers is that you can share a
355 signal between multiple watchers. 362 signal between multiple watchers, and AnyEvent will ensure that signals
363 will not interrupt your program at bad times.
356 364
357 This watcher might use %SIG, so programs overwriting those signals 365 This watcher might use %SIG (depending on the event loop used), so
358 directly will likely not work correctly. 366 programs overwriting those signals directly will likely not work
367 correctly.
359 368
360 Example: exit on SIGINT 369 Example: exit on SIGINT
361 370
362 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 371 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
372
373 Signal Races, Delays and Workarounds
374 Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching
375 callbacks to signals in a generic way, which is a pity, as you cannot do
376 race-free signal handling in perl. AnyEvent will try to do it's best,
377 but in some cases, signals will be delayed. The maximum time a signal
378 might be delayed is specified in $AnyEvent::MAX_SIGNAL_LATENCY (default:
379 10 seconds). This variable can be changed only before the first signal
380 watcher is created, and should be left alone otherwise. Higher values
381 will cause fewer spurious wake-ups, which is better for power and CPU
382 saving. All these problems can be avoided by installing the optional
383 Async::Interrupt module. This will not work with inherently broken event
384 loops such as Event or Event::Lib (and not with POE currently, as POE
385 does it's own workaround with one-second latency). With those, you just
386 have to suffer the delays.
363 387
364 CHILD PROCESS WATCHERS 388 CHILD PROCESS WATCHERS
365 You can also watch on a child process exit and catch its exit status. 389 You can also watch on a child process exit and catch its exit status.
366 390
367 The child process is specified by the "pid" argument (if set to 0, it 391 The child process is specified by the "pid" argument (one some backends,
368 watches for any child process exit). The watcher will triggered only 392 using 0 watches for any child process exit, on others this will croak).
369 when the child process has finished and an exit status is available, not 393 The watcher will be triggered only when the child process has finished
370 on any trace events (stopped/continued). 394 and an exit status is available, not on any trace events
395 (stopped/continued).
371 396
372 The callback will be called with the pid and exit status (as returned by 397 The callback will be called with the pid and exit status (as returned by
373 waitpid), so unlike other watcher types, you *can* rely on child watcher 398 waitpid), so unlike other watcher types, you *can* rely on child watcher
374 callback arguments. 399 callback arguments.
375 400
390 of when you start the watcher. 415 of when you start the watcher.
391 416
392 This means you cannot create a child watcher as the very first thing in 417 This means you cannot create a child watcher as the very first thing in
393 an AnyEvent program, you *have* to create at least one watcher before 418 an AnyEvent program, you *have* to create at least one watcher before
394 you "fork" the child (alternatively, you can call "AnyEvent::detect"). 419 you "fork" the child (alternatively, you can call "AnyEvent::detect").
420
421 As most event loops do not support waiting for child events, they will
422 be emulated by AnyEvent in most cases, in which the latency and race
423 problems mentioned in the description of signal watchers apply.
395 424
396 Example: fork a process and wait for it 425 Example: fork a process and wait for it
397 426
398 my $done = AnyEvent->condvar; 427 my $done = AnyEvent->condvar;
399 428
473 Condition variables are similar to callbacks, except that you can 502 Condition variables are similar to callbacks, except that you can
474 optionally wait for them. They can also be called merge points - points 503 optionally wait for them. They can also be called merge points - points
475 in time where multiple outstanding events have been processed. And yet 504 in time where multiple outstanding events have been processed. And yet
476 another way to call them is transactions - each condition variable can 505 another way to call them is transactions - each condition variable can
477 be used to represent a transaction, which finishes at some point and 506 be used to represent a transaction, which finishes at some point and
478 delivers a result. 507 delivers a result. And yet some people know them as "futures" - a
508 promise to compute/deliver something that you can wait for.
479 509
480 Condition variables are very useful to signal that something has 510 Condition variables are very useful to signal that something has
481 finished, for example, if you write a module that does asynchronous http 511 finished, for example, if you write a module that does asynchronous http
482 requests, then a condition variable would be the ideal candidate to 512 requests, then a condition variable would be the ideal candidate to
483 signal the availability of results. The user can either act when the 513 signal the availability of results. The user can either act when the
731 761
732 AnyEvent::Impl::Glib based on Glib, slow but very stable. 762 AnyEvent::Impl::Glib based on Glib, slow but very stable.
733 AnyEvent::Impl::Tk based on Tk, very broken. 763 AnyEvent::Impl::Tk based on Tk, very broken.
734 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 764 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
735 AnyEvent::Impl::POE based on POE, very slow, some limitations. 765 AnyEvent::Impl::POE based on POE, very slow, some limitations.
766 AnyEvent::Impl::Irssi used when running within irssi.
736 767
737 Backends with special needs. 768 Backends with special needs.
738 Qt requires the Qt::Application to be instantiated first, but will 769 Qt requires the Qt::Application to be instantiated first, but will
739 otherwise be picked up automatically. As long as the main program 770 otherwise be picked up automatically. As long as the main program
740 instantiates the application before any AnyEvent watchers are 771 instantiates the application before any AnyEvent watchers are
805 creates and installs the global IO::AIO watcher in a "post_detect" 836 creates and installs the global IO::AIO watcher in a "post_detect"
806 block to avoid autodetecting the event module at load time. 837 block to avoid autodetecting the event module at load time.
807 838
808 If called in scalar or list context, then it creates and returns an 839 If called in scalar or list context, then it creates and returns an
809 object that automatically removes the callback again when it is 840 object that automatically removes the callback again when it is
841 destroyed (or "undef" when the hook was immediately executed). See
810 destroyed. See Coro::BDB for a case where this is useful. 842 AnyEvent::AIO for a case where this is useful.
843
844 Example: Create a watcher for the IO::AIO module and store it in
845 $WATCHER. Only do so after the event loop is initialised, though.
846
847 our WATCHER;
848
849 my $guard = AnyEvent::post_detect {
850 $WATCHER = AnyEvent->io (fh => IO::AIO::poll_fileno, poll => 'r', cb => \&IO::AIO::poll_cb);
851 };
852
853 # the ||= is important in case post_detect immediately runs the block,
854 # as to not clobber the newly-created watcher. assigning both watcher and
855 # post_detect guard to the same variable has the advantage of users being
856 # able to just C<undef $WATCHER> if the watcher causes them grief.
857
858 $WATCHER ||= $guard;
811 859
812 @AnyEvent::post_detect 860 @AnyEvent::post_detect
813 If there are any code references in this array (you can "push" to it 861 If there are any code references in this array (you can "push" to it
814 before or after loading AnyEvent), then they will called directly 862 before or after loading AnyEvent), then they will called directly
815 after the event loop has been chosen. 863 after the event loop has been chosen.
991 by "PERL_ANYEVENT_MODEL". 1039 by "PERL_ANYEVENT_MODEL".
992 1040
993 When set to 2 or higher, cause AnyEvent to report to STDERR which 1041 When set to 2 or higher, cause AnyEvent to report to STDERR which
994 event model it chooses. 1042 event model it chooses.
995 1043
1044 When set to 8 or higher, then AnyEvent will report extra information
1045 on which optional modules it loads and how it implements certain
1046 features.
1047
996 "PERL_ANYEVENT_STRICT" 1048 "PERL_ANYEVENT_STRICT"
997 AnyEvent does not do much argument checking by default, as thorough 1049 AnyEvent does not do much argument checking by default, as thorough
998 argument checking is very costly. Setting this variable to a true 1050 argument checking is very costly. Setting this variable to a true
999 value will cause AnyEvent to load "AnyEvent::Strict" and then to 1051 value will cause AnyEvent to load "AnyEvent::Strict" and then to
1000 thoroughly check the arguments passed to most method calls. If it 1052 thoroughly check the arguments passed to most method calls. If it
1001 finds any problems, it will croak. 1053 finds any problems, it will croak.
1002 1054
1003 In other words, enables "strict" mode. 1055 In other words, enables "strict" mode.
1004 1056
1005 Unlike "use strict", it is definitely recommended to keep it off in 1057 Unlike "use strict" (or it's modern cousin, "use common::sense", it
1006 production. Keeping "PERL_ANYEVENT_STRICT=1" in your environment 1058 is definitely recommended to keep it off in production. Keeping
1059 "PERL_ANYEVENT_STRICT=1" in your environment while developing
1007 while developing programs can be very useful, however. 1060 programs can be very useful, however.
1008 1061
1009 "PERL_ANYEVENT_MODEL" 1062 "PERL_ANYEVENT_MODEL"
1010 This can be used to specify the event model to be used by AnyEvent, 1063 This can be used to specify the event model to be used by AnyEvent,
1011 before auto detection and -probing kicks in. It must be a string 1064 before auto detection and -probing kicks in. It must be a string
1012 consisting entirely of ASCII letters. The string "AnyEvent::Impl::" 1065 consisting entirely of ASCII letters. The string "AnyEvent::Impl::"
1069 "PERL_ANYEVENT_CA_FILE", "PERL_ANYEVENT_CA_PATH". 1122 "PERL_ANYEVENT_CA_FILE", "PERL_ANYEVENT_CA_PATH".
1070 When neither "ca_file" nor "ca_path" was specified during 1123 When neither "ca_file" nor "ca_path" was specified during
1071 AnyEvent::TLS context creation, and either of these environment 1124 AnyEvent::TLS context creation, and either of these environment
1072 variables exist, they will be used to specify CA certificate 1125 variables exist, they will be used to specify CA certificate
1073 locations instead of a system-dependent default. 1126 locations instead of a system-dependent default.
1127
1128 "PERL_ANYEVENT_AVOID_GUARD" and "PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT"
1129 When these are set to 1, then the respective modules are not loaded.
1130 Mostly good for testing AnyEvent itself.
1074 1131
1075SUPPLYING YOUR OWN EVENT MODEL INTERFACE 1132SUPPLYING YOUR OWN EVENT MODEL INTERFACE
1076 This is an advanced topic that you do not normally need to use AnyEvent 1133 This is an advanced topic that you do not normally need to use AnyEvent
1077 in a module. This section is only of use to event loop authors who want 1134 in a module. This section is only of use to event loop authors who want
1078 to provide AnyEvent compatibility. 1135 to provide AnyEvent compatibility.
1603 it is that this way, the handler will be restored to defaults on 1660 it is that this way, the handler will be restored to defaults on
1604 exec. 1661 exec.
1605 1662
1606 Feel free to install your own handler, or reset it to defaults. 1663 Feel free to install your own handler, or reset it to defaults.
1607 1664
1665RECOMMENDED/OPTIONAL MODULES
1666 One of AnyEvent's main goals is to be 100% Pure-Perl(tm): only perl (and
1667 it's built-in modules) are required to use it.
1668
1669 That does not mean that AnyEvent won't take advantage of some additional
1670 modules if they are installed.
1671
1672 This section epxlains which additional modules will be used, and how
1673 they affect AnyEvent's operetion.
1674
1675 Async::Interrupt
1676 This slightly arcane module is used to implement fast signal
1677 handling: To my knowledge, there is no way to do completely
1678 race-free and quick signal handling in pure perl. To ensure that
1679 signals still get delivered, AnyEvent will start an interval timer
1680 to wake up perl (and catch the signals) with some delay (default is
1681 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY).
1682
1683 If this module is available, then it will be used to implement
1684 signal catching, which means that signals will not be delayed, and
1685 the event loop will not be interrupted regularly, which is more
1686 efficient (And good for battery life on laptops).
1687
1688 This affects not just the pure-perl event loop, but also other event
1689 loops that have no signal handling on their own (e.g. Glib, Tk, Qt).
1690
1691 Some event loops (POE, Event, Event::Lib) offer signal watchers
1692 natively, and either employ their own workarounds (POE) or use
1693 AnyEvent's workaround (using $AnyEvent::MAX_SIGNAL_LATENCY).
1694 Installing Async::Interrupt does nothing for those backends.
1695
1696 EV This module isn't really "optional", as it is simply one of the
1697 backend event loops that AnyEvent can use. However, it is simply the
1698 best event loop available in terms of features, speed and stability:
1699 It supports the AnyEvent API optimally, implements all the watcher
1700 types in XS, does automatic timer adjustments even when no monotonic
1701 clock is available, can take avdantage of advanced kernel interfaces
1702 such as "epoll" and "kqueue", and is the fastest backend *by far*.
1703 You can even embed Glib/Gtk2 in it (or vice versa, see EV::Glib and
1704 Glib::EV).
1705
1706 Guard
1707 The guard module, when used, will be used to implement
1708 "AnyEvent::Util::guard". This speeds up guards considerably (and
1709 uses a lot less memory), but otherwise doesn't affect guard
1710 operation much. It is purely used for performance.
1711
1712 JSON and JSON::XS
1713 This module is required when you want to read or write JSON data via
1714 AnyEvent::Handle. It is also written in pure-perl, but can take
1715 advantage of the ultra-high-speed JSON::XS module when it is
1716 installed.
1717
1718 In fact, AnyEvent::Handle will use JSON::XS by default if it is
1719 installed.
1720
1721 Net::SSLeay
1722 Implementing TLS/SSL in Perl is certainly interesting, but not very
1723 worthwhile: If this module is installed, then AnyEvent::Handle (with
1724 the help of AnyEvent::TLS), gains the ability to do TLS/SSL.
1725
1726 Time::HiRes
1727 This module is part of perl since release 5.008. It will be used
1728 when the chosen event library does not come with a timing source on
1729 it's own. The pure-perl event loop (AnyEvent::Impl::Perl) will
1730 additionally use it to try to use a monotonic clock for timing
1731 stability.
1732
1608FORK 1733FORK
1609 Most event libraries are not fork-safe. The ones who are usually are 1734 Most event libraries are not fork-safe. The ones who are usually are
1610 because they rely on inefficient but fork-safe "select" or "poll" calls. 1735 because they rely on inefficient but fork-safe "select" or "poll" calls.
1611 Only EV is fully fork-aware. 1736 Only EV is fully fork-aware.
1612 1737
1613 If you have to fork, you must either do so *before* creating your first 1738 If you have to fork, you must either do so *before* creating your first
1614 watcher OR you must not use AnyEvent at all in the child. 1739 watcher OR you must not use AnyEvent at all in the child OR you must do
1740 something completely out of the scope of AnyEvent.
1615 1741
1616SECURITY CONSIDERATIONS 1742SECURITY CONSIDERATIONS
1617 AnyEvent can be forced to load any event model via 1743 AnyEvent can be forced to load any event model via
1618 $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used 1744 $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used
1619 to execute arbitrary code or directly gain access, it can easily be used 1745 to execute arbitrary code or directly gain access, it can easily be used
1651 Event::Lib, Qt, POE. 1777 Event::Lib, Qt, POE.
1652 1778
1653 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event, 1779 Implementations: AnyEvent::Impl::EV, AnyEvent::Impl::Event,
1654 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl, 1780 AnyEvent::Impl::Glib, AnyEvent::Impl::Tk, AnyEvent::Impl::Perl,
1655 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE, 1781 AnyEvent::Impl::EventLib, AnyEvent::Impl::Qt, AnyEvent::Impl::POE,
1656 AnyEvent::Impl::IOAsync. 1782 AnyEvent::Impl::IOAsync, Anyevent::Impl::Irssi.
1657 1783
1658 Non-blocking file handles, sockets, TCP clients and servers: 1784 Non-blocking file handles, sockets, TCP clients and servers:
1659 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS. 1785 AnyEvent::Handle, AnyEvent::Socket, AnyEvent::TLS.
1660 1786
1661 Asynchronous DNS: AnyEvent::DNS. 1787 Asynchronous DNS: AnyEvent::DNS.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines