… | |
… | |
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. |
… | |
… | |
463 | $done->recv; |
486 | $done->recv; |
464 | |
487 | |
465 | IDLE WATCHERS |
488 | IDLE WATCHERS |
466 | $w = AnyEvent->idle (cb => <callback>); |
489 | $w = AnyEvent->idle (cb => <callback>); |
467 | |
490 | |
468 | Sometimes there is a need to do something, but it is not so important to |
491 | Repeatedly invoke the callback after the process becomes idle, until |
469 | do it instantly, but only when there is nothing better to do. This |
492 | either the watcher is destroyed or new events have been detected. |
470 | "nothing better to do" is usually defined to be "no other events need |
|
|
471 | attention by the event loop". |
|
|
472 | |
493 | |
473 | Idle watchers ideally get invoked when the event loop has nothing better |
494 | Idle watchers are useful when there is a need to do something, but it is |
474 | to do, just before it would block the process to wait for new events. |
495 | not so important (or wise) to do it instantly. The callback will be |
475 | Instead of blocking, the idle watcher is invoked. |
496 | invoked only when there is "nothing better to do", which is usually |
|
|
497 | defined as "all outstanding events have been handled and no new events |
|
|
498 | have been detected". That means that idle watchers ideally get invoked |
|
|
499 | when the event loop has just polled for new events but none have been |
|
|
500 | detected. Instead of blocking to wait for more events, the idle watchers |
|
|
501 | will be invoked. |
476 | |
502 | |
477 | Most event loops unfortunately do not really support idle watchers (only |
503 | Unfortunately, most event loops do not really support idle watchers |
478 | EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent |
504 | (only EV, Event and Glib do it in a usable fashion) - for the rest, |
479 | will simply call the callback "from time to time". |
505 | AnyEvent will simply call the callback "from time to time". |
480 | |
506 | |
481 | Example: read lines from STDIN, but only process them when the program |
507 | Example: read lines from STDIN, but only process them when the program |
482 | is otherwise idle: |
508 | is otherwise idle: |
483 | |
509 | |
484 | my @lines; # read data |
510 | my @lines; # read data |
… | |
… | |
896 | You should check $AnyEvent::MODEL before adding to this array, |
922 | You should check $AnyEvent::MODEL before adding to this array, |
897 | though: if it is defined then the event loop has already been |
923 | though: if it is defined then the event loop has already been |
898 | detected, and the array will be ignored. |
924 | detected, and the array will be ignored. |
899 | |
925 | |
900 | Best use "AnyEvent::post_detect { BLOCK }" when your application |
926 | Best use "AnyEvent::post_detect { BLOCK }" when your application |
901 | allows it,as it takes care of these details. |
927 | allows it, as it takes care of these details. |
902 | |
928 | |
903 | This variable is mainly useful for modules that can do something |
929 | This variable is mainly useful for modules that can do something |
904 | useful when AnyEvent is used and thus want to know when it is |
930 | useful when AnyEvent is used and thus want to know when it is |
905 | initialised, but do not need to even load it by default. This array |
931 | initialised, but do not need to even load it by default. This array |
906 | provides the means to hook into AnyEvent passively, without loading |
932 | provides the means to hook into AnyEvent passively, without loading |
907 | it. |
933 | it. |
|
|
934 | |
|
|
935 | Example: To load Coro::AnyEvent whenever Coro and AnyEvent are used |
|
|
936 | together, you could put this into Coro (this is the actual code used |
|
|
937 | by Coro to accomplish this): |
|
|
938 | |
|
|
939 | if (defined $AnyEvent::MODEL) { |
|
|
940 | # AnyEvent already initialised, so load Coro::AnyEvent |
|
|
941 | require Coro::AnyEvent; |
|
|
942 | } else { |
|
|
943 | # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent |
|
|
944 | # as soon as it is |
|
|
945 | push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; |
|
|
946 | } |
908 | |
947 | |
909 | WHAT TO DO IN A MODULE |
948 | WHAT TO DO IN A MODULE |
910 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
949 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
911 | freely, but you should not load a specific event module or rely on it. |
950 | freely, but you should not load a specific event module or rely on it. |
912 | |
951 | |
… | |
… | |
1701 | it's built-in modules) are required to use it. |
1740 | it's built-in modules) are required to use it. |
1702 | |
1741 | |
1703 | That does not mean that AnyEvent won't take advantage of some additional |
1742 | That does not mean that AnyEvent won't take advantage of some additional |
1704 | modules if they are installed. |
1743 | modules if they are installed. |
1705 | |
1744 | |
1706 | This section epxlains which additional modules will be used, and how |
1745 | This section explains which additional modules will be used, and how |
1707 | they affect AnyEvent's operetion. |
1746 | they affect AnyEvent's operation. |
1708 | |
1747 | |
1709 | Async::Interrupt |
1748 | Async::Interrupt |
1710 | This slightly arcane module is used to implement fast signal |
1749 | This slightly arcane module is used to implement fast signal |
1711 | handling: To my knowledge, there is no way to do completely |
1750 | handling: To my knowledge, there is no way to do completely |
1712 | race-free and quick signal handling in pure perl. To ensure that |
1751 | race-free and quick signal handling in pure perl. To ensure that |
… | |
… | |
1715 | 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY). |
1754 | 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY). |
1716 | |
1755 | |
1717 | If this module is available, then it will be used to implement |
1756 | If this module is available, then it will be used to implement |
1718 | signal catching, which means that signals will not be delayed, and |
1757 | signal catching, which means that signals will not be delayed, and |
1719 | the event loop will not be interrupted regularly, which is more |
1758 | the event loop will not be interrupted regularly, which is more |
1720 | efficient (And good for battery life on laptops). |
1759 | efficient (and good for battery life on laptops). |
1721 | |
1760 | |
1722 | This affects not just the pure-perl event loop, but also other event |
1761 | This affects not just the pure-perl event loop, but also other event |
1723 | loops that have no signal handling on their own (e.g. Glib, Tk, Qt). |
1762 | loops that have no signal handling on their own (e.g. Glib, Tk, Qt). |
1724 | |
1763 | |
1725 | Some event loops (POE, Event, Event::Lib) offer signal watchers |
1764 | Some event loops (POE, Event, Event::Lib) offer signal watchers |
… | |
… | |
1764 | additionally use it to try to use a monotonic clock for timing |
1803 | additionally use it to try to use a monotonic clock for timing |
1765 | stability. |
1804 | stability. |
1766 | |
1805 | |
1767 | FORK |
1806 | FORK |
1768 | Most event libraries are not fork-safe. The ones who are usually are |
1807 | Most event libraries are not fork-safe. The ones who are usually are |
1769 | because they rely on inefficient but fork-safe "select" or "poll" calls. |
1808 | because they rely on inefficient but fork-safe "select" or "poll" calls |
1770 | Only EV is fully fork-aware. |
1809 | - higher performance APIs such as BSD's kqueue or the dreaded Linux |
|
|
1810 | epoll are usually badly thought-out hacks that are incompatible with |
|
|
1811 | fork in one way or another. Only EV is fully fork-aware and ensures that |
|
|
1812 | you continue event-processing in both parent and child (or both, if you |
|
|
1813 | know what you are doing). |
|
|
1814 | |
|
|
1815 | This means that, in general, you cannot fork and do event processing in |
|
|
1816 | the child if the event library was initialised before the fork (which |
|
|
1817 | usually happens when the first AnyEvent watcher is created, or the |
|
|
1818 | library is loaded). |
1771 | |
1819 | |
1772 | If you have to fork, you must either do so *before* creating your first |
1820 | If you have to fork, you must either do so *before* creating your first |
1773 | watcher OR you must not use AnyEvent at all in the child OR you must do |
1821 | watcher OR you must not use AnyEvent at all in the child OR you must do |
1774 | something completely out of the scope of AnyEvent. |
1822 | something completely out of the scope of AnyEvent. |
|
|
1823 | |
|
|
1824 | The problem of doing event processing in the parent *and* the child is |
|
|
1825 | much more complicated: even for backends that *are* fork-aware or |
|
|
1826 | fork-safe, their behaviour is not usually what you want: fork clones all |
|
|
1827 | watchers, that means all timers, I/O watchers etc. are active in both |
|
|
1828 | parent and child, which is almost never what you want. USing "exec" to |
|
|
1829 | start worker children from some kind of manage rprocess is usually |
|
|
1830 | preferred, because it is much easier and cleaner, at the expense of |
|
|
1831 | having to have another binary. |
1775 | |
1832 | |
1776 | SECURITY CONSIDERATIONS |
1833 | SECURITY CONSIDERATIONS |
1777 | AnyEvent can be forced to load any event model via |
1834 | AnyEvent can be forced to load any event model via |
1778 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used |
1835 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used |
1779 | to execute arbitrary code or directly gain access, it can easily be used |
1836 | to execute arbitrary code or directly gain access, it can easily be used |