… | |
… | |
271 | |
271 | |
272 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
272 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
273 | |
273 | |
274 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
274 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
275 | warn "timeout\n"; |
275 | warn "timeout\n"; |
276 | }; |
276 | }); |
277 | |
277 | |
278 | =head3 TIMING ISSUES |
278 | =head3 TIMING ISSUES |
279 | |
279 | |
280 | There are two ways to handle timers: based on real time (relative, "fire |
280 | There are two ways to handle timers: based on real time (relative, "fire |
281 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
281 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
… | |
… | |
487 | |
487 | |
488 | Example: fork a process and wait for it |
488 | Example: fork a process and wait for it |
489 | |
489 | |
490 | my $done = AnyEvent->condvar; |
490 | my $done = AnyEvent->condvar; |
491 | |
491 | |
|
|
492 | # this forks and immediately calls exit in the child. this |
|
|
493 | # normally has all sorts of bad consequences for your parent, |
|
|
494 | # so take this as an example only. always fork and exec, |
|
|
495 | # or call POSIX::_exit, in real code. |
492 | my $pid = fork or exit 5; |
496 | my $pid = fork or exit 5; |
493 | |
497 | |
494 | my $w = AnyEvent->child ( |
498 | my $w = AnyEvent->child ( |
495 | pid => $pid, |
499 | pid => $pid, |
496 | cb => sub { |
500 | cb => sub { |
… | |
… | |
745 | This works because for every event source (EOF on file handle), there is |
749 | This works because for every event source (EOF on file handle), there is |
746 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
750 | one call to C<begin>, so the condvar waits for all calls to C<end> before |
747 | sending. |
751 | sending. |
748 | |
752 | |
749 | The ping example mentioned above is slightly more complicated, as the |
753 | The ping example mentioned above is slightly more complicated, as the |
750 | there are results to be passwd back, and the number of tasks that are |
754 | there are results to be passed back, and the number of tasks that are |
751 | begun can potentially be zero: |
755 | begun can potentially be zero: |
752 | |
756 | |
753 | my $cv = AnyEvent->condvar; |
757 | my $cv = AnyEvent->condvar; |
754 | |
758 | |
755 | my %result; |
759 | my %result; |
… | |
… | |
763 | }; |
767 | }; |
764 | } |
768 | } |
765 | |
769 | |
766 | $cv->end; |
770 | $cv->end; |
767 | |
771 | |
|
|
772 | ... |
|
|
773 | |
|
|
774 | my $results = $cv->recv; |
|
|
775 | |
768 | This code fragment supposedly pings a number of hosts and calls |
776 | This code fragment supposedly pings a number of hosts and calls |
769 | C<send> after results for all then have have been gathered - in any |
777 | C<send> after results for all then have have been gathered - in any |
770 | order. To achieve this, the code issues a call to C<begin> when it starts |
778 | order. To achieve this, the code issues a call to C<begin> when it starts |
771 | each ping request and calls C<end> when it has received some result for |
779 | each ping request and calls C<end> when it has received some result for |
772 | it. Since C<begin> and C<end> only maintain a counter, the order in which |
780 | it. Since C<begin> and C<end> only maintain a counter, the order in which |
… | |
… | |
807 | |
815 | |
808 | In list context, all parameters passed to C<send> will be returned, |
816 | In list context, all parameters passed to C<send> will be returned, |
809 | in scalar context only the first one will be returned. |
817 | in scalar context only the first one will be returned. |
810 | |
818 | |
811 | Note that doing a blocking wait in a callback is not supported by any |
819 | Note that doing a blocking wait in a callback is not supported by any |
812 | event loop, that is, recursive invocation of a blocking C<< ->recv |
820 | event loop, that is, recursive invocation of a blocking C<< ->recv >> is |
813 | >> is not allowed, and the C<recv> call will C<croak> if such a |
821 | not allowed and the C<recv> call will C<croak> if such a condition is |
814 | condition is detected. This condition can be slightly loosened by using |
822 | detected. This requirement can be dropped by relying on L<Coro::AnyEvent> |
815 | L<Coro::AnyEvent>, which allows you to do a blocking C<< ->recv >> from |
823 | , which allows you to do a blocking C<< ->recv >> from any thread |
816 | any thread that doesn't run the event loop itself. |
824 | that doesn't run the event loop itself. L<Coro::AnyEvent> is loaded |
|
|
825 | automatically when L<Coro> is used with L<AnyEvent>, so code does not need |
|
|
826 | to do anything special to take advantage of that: any code that would |
|
|
827 | normally block your program because it calls C<recv>, be executed in an |
|
|
828 | C<async> thread instead without blocking other threads. |
817 | |
829 | |
818 | Not all event models support a blocking wait - some die in that case |
830 | Not all event models support a blocking wait - some die in that case |
819 | (programs might want to do that to stay interactive), so I<if you are |
831 | (programs might want to do that to stay interactive), so I<if you are |
820 | using this from a module, never require a blocking wait>. Instead, let the |
832 | using this from a module, never require a blocking wait>. Instead, let the |
821 | caller decide whether the call will block or not (for example, by coupling |
833 | caller decide whether the call will block or not (for example, by coupling |
… | |
… | |
1139 | a longer non-exhaustive list), and the list is heavily biased towards |
1151 | a longer non-exhaustive list), and the list is heavily biased towards |
1140 | modules of the AnyEvent author himself :) |
1152 | modules of the AnyEvent author himself :) |
1141 | |
1153 | |
1142 | =over 4 |
1154 | =over 4 |
1143 | |
1155 | |
1144 | =item L<AnyEvent::Util> |
1156 | =item L<AnyEvent::Util> (part of the AnyEvent distribution) |
1145 | |
1157 | |
1146 | Contains various utility functions that replace often-used blocking |
1158 | Contains various utility functions that replace often-used blocking |
1147 | functions such as C<inet_aton> with event/callback-based versions. |
1159 | functions such as C<inet_aton> with event/callback-based versions. |
1148 | |
1160 | |
1149 | =item L<AnyEvent::Socket> |
1161 | =item L<AnyEvent::Socket> (part of the AnyEvent distribution) |
1150 | |
1162 | |
1151 | Provides various utility functions for (internet protocol) sockets, |
1163 | Provides various utility functions for (internet protocol) sockets, |
1152 | addresses and name resolution. Also functions to create non-blocking tcp |
1164 | addresses and name resolution. Also functions to create non-blocking tcp |
1153 | connections or tcp servers, with IPv6 and SRV record support and more. |
1165 | connections or tcp servers, with IPv6 and SRV record support and more. |
1154 | |
1166 | |
1155 | =item L<AnyEvent::Handle> |
1167 | =item L<AnyEvent::Handle> (part of the AnyEvent distribution) |
1156 | |
1168 | |
1157 | Provide read and write buffers, manages watchers for reads and writes, |
1169 | Provide read and write buffers, manages watchers for reads and writes, |
1158 | supports raw and formatted I/O, I/O queued and fully transparent and |
1170 | supports raw and formatted I/O, I/O queued and fully transparent and |
1159 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1171 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1160 | |
1172 | |
1161 | =item L<AnyEvent::DNS> |
1173 | =item L<AnyEvent::DNS> (part of the AnyEvent distribution) |
1162 | |
1174 | |
1163 | Provides rich asynchronous DNS resolver capabilities. |
1175 | Provides rich asynchronous DNS resolver capabilities. |
1164 | |
1176 | |
1165 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1177 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1166 | |
1178 | |
1167 | Implement event-based interfaces to the protocols of the same name (for |
1179 | Implement event-based interfaces to the protocols of the same name (for |
1168 | the curious, IGS is the International Go Server and FCP is the Freenet |
1180 | the curious, IGS is the International Go Server and FCP is the Freenet |
1169 | Client Protocol). |
1181 | Client Protocol). |
1170 | |
1182 | |
1171 | =item L<AnyEvent::AIO> |
1183 | =item L<AnyEvent::AIO> (part of the AnyEvent distribution) |
1172 | |
1184 | |
1173 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1185 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1174 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1186 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1175 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1187 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1176 | file I/O, and much more. |
1188 | file I/O, and much more. |
|
|
1189 | |
|
|
1190 | =item L<AnyEvent::Fork>, L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool>, L<AnyEvent::Fork::Remote> |
|
|
1191 | |
|
|
1192 | These let you safely fork new subprocesses, either locally or |
|
|
1193 | remotely (e.g.v ia ssh), using some RPC protocol or not, without |
|
|
1194 | the limitations normally imposed by fork (AnyEvent works fine for |
|
|
1195 | example). Dynamically-resized worker pools are obviously included as well. |
|
|
1196 | |
|
|
1197 | And they are quite tiny and fast as well - "abusing" L<AnyEvent::Fork> |
|
|
1198 | just to exec external programs can easily beat using C<fork> and C<exec> |
|
|
1199 | (or even C<system>) in most programs. |
1177 | |
1200 | |
1178 | =item L<AnyEvent::Filesys::Notify> |
1201 | =item L<AnyEvent::Filesys::Notify> |
1179 | |
1202 | |
1180 | AnyEvent is good for non-blocking stuff, but it can't detect file or |
1203 | AnyEvent is good for non-blocking stuff, but it can't detect file or |
1181 | path changes (e.g. "watch this directory for new files", "watch this |
1204 | path changes (e.g. "watch this directory for new files", "watch this |
… | |
… | |
1183 | do just that in a portbale fashion, supporting inotify on GNU/Linux and |
1206 | do just that in a portbale fashion, supporting inotify on GNU/Linux and |
1184 | some weird, without doubt broken, stuff on OS X to monitor files. It can |
1207 | some weird, without doubt broken, stuff on OS X to monitor files. It can |
1185 | fall back to blocking scans at regular intervals transparently on other |
1208 | fall back to blocking scans at regular intervals transparently on other |
1186 | platforms, so it's about as portable as it gets. |
1209 | platforms, so it's about as portable as it gets. |
1187 | |
1210 | |
1188 | (I haven't used it myself, but I haven't heard anybody complaining about |
1211 | (I haven't used it myself, but it seems the biggest problem with it is |
1189 | it yet). |
1212 | it quite bad performance). |
1190 | |
1213 | |
1191 | =item L<AnyEvent::DBI> |
1214 | =item L<AnyEvent::DBI> |
1192 | |
1215 | |
1193 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1216 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1194 | notifying you in an event-based way when the operation is finished. |
1217 | notifying you in an event-based way when the operation is finished. |
1195 | |
|
|
1196 | =item L<AnyEvent::HTTPD> |
|
|
1197 | |
|
|
1198 | A simple embedded webserver. |
|
|
1199 | |
1218 | |
1200 | =item L<AnyEvent::FastPing> |
1219 | =item L<AnyEvent::FastPing> |
1201 | |
1220 | |
1202 | The fastest ping in the west. |
1221 | The fastest ping in the west. |
1203 | |
1222 | |
… | |
… | |
1221 | |
1240 | |
1222 | =cut |
1241 | =cut |
1223 | |
1242 | |
1224 | package AnyEvent; |
1243 | package AnyEvent; |
1225 | |
1244 | |
1226 | # basically a tuned-down version of common::sense |
1245 | BEGIN { |
1227 | sub common_sense { |
1246 | require "AnyEvent/constants.pl"; |
1228 | # from common:.sense 3.4 |
1247 | &AnyEvent::common_sense; |
1229 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00"; |
|
|
1230 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
|
|
1231 | $^H |= 0x00000600; |
|
|
1232 | } |
1248 | } |
1233 | |
|
|
1234 | BEGIN { AnyEvent::common_sense } |
|
|
1235 | |
1249 | |
1236 | use Carp (); |
1250 | use Carp (); |
1237 | |
1251 | |
1238 | our $VERSION = '6.14'; |
1252 | our $VERSION = '7.07'; |
1239 | our $MODEL; |
1253 | our $MODEL; |
1240 | our @ISA; |
1254 | our @ISA; |
1241 | our @REGISTRY; |
1255 | our @REGISTRY; |
1242 | our $VERBOSE; |
1256 | our $VERBOSE; |
1243 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1257 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1244 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1258 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1245 | |
1259 | |
1246 | BEGIN { |
1260 | BEGIN { |
1247 | require "AnyEvent/constants.pl"; |
|
|
1248 | |
|
|
1249 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1261 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1250 | |
1262 | |
1251 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1263 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1252 | if ${^TAINT}; |
1264 | if ${^TAINT}; |
1253 | |
1265 | |
… | |
… | |
1395 | |
1407 | |
1396 | # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it |
1408 | # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it |
1397 | # the author knows about the problems and what it does to AnyEvent as a whole |
1409 | # the author knows about the problems and what it does to AnyEvent as a whole |
1398 | # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent |
1410 | # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent |
1399 | # anyway. |
1411 | # anyway. |
1400 | AnyEvent::log fatal => "AnyEvent: IO::Async::Loop::AnyEvent detected - that module is broken by\n" |
1412 | AnyEvent::log fatal => "IO::Async::Loop::AnyEvent detected - that module is broken by\n" |
1401 | . "design, abuses internals and breaks AnyEvent - will not continue." |
1413 | . "design, abuses internals and breaks AnyEvent - will not continue." |
1402 | if exists $INC{"IO/Async/Loop/AnyEvent.pm"}; |
1414 | if exists $INC{"IO/Async/Loop/AnyEvent.pm"}; |
1403 | |
1415 | |
1404 | local $!; # for good measure |
1416 | local $!; # for good measure |
1405 | local $SIG{__DIE__}; # we use eval |
1417 | local $SIG{__DIE__}; # we use eval |
… | |
… | |
1416 | |
1428 | |
1417 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1429 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1418 | my $model = $1; |
1430 | my $model = $1; |
1419 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1431 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1420 | if (eval "require $model") { |
1432 | if (eval "require $model") { |
1421 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1433 | AnyEvent::log 7 => "Loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1422 | $MODEL = $model; |
1434 | $MODEL = $model; |
1423 | } else { |
1435 | } else { |
1424 | AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1436 | AnyEvent::log 4 => "Unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1425 | } |
1437 | } |
1426 | } |
1438 | } |
1427 | |
1439 | |
1428 | # check for already loaded models |
1440 | # check for already loaded models |
1429 | unless ($MODEL) { |
1441 | unless ($MODEL) { |
1430 | for (@REGISTRY, @models) { |
1442 | for (@REGISTRY, @models) { |
1431 | my ($package, $model) = @$_; |
1443 | my ($package, $model) = @$_; |
1432 | if (${"$package\::VERSION"} > 0) { |
1444 | if (${"$package\::VERSION"} > 0) { |
1433 | if (eval "require $model") { |
1445 | if (eval "require $model") { |
1434 | AnyEvent::log 7 => "autodetected model '$model', using it."; |
1446 | AnyEvent::log 7 => "Autodetected model '$model', using it."; |
1435 | $MODEL = $model; |
1447 | $MODEL = $model; |
1436 | last; |
1448 | last; |
1437 | } else { |
1449 | } else { |
1438 | AnyEvent::log 8 => "detected event loop $package, but cannot load '$model', skipping: $@"; |
1450 | AnyEvent::log 8 => "Detected event loop $package, but cannot load '$model', skipping: $@"; |
1439 | } |
1451 | } |
1440 | } |
1452 | } |
1441 | } |
1453 | } |
1442 | |
1454 | |
1443 | unless ($MODEL) { |
1455 | unless ($MODEL) { |
… | |
… | |
1447 | if ( |
1459 | if ( |
1448 | eval "require $package" |
1460 | eval "require $package" |
1449 | and ${"$package\::VERSION"} > 0 |
1461 | and ${"$package\::VERSION"} > 0 |
1450 | and eval "require $model" |
1462 | and eval "require $model" |
1451 | ) { |
1463 | ) { |
1452 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1464 | AnyEvent::log 7 => "Autoloaded model '$model', using it."; |
1453 | $MODEL = $model; |
1465 | $MODEL = $model; |
1454 | last; |
1466 | last; |
1455 | } |
1467 | } |
1456 | } |
1468 | } |
1457 | |
1469 | |
1458 | $MODEL |
1470 | $MODEL |
1459 | or AnyEvent::log fatal => "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1471 | or AnyEvent::log fatal => "Backend autodetection failed - did you properly install AnyEvent?"; |
1460 | } |
1472 | } |
1461 | } |
1473 | } |
1462 | |
1474 | |
1463 | # free memory only needed for probing |
1475 | # free memory only needed for probing |
1464 | undef @models; |
1476 | undef @models; |
… | |
… | |
1611 | # probe for availability of Time::HiRes |
1623 | # probe for availability of Time::HiRes |
1612 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1624 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1613 | *time = sub { Time::HiRes::time () }; |
1625 | *time = sub { Time::HiRes::time () }; |
1614 | *AE::time = \& Time::HiRes::time ; |
1626 | *AE::time = \& Time::HiRes::time ; |
1615 | *now = \&time; |
1627 | *now = \&time; |
1616 | AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy."; |
1628 | AnyEvent::log 8 => "using Time::HiRes for sub-second timing accuracy."; |
1617 | # if (eval "use POSIX (); (POSIX::times())... |
1629 | # if (eval "use POSIX (); (POSIX::times())... |
1618 | } else { |
1630 | } else { |
1619 | *time = sub { CORE::time }; |
1631 | *time = sub { CORE::time }; |
1620 | *AE::time = sub (){ CORE::time }; |
1632 | *AE::time = sub (){ CORE::time }; |
1621 | *now = \&time; |
1633 | *now = \&time; |
1622 | AnyEvent::log 3 => "using built-in time(), WARNING, no sub-second resolution!"; |
1634 | AnyEvent::log 3 => "Using built-in time(), no sub-second resolution!"; |
1623 | } |
1635 | } |
1624 | }; |
1636 | }; |
1625 | die if $@; |
1637 | die if $@; |
1626 | |
1638 | |
1627 | &time |
1639 | &time |
… | |
… | |
1721 | |
1733 | |
1722 | sub signal { |
1734 | sub signal { |
1723 | eval q{ # poor man's autoloading {} |
1735 | eval q{ # poor man's autoloading {} |
1724 | # probe for availability of Async::Interrupt |
1736 | # probe for availability of Async::Interrupt |
1725 | if (_have_async_interrupt) { |
1737 | if (_have_async_interrupt) { |
1726 | AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling."; |
1738 | AnyEvent::log 8 => "Using Async::Interrupt for race-free signal handling."; |
1727 | |
1739 | |
1728 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1740 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1729 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1741 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1730 | |
1742 | |
1731 | } else { |
1743 | } else { |
1732 | AnyEvent::log 8 => "using emulated perl signal handling with latency timer."; |
1744 | AnyEvent::log 8 => "Using emulated perl signal handling with latency timer."; |
1733 | |
1745 | |
1734 | if (AnyEvent::WIN32) { |
1746 | if (AnyEvent::WIN32) { |
1735 | require AnyEvent::Util; |
1747 | require AnyEvent::Util; |
1736 | |
1748 | |
1737 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1749 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
… | |
… | |
2176 | |
2188 | |
2177 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2189 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2178 | could start your program like this: |
2190 | could start your program like this: |
2179 | |
2191 | |
2180 | PERL_ANYEVENT_MODEL=Perl perl ... |
2192 | PERL_ANYEVENT_MODEL=Perl perl ... |
|
|
2193 | |
|
|
2194 | =item C<PERL_ANYEVENT_IO_MODEL> |
|
|
2195 | |
|
|
2196 | The current file I/O model - see L<AnyEvent::IO> for more info. |
|
|
2197 | |
|
|
2198 | At the moment, only C<Perl> (small, pure-perl, synchronous) and |
|
|
2199 | C<IOAIO> (truly asynchronous) are supported. The default is C<IOAIO> if |
|
|
2200 | L<AnyEvent::AIO> can be loaded, otherwise it is C<Perl>. |
2181 | |
2201 | |
2182 | =item C<PERL_ANYEVENT_PROTOCOLS> |
2202 | =item C<PERL_ANYEVENT_PROTOCOLS> |
2183 | |
2203 | |
2184 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
2204 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
2185 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
2205 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
… | |
… | |
2912 | This module is part of perl since release 5.008. It will be used when the |
2932 | This module is part of perl since release 5.008. It will be used when the |
2913 | chosen event library does not come with a timing source of its own. The |
2933 | chosen event library does not come with a timing source of its own. The |
2914 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2934 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2915 | try to use a monotonic clock for timing stability. |
2935 | try to use a monotonic clock for timing stability. |
2916 | |
2936 | |
|
|
2937 | =item L<AnyEvent::AIO> (and L<IO::AIO>) |
|
|
2938 | |
|
|
2939 | The default implementation of L<AnyEvent::IO> is to do I/O synchronously, |
|
|
2940 | stopping programs while they access the disk, which is fine for a lot of |
|
|
2941 | programs. |
|
|
2942 | |
|
|
2943 | Installing AnyEvent::AIO (and its IO::AIO dependency) makes it switch to |
|
|
2944 | a true asynchronous implementation, so event processing can continue even |
|
|
2945 | while waiting for disk I/O. |
|
|
2946 | |
2917 | =back |
2947 | =back |
2918 | |
2948 | |
2919 | |
2949 | |
2920 | =head1 FORK |
2950 | =head1 FORK |
2921 | |
2951 | |
… | |
… | |
2932 | usually happens when the first AnyEvent watcher is created, or the library |
2962 | usually happens when the first AnyEvent watcher is created, or the library |
2933 | is loaded). |
2963 | is loaded). |
2934 | |
2964 | |
2935 | If you have to fork, you must either do so I<before> creating your first |
2965 | If you have to fork, you must either do so I<before> creating your first |
2936 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2966 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2937 | something completely out of the scope of AnyEvent. |
2967 | something completely out of the scope of AnyEvent (see below). |
2938 | |
2968 | |
2939 | The problem of doing event processing in the parent I<and> the child |
2969 | The problem of doing event processing in the parent I<and> the child |
2940 | is much more complicated: even for backends that I<are> fork-aware or |
2970 | is much more complicated: even for backends that I<are> fork-aware or |
2941 | fork-safe, their behaviour is not usually what you want: fork clones all |
2971 | fork-safe, their behaviour is not usually what you want: fork clones all |
2942 | watchers, that means all timers, I/O watchers etc. are active in both |
2972 | watchers, that means all timers, I/O watchers etc. are active in both |
2943 | parent and child, which is almost never what you want. USing C<exec> |
2973 | parent and child, which is almost never what you want. Using C<exec> |
2944 | to start worker children from some kind of manage rprocess is usually |
2974 | to start worker children from some kind of manage prrocess is usually |
2945 | preferred, because it is much easier and cleaner, at the expense of having |
2975 | preferred, because it is much easier and cleaner, at the expense of having |
2946 | to have another binary. |
2976 | to have another binary. |
|
|
2977 | |
|
|
2978 | In addition to logical problems with fork, there are also implementation |
|
|
2979 | problems. For example, on POSIX systems, you cannot fork at all in Perl |
|
|
2980 | code if a thread (I am talking of pthreads here) was ever created in the |
|
|
2981 | process, and this is just the tip of the iceberg. In general, using fork |
|
|
2982 | from Perl is difficult, and attempting to use fork without an exec to |
|
|
2983 | implement some kind of parallel processing is almost certainly doomed. |
|
|
2984 | |
|
|
2985 | To safely fork and exec, you should use a module such as |
|
|
2986 | L<Proc::FastSpawn> that let's you safely fork and exec new processes. |
|
|
2987 | |
|
|
2988 | If you want to do multiprocessing using processes, you can |
|
|
2989 | look at the L<AnyEvent::Fork> module (and some related modules |
|
|
2990 | such as L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool> and |
|
|
2991 | L<AnyEvent::Fork::Remote>). This module allows you to safely create |
|
|
2992 | subprocesses without any limitations - you can use X11 toolkits or |
|
|
2993 | AnyEvent in the children created by L<AnyEvent::Fork> safely and without |
|
|
2994 | any special precautions. |
2947 | |
2995 | |
2948 | |
2996 | |
2949 | =head1 SECURITY CONSIDERATIONS |
2997 | =head1 SECURITY CONSIDERATIONS |
2950 | |
2998 | |
2951 | AnyEvent can be forced to load any event model via |
2999 | AnyEvent can be forced to load any event model via |
… | |
… | |
3004 | L<AnyEvent::Impl::FLTK>. |
3052 | L<AnyEvent::Impl::FLTK>. |
3005 | |
3053 | |
3006 | Non-blocking handles, pipes, stream sockets, TCP clients and |
3054 | Non-blocking handles, pipes, stream sockets, TCP clients and |
3007 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
3055 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
3008 | |
3056 | |
|
|
3057 | Asynchronous File I/O: L<AnyEvent::IO>. |
|
|
3058 | |
3009 | Asynchronous DNS: L<AnyEvent::DNS>. |
3059 | Asynchronous DNS: L<AnyEvent::DNS>. |
3010 | |
3060 | |
3011 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
3061 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
3012 | |
3062 | |
3013 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
3063 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
… | |
… | |
3015 | |
3065 | |
3016 | |
3066 | |
3017 | =head1 AUTHOR |
3067 | =head1 AUTHOR |
3018 | |
3068 | |
3019 | Marc Lehmann <schmorp@schmorp.de> |
3069 | Marc Lehmann <schmorp@schmorp.de> |
3020 | http://home.schmorp.de/ |
3070 | http://anyevent.schmorp.de |
3021 | |
3071 | |
3022 | =cut |
3072 | =cut |
3023 | |
3073 | |
3024 | 1 |
3074 | 1 |
3025 | |
3075 | |