… | |
… | |
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 | |
… | |
… | |
1228 | warn "read: $input\n"; # output what has been read |
1251 | warn "read: $input\n"; # output what has been read |
1229 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1252 | $cv->send if $input =~ /^q/i; # quit program if /^q/i |
1230 | }, |
1253 | }, |
1231 | ); |
1254 | ); |
1232 | |
1255 | |
1233 | my $time_watcher; # can only be used once |
|
|
1234 | |
|
|
1235 | sub new_timer { |
|
|
1236 | $timer = AnyEvent->timer (after => 1, cb => sub { |
1256 | my $time_watcher = AnyEvent->timer (after => 1, interval => 1, cb => sub { |
1237 | warn "timeout\n"; # print 'timeout' about every second |
1257 | warn "timeout\n"; # print 'timeout' at most every second |
1238 | &new_timer; # and restart the time |
|
|
1239 | }); |
|
|
1240 | } |
1258 | }); |
1241 | |
|
|
1242 | new_timer; # create first timer |
|
|
1243 | |
1259 | |
1244 | $cv->recv; # wait until user enters /^q/i |
1260 | $cv->recv; # wait until user enters /^q/i |
1245 | |
1261 | |
1246 | REAL-WORLD EXAMPLE |
1262 | REAL-WORLD EXAMPLE |
1247 | Consider the Net::FCP module. It features (among others) the following |
1263 | Consider the Net::FCP module. It features (among others) the following |
… | |
… | |
1665 | As you can see, the AnyEvent + EV combination even beats the |
1681 | As you can see, the AnyEvent + EV combination even beats the |
1666 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1682 | hand-optimised "raw sockets benchmark", while AnyEvent + its pure perl |
1667 | backend easily beats IO::Lambda and POE. |
1683 | backend easily beats IO::Lambda and POE. |
1668 | |
1684 | |
1669 | And even the 100% non-blocking version written using the high-level (and |
1685 | And even the 100% non-blocking version written using the high-level (and |
1670 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda by a |
1686 | slow :) AnyEvent::Handle abstraction beats both POE and IO::Lambda |
1671 | large margin, even though it does all of DNS, tcp-connect and socket I/O |
1687 | higher level ("unoptimised") abstractions by a large margin, even though |
1672 | in a non-blocking way. |
1688 | it does all of DNS, tcp-connect and socket I/O in a non-blocking way. |
1673 | |
1689 | |
1674 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1690 | The two AnyEvent benchmarks programs can be found as eg/ae0.pl and |
1675 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1691 | eg/ae2.pl in the AnyEvent distribution, the remaining benchmarks are |
1676 | part of the IO::lambda distribution and were used without any changes. |
1692 | part of the IO::Lambda distribution and were used without any changes. |
1677 | |
1693 | |
1678 | SIGNALS |
1694 | SIGNALS |
1679 | AnyEvent currently installs handlers for these signals: |
1695 | AnyEvent currently installs handlers for these signals: |
1680 | |
1696 | |
1681 | SIGCHLD |
1697 | SIGCHLD |
… | |
… | |
1708 | it's built-in modules) are required to use it. |
1724 | it's built-in modules) are required to use it. |
1709 | |
1725 | |
1710 | That does not mean that AnyEvent won't take advantage of some additional |
1726 | That does not mean that AnyEvent won't take advantage of some additional |
1711 | modules if they are installed. |
1727 | modules if they are installed. |
1712 | |
1728 | |
1713 | This section epxlains which additional modules will be used, and how |
1729 | This section explains which additional modules will be used, and how |
1714 | they affect AnyEvent's operetion. |
1730 | they affect AnyEvent's operation. |
1715 | |
1731 | |
1716 | Async::Interrupt |
1732 | Async::Interrupt |
1717 | This slightly arcane module is used to implement fast signal |
1733 | This slightly arcane module is used to implement fast signal |
1718 | handling: To my knowledge, there is no way to do completely |
1734 | handling: To my knowledge, there is no way to do completely |
1719 | race-free and quick signal handling in pure perl. To ensure that |
1735 | race-free and quick signal handling in pure perl. To ensure that |
… | |
… | |
1722 | 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY). |
1738 | 10 seconds, look for $AnyEvent::MAX_SIGNAL_LATENCY). |
1723 | |
1739 | |
1724 | If this module is available, then it will be used to implement |
1740 | If this module is available, then it will be used to implement |
1725 | signal catching, which means that signals will not be delayed, and |
1741 | signal catching, which means that signals will not be delayed, and |
1726 | the event loop will not be interrupted regularly, which is more |
1742 | the event loop will not be interrupted regularly, which is more |
1727 | efficient (And good for battery life on laptops). |
1743 | efficient (and good for battery life on laptops). |
1728 | |
1744 | |
1729 | This affects not just the pure-perl event loop, but also other event |
1745 | This affects not just the pure-perl event loop, but also other event |
1730 | loops that have no signal handling on their own (e.g. Glib, Tk, Qt). |
1746 | loops that have no signal handling on their own (e.g. Glib, Tk, Qt). |
1731 | |
1747 | |
1732 | Some event loops (POE, Event, Event::Lib) offer signal watchers |
1748 | Some event loops (POE, Event, Event::Lib) offer signal watchers |
… | |
… | |
1749 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1765 | "AnyEvent::Util::guard". This speeds up guards considerably (and |
1750 | uses a lot less memory), but otherwise doesn't affect guard |
1766 | uses a lot less memory), but otherwise doesn't affect guard |
1751 | operation much. It is purely used for performance. |
1767 | operation much. It is purely used for performance. |
1752 | |
1768 | |
1753 | JSON and JSON::XS |
1769 | JSON and JSON::XS |
1754 | This module is required when you want to read or write JSON data via |
1770 | One of these modules is required when you want to read or write JSON |
1755 | AnyEvent::Handle. It is also written in pure-perl, but can take |
1771 | data via AnyEvent::Handle. It is also written in pure-perl, but can |
1756 | advantage of the ultra-high-speed JSON::XS module when it is |
1772 | take advantage of the ultra-high-speed JSON::XS module when it is |
1757 | installed. |
1773 | installed. |
1758 | |
1774 | |
1759 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1775 | In fact, AnyEvent::Handle will use JSON::XS by default if it is |
1760 | installed. |
1776 | installed. |
1761 | |
1777 | |
… | |
… | |
1774 | FORK |
1790 | FORK |
1775 | Most event libraries are not fork-safe. The ones who are usually are |
1791 | Most event libraries are not fork-safe. The ones who are usually are |
1776 | because they rely on inefficient but fork-safe "select" or "poll" calls. |
1792 | because they rely on inefficient but fork-safe "select" or "poll" calls. |
1777 | Only EV is fully fork-aware. |
1793 | Only EV is fully fork-aware. |
1778 | |
1794 | |
|
|
1795 | This means that, in general, you cannot fork and do event processing in |
|
|
1796 | the child if a watcher was created before the fork (which in turn |
|
|
1797 | initialises the event library). |
|
|
1798 | |
1779 | If you have to fork, you must either do so *before* creating your first |
1799 | If you have to fork, you must either do so *before* creating your first |
1780 | watcher OR you must not use AnyEvent at all in the child OR you must do |
1800 | watcher OR you must not use AnyEvent at all in the child OR you must do |
1781 | something completely out of the scope of AnyEvent. |
1801 | something completely out of the scope of AnyEvent. |
|
|
1802 | |
|
|
1803 | The problem of doing event processing in the parent *and* the child is |
|
|
1804 | much more complicated: even for backends that *are* fork-aware or |
|
|
1805 | fork-safe, their behaviour is not usually what you want: fork clones all |
|
|
1806 | watchers, that means all timers, I/O watchers etc. are active in both |
|
|
1807 | parent and child, which is almost never what you want. |
1782 | |
1808 | |
1783 | SECURITY CONSIDERATIONS |
1809 | SECURITY CONSIDERATIONS |
1784 | AnyEvent can be forced to load any event model via |
1810 | AnyEvent can be forced to load any event model via |
1785 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used |
1811 | $ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used |
1786 | to execute arbitrary code or directly gain access, it can easily be used |
1812 | to execute arbitrary code or directly gain access, it can easily be used |