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

Comparing AnyEvent/README (file contents):
Revision 1.51 by root, Sun Aug 9 16:05:11 2009 UTC vs.
Revision 1.58 by root, Sun Dec 20 22:49:52 2009 UTC

354 time, which might affect timers and time-outs. 354 time, which might affect timers and time-outs.
355 355
356 When this is the case, you can call this method, which will update 356 When this is the case, you can call this method, which will update
357 the event loop's idea of "current time". 357 the event loop's idea of "current time".
358 358
359 A typical example would be a script in a web server (e.g.
360 "mod_perl") - when mod_perl executes the script, then the event loop
361 will have the wrong idea about the "current time" (being potentially
362 far in the past, when the script ran the last time). In that case
363 you should arrange a call to "AnyEvent->now_update" each time the
364 web server process wakes up again (e.g. at the start of your script,
365 or in a handler).
366
359 Note that updating the time *might* cause some events to be handled. 367 Note that updating the time *might* cause some events to be handled.
360 368
361 SIGNAL WATCHERS 369 SIGNAL WATCHERS
362 $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>); 370 $w = AnyEvent->signal (signal => <uppercase_signal_name>, cb => <callback>);
363 371
383 correctly. 391 correctly.
384 392
385 Example: exit on SIGINT 393 Example: exit on SIGINT
386 394
387 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 395 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
396
397 Restart Behaviour
398 While restart behaviour is up to the event loop implementation, most
399 will not restart syscalls (that includes Async::Interrupt and AnyEvent's
400 pure perl implementation).
401
402 Safe/Unsafe Signals
403 Perl signals can be either "safe" (synchronous to opcode handling) or
404 "unsafe" (asynchronous) - the former might get delayed indefinitely, the
405 latter might corrupt your memory.
406
407 AnyEvent signal handlers are, in addition, synchronous to the event
408 loop, i.e. they will not interrupt your running perl program but will
409 only be called as part of the normal event handling (just like timer,
410 I/O etc. callbacks, too).
388 411
389 Signal Races, Delays and Workarounds 412 Signal Races, Delays and Workarounds
390 Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching 413 Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching
391 callbacks to signals in a generic way, which is a pity, as you cannot do 414 callbacks to signals in a generic way, which is a pity, as you cannot do
392 race-free signal handling in perl, requiring C libraries for this. 415 race-free signal handling in perl, requiring C libraries for this.
576 after => 1, 599 after => 1,
577 cb => sub { $result_ready->send }, 600 cb => sub { $result_ready->send },
578 ); 601 );
579 602
580 # this "blocks" (while handling events) till the callback 603 # this "blocks" (while handling events) till the callback
581 # calls -<send 604 # calls ->send
582 $result_ready->recv; 605 $result_ready->recv;
583 606
584 Example: wait for a timer, but take advantage of the fact that condition 607 Example: wait for a timer, but take advantage of the fact that condition
585 variables are also callable directly. 608 variables are also callable directly.
586 609
643 into one. For example, a function that pings many hosts in parallel 666 into one. For example, a function that pings many hosts in parallel
644 might want to use a condition variable for the whole process. 667 might want to use a condition variable for the whole process.
645 668
646 Every call to "->begin" will increment a counter, and every call to 669 Every call to "->begin" will increment a counter, and every call to
647 "->end" will decrement it. If the counter reaches 0 in "->end", the 670 "->end" will decrement it. If the counter reaches 0 in "->end", the
648 (last) callback passed to "begin" will be executed. That callback is 671 (last) callback passed to "begin" will be executed, passing the
649 *supposed* to call "->send", but that is not required. If no 672 condvar as first argument. That callback is *supposed* to call
673 "->send", but that is not required. If no group callback was set,
650 callback was set, "send" will be called without any arguments. 674 "send" will be called without any arguments.
651 675
652 You can think of "$cv->send" giving you an OR condition (one call 676 You can think of "$cv->send" giving you an OR condition (one call
653 sends), while "$cv->begin" and "$cv->end" giving you an AND 677 sends), while "$cv->begin" and "$cv->end" giving you an AND
654 condition (all "begin" calls must be "end"'ed before the condvar 678 condition (all "begin" calls must be "end"'ed before the condvar
655 sends). 679 sends).
683 that are begung can potentially be zero: 707 that are begung can potentially be zero:
684 708
685 my $cv = AnyEvent->condvar; 709 my $cv = AnyEvent->condvar;
686 710
687 my %result; 711 my %result;
688 $cv->begin (sub { $cv->send (\%result) }); 712 $cv->begin (sub { shift->send (\%result) });
689 713
690 for my $host (@list_of_hosts) { 714 for my $host (@list_of_hosts) {
691 $cv->begin; 715 $cv->begin;
692 ping_host_then_call_callback $host, sub { 716 ping_host_then_call_callback $host, sub {
693 $result{$host} = ...; 717 $result{$host} = ...;
895 You should check $AnyEvent::MODEL before adding to this array, 919 You should check $AnyEvent::MODEL before adding to this array,
896 though: if it is defined then the event loop has already been 920 though: if it is defined then the event loop has already been
897 detected, and the array will be ignored. 921 detected, and the array will be ignored.
898 922
899 Best use "AnyEvent::post_detect { BLOCK }" when your application 923 Best use "AnyEvent::post_detect { BLOCK }" when your application
900 allows it,as it takes care of these details. 924 allows it, as it takes care of these details.
901 925
902 This variable is mainly useful for modules that can do something 926 This variable is mainly useful for modules that can do something
903 useful when AnyEvent is used and thus want to know when it is 927 useful when AnyEvent is used and thus want to know when it is
904 initialised, but do not need to even load it by default. This array 928 initialised, but do not need to even load it by default. This array
905 provides the means to hook into AnyEvent passively, without loading 929 provides the means to hook into AnyEvent passively, without loading
906 it. 930 it.
931
932 Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used
933 together, you could put this into Coro (this is the actual code used
934 by Coro to accomplish this):
935
936 if (defined $AnyEvent::MODEL) {
937 # AnyEvent already initialised, so load Coro::AnyEvent
938 require Coro::AnyEvent;
939 } else {
940 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent
941 # as soon as it is
942 push @AnyEvent::post_detect, sub { require Coro::AnyEvent };
943 }
907 944
908WHAT TO DO IN A MODULE 945WHAT TO DO IN A MODULE
909 As a module author, you should "use AnyEvent" and call AnyEvent methods 946 As a module author, you should "use AnyEvent" and call AnyEvent methods
910 freely, but you should not load a specific event module or rely on it. 947 freely, but you should not load a specific event module or rely on it.
911 948
1227 warn "read: $input\n"; # output what has been read 1264 warn "read: $input\n"; # output what has been read
1228 $cv->send if $input =~ /^q/i; # quit program if /^q/i 1265 $cv->send if $input =~ /^q/i; # quit program if /^q/i
1229 }, 1266 },
1230 ); 1267 );
1231 1268
1232 my $time_watcher; # can only be used once
1233
1234 sub new_timer {
1235 $timer = AnyEvent->timer (after => 1, cb => sub { 1269 my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub {
1236 warn "timeout\n"; # print 'timeout' about every second 1270 warn "timeout\n"; # print 'timeout' at most every second
1237 &new_timer; # and restart the time
1238 });
1239 } 1271 });
1240
1241 new_timer; # create first timer
1242 1272
1243 $cv->recv; # wait until user enters /^q/i 1273 $cv->recv; # wait until user enters /^q/i
1244 1274
1245REAL-WORLD EXAMPLE 1275REAL-WORLD EXAMPLE
1246 Consider the Net::FCP module. It features (among others) the following 1276 Consider the Net::FCP module. It features (among others) the following
1664 As you can see, the AnyEvent + EV combination even beats the 1694 As you can see, the AnyEvent + EV combination even beats the
1665 hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl 1695 hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl
1666 backend easily beats IO::Lambda and POE. 1696 backend easily beats IO::Lambda and POE.
1667 1697
1668 And even the 100% non-blocking version written using the high-level (and 1698 And even the 100% non-blocking version written using the high-level (and
1669 slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda by a 1699 slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda
1670 large margin, even though it does all of DNS, tcp-connect and socket I/O 1700 higher level ("unoptimised") abstractions by a large margin, even though
1671 in a non-blocking way. 1701 it does all of DNS, tcp-connect and socket I/O in a non-blocking way.
1672 1702
1673 The two AnyEvent benchmarks programs can be found as eg/ae0.pl and 1703 The two AnyEvent benchmarks programs can be found as eg/ae0.pl and
1674 eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are 1704 eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are
1675 part of the IO::lambda distribution and were used without any changes. 1705 part of the IO::Lambda distribution and were used without any changes.
1676 1706
1677SIGNALS 1707SIGNALS
1678 AnyEvent currently installs handlers for these signals: 1708 AnyEvent currently installs handlers for these signals:
1679 1709
1680 SIGCHLD 1710 SIGCHLD
1707 it's built-in modules) are required to use it. 1737 it's built-in modules) are required to use it.
1708 1738
1709 That does not mean that AnyEvent won't take advantage of some additional 1739 That does not mean that AnyEvent won't take advantage of some additional
1710 modules if they are installed. 1740 modules if they are installed.
1711 1741
1712 This section epxlains which additional modules will be used, and how 1742 This section explains which additional modules will be used, and how
1713 they affect AnyEvent's operetion. 1743 they affect AnyEvent's operation.
1714 1744
1715 Async::Interrupt 1745 Async::Interrupt
1716 This slightly arcane module is used to implement fast signal 1746 This slightly arcane module is used to implement fast signal
1717 handling: To my knowledge, there is no way to do completely 1747 handling: To my knowledge, there is no way to do completely
1718 race-free and quick signal handling in pure perl. To ensure that 1748 race-free and quick signal handling in pure perl. To ensure that
1721 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY). 1751 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY).
1722 1752
1723 If this module is available, then it will be used to implement 1753 If this module is available, then it will be used to implement
1724 signal catching, which means that signals will not be delayed, and 1754 signal catching, which means that signals will not be delayed, and
1725 the event loop will not be interrupted regularly, which is more 1755 the event loop will not be interrupted regularly, which is more
1726 efficient (And good for battery life on laptops). 1756 efficient (and good for battery life on laptops).
1727 1757
1728 This affects not just the pure-perl event loop, but also other event 1758 This affects not just the pure-perl event loop, but also other event
1729 loops that have no signal handling on their own (e.g. Glib, Tk, Qt). 1759 loops that have no signal handling on their own (e.g. Glib, Tk, Qt).
1730 1760
1731 Some event loops (POE, Event, Event::Lib) offer signal watchers 1761 Some event loops (POE, Event, Event::Lib) offer signal watchers
1748 "AnyEvent::Util::guard". This speeds up guards considerably (and 1778 "AnyEvent::Util::guard". This speeds up guards considerably (and
1749 uses a lot less memory), but otherwise doesn't affect guard 1779 uses a lot less memory), but otherwise doesn't affect guard
1750 operation much. It is purely used for performance. 1780 operation much. It is purely used for performance.
1751 1781
1752 JSON and JSON::XS 1782 JSON and JSON::XS
1753 This module is required when you want to read or write JSON data via 1783 One of these modules is required when you want to read or write JSON
1754 AnyEvent::Handle. It is also written in pure-perl, but can take 1784 data via AnyEvent::Handle. It is also written in pure-perl, but can
1755 advantage of the ultra-high-speed JSON::XS module when it is 1785 take advantage of the ultra-high-speed JSON::XS module when it is
1756 installed. 1786 installed.
1757 1787
1758 In fact, AnyEvent::Handle will use JSON::XS by default if it is 1788 In fact, AnyEvent::Handle will use JSON::XS by default if it is
1759 installed. 1789 installed.
1760 1790
1773FORK 1803FORK
1774 Most event libraries are not fork-safe. The ones who are usually are 1804 Most event libraries are not fork-safe. The ones who are usually are
1775 because they rely on inefficient but fork-safe "select" or "poll" calls. 1805 because they rely on inefficient but fork-safe "select" or "poll" calls.
1776 Only EV is fully fork-aware. 1806 Only EV is fully fork-aware.
1777 1807
1808 This means that, in general, you cannot fork and do event processing in
1809 the child if a watcher was created before the fork (which in turn
1810 initialises the event library).
1811
1778 If you have to fork, you must either do so *before* creating your first 1812 If you have to fork, you must either do so *before* creating your first
1779 watcher OR you must not use AnyEvent at all in the child OR you must do 1813 watcher OR you must not use AnyEvent at all in the child OR you must do
1780 something completely out of the scope of AnyEvent. 1814 something completely out of the scope of AnyEvent.
1815
1816 The problem of doing event processing in the parent *and* the child is
1817 much more complicated: even for backends that *are* fork-aware or
1818 fork-safe, their behaviour is not usually what you want: fork clones all
1819 watchers, that means all timers, I/O watchers etc. are active in both
1820 parent and child, which is almost never what you want.
1781 1821
1782SECURITY CONSIDERATIONS 1822SECURITY CONSIDERATIONS
1783 AnyEvent can be forced to load any event model via 1823 AnyEvent can be forced to load any event model via
1784 $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used 1824 $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used
1785 to execute arbitrary code or directly gain access, it can easily be used 1825 to execute arbitrary code or directly gain access, it can easily be used

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines