1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - the DBI of event loop programming |
3 | AnyEvent - the DBI of event loop programming |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt, |
5 | EV, Event, Glib, Tk, UV, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, |
6 | FLTK and POE are various supported event loops/environments. |
6 | Qt, FLTK and POE are various supported event loops/environments. |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
9 | |
9 | |
10 | use AnyEvent; |
10 | use AnyEvent; |
11 | |
11 | |
… | |
… | |
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 |
… | |
… | |
871 | create watchers. Nothing special needs to be done by the main program. |
883 | create watchers. Nothing special needs to be done by the main program. |
872 | |
884 | |
873 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
885 | AnyEvent::Impl::Event based on Event, very stable, few glitches. |
874 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
886 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
875 | AnyEvent::Impl::Tk based on Tk, very broken. |
887 | AnyEvent::Impl::Tk based on Tk, very broken. |
|
|
888 | AnyEvent::Impl::UV based on UV, innovated square wheels. |
876 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
889 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
877 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
890 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
878 | AnyEvent::Impl::Irssi used when running within irssi. |
891 | AnyEvent::Impl::Irssi used when running within irssi. |
879 | AnyEvent::Impl::IOAsync based on IO::Async. |
892 | AnyEvent::Impl::IOAsync based on IO::Async. |
880 | AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. |
893 | AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. |
… | |
… | |
1021 | To understand the usefulness of this function, consider a function that |
1034 | To understand the usefulness of this function, consider a function that |
1022 | asynchronously does something for you and returns some transaction |
1035 | asynchronously does something for you and returns some transaction |
1023 | object or guard to let you cancel the operation. For example, |
1036 | object or guard to let you cancel the operation. For example, |
1024 | C<AnyEvent::Socket::tcp_connect>: |
1037 | C<AnyEvent::Socket::tcp_connect>: |
1025 | |
1038 | |
1026 | # start a conenction attempt unless one is active |
1039 | # start a connection attempt unless one is active |
1027 | $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { |
1040 | $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub { |
1028 | delete $self->{connect_guard}; |
1041 | delete $self->{connect_guard}; |
1029 | ... |
1042 | ... |
1030 | }; |
1043 | }; |
1031 | |
1044 | |
… | |
… | |
1139 | a longer non-exhaustive list), and the list is heavily biased towards |
1152 | a longer non-exhaustive list), and the list is heavily biased towards |
1140 | modules of the AnyEvent author himself :) |
1153 | modules of the AnyEvent author himself :) |
1141 | |
1154 | |
1142 | =over 4 |
1155 | =over 4 |
1143 | |
1156 | |
1144 | =item L<AnyEvent::Util> |
1157 | =item L<AnyEvent::Util> (part of the AnyEvent distribution) |
1145 | |
1158 | |
1146 | Contains various utility functions that replace often-used blocking |
1159 | Contains various utility functions that replace often-used blocking |
1147 | functions such as C<inet_aton> with event/callback-based versions. |
1160 | functions such as C<inet_aton> with event/callback-based versions. |
1148 | |
1161 | |
1149 | =item L<AnyEvent::Socket> |
1162 | =item L<AnyEvent::Socket> (part of the AnyEvent distribution) |
1150 | |
1163 | |
1151 | Provides various utility functions for (internet protocol) sockets, |
1164 | Provides various utility functions for (internet protocol) sockets, |
1152 | addresses and name resolution. Also functions to create non-blocking tcp |
1165 | addresses and name resolution. Also functions to create non-blocking tcp |
1153 | connections or tcp servers, with IPv6 and SRV record support and more. |
1166 | connections or tcp servers, with IPv6 and SRV record support and more. |
1154 | |
1167 | |
1155 | =item L<AnyEvent::Handle> |
1168 | =item L<AnyEvent::Handle> (part of the AnyEvent distribution) |
1156 | |
1169 | |
1157 | Provide read and write buffers, manages watchers for reads and writes, |
1170 | 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 |
1171 | supports raw and formatted I/O, I/O queued and fully transparent and |
1159 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1172 | non-blocking SSL/TLS (via L<AnyEvent::TLS>). |
1160 | |
1173 | |
1161 | =item L<AnyEvent::DNS> |
1174 | =item L<AnyEvent::DNS> (part of the AnyEvent distribution) |
1162 | |
1175 | |
1163 | Provides rich asynchronous DNS resolver capabilities. |
1176 | Provides rich asynchronous DNS resolver capabilities. |
1164 | |
1177 | |
1165 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1178 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
1166 | |
1179 | |
1167 | Implement event-based interfaces to the protocols of the same name (for |
1180 | 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 |
1181 | the curious, IGS is the International Go Server and FCP is the Freenet |
1169 | Client Protocol). |
1182 | Client Protocol). |
1170 | |
1183 | |
1171 | =item L<AnyEvent::AIO> |
1184 | =item L<AnyEvent::AIO> (part of the AnyEvent distribution) |
1172 | |
1185 | |
1173 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1186 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
1174 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1187 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
1175 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1188 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
1176 | file I/O, and much more. |
1189 | file I/O, and much more. |
|
|
1190 | |
|
|
1191 | =item L<AnyEvent::Fork>, L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool>, L<AnyEvent::Fork::Remote> |
|
|
1192 | |
|
|
1193 | These let you safely fork new subprocesses, either locally or |
|
|
1194 | remotely (e.g.v ia ssh), using some RPC protocol or not, without |
|
|
1195 | the limitations normally imposed by fork (AnyEvent works fine for |
|
|
1196 | example). Dynamically-resized worker pools are obviously included as well. |
|
|
1197 | |
|
|
1198 | And they are quite tiny and fast as well - "abusing" L<AnyEvent::Fork> |
|
|
1199 | just to exec external programs can easily beat using C<fork> and C<exec> |
|
|
1200 | (or even C<system>) in most programs. |
1177 | |
1201 | |
1178 | =item L<AnyEvent::Filesys::Notify> |
1202 | =item L<AnyEvent::Filesys::Notify> |
1179 | |
1203 | |
1180 | AnyEvent is good for non-blocking stuff, but it can't detect file or |
1204 | 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 |
1205 | 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 |
1207 | 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 |
1208 | 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 |
1209 | fall back to blocking scans at regular intervals transparently on other |
1186 | platforms, so it's about as portable as it gets. |
1210 | platforms, so it's about as portable as it gets. |
1187 | |
1211 | |
1188 | (I haven't used it myself, but I haven't heard anybody complaining about |
1212 | (I haven't used it myself, but it seems the biggest problem with it is |
1189 | it yet). |
1213 | it quite bad performance). |
1190 | |
1214 | |
1191 | =item L<AnyEvent::DBI> |
1215 | =item L<AnyEvent::DBI> |
1192 | |
1216 | |
1193 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1217 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1194 | notifying you in an event-based way when the operation is finished. |
1218 | 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 | |
1219 | |
1200 | =item L<AnyEvent::FastPing> |
1220 | =item L<AnyEvent::FastPing> |
1201 | |
1221 | |
1202 | The fastest ping in the west. |
1222 | The fastest ping in the west. |
1203 | |
1223 | |
… | |
… | |
1221 | |
1241 | |
1222 | =cut |
1242 | =cut |
1223 | |
1243 | |
1224 | package AnyEvent; |
1244 | package AnyEvent; |
1225 | |
1245 | |
1226 | # basically a tuned-down version of common::sense |
1246 | BEGIN { |
1227 | sub common_sense { |
1247 | require "AnyEvent/constants.pl"; |
1228 | # from common:.sense 3.5 |
1248 | &AnyEvent::common_sense; |
1229 | local $^W; |
|
|
1230 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00"; |
|
|
1231 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
|
|
1232 | $^H |= 0x00000600; |
|
|
1233 | } |
1249 | } |
1234 | |
|
|
1235 | BEGIN { AnyEvent::common_sense } |
|
|
1236 | |
1250 | |
1237 | use Carp (); |
1251 | use Carp (); |
1238 | |
1252 | |
1239 | our $VERSION = '6.14'; |
1253 | our $VERSION = '7.08'; |
1240 | our $MODEL; |
1254 | our $MODEL; |
1241 | our @ISA; |
1255 | our @ISA; |
1242 | our @REGISTRY; |
1256 | our @REGISTRY; |
1243 | our $VERBOSE; |
1257 | our $VERBOSE; |
1244 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1258 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1245 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1259 | our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!) |
1246 | |
1260 | |
1247 | BEGIN { |
1261 | BEGIN { |
1248 | require "AnyEvent/constants.pl"; |
|
|
1249 | |
|
|
1250 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1262 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1251 | |
1263 | |
1252 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1264 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1253 | if ${^TAINT}; |
1265 | if ${^TAINT}; |
1254 | |
1266 | |
… | |
… | |
1355 | [Event:: => AnyEvent::Impl::Event::], # slow, stable |
1367 | [Event:: => AnyEvent::Impl::Event::], # slow, stable |
1356 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
1368 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
1357 | # everything below here should not be autoloaded |
1369 | # everything below here should not be autoloaded |
1358 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1370 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
1359 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1371 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
1372 | [UV:: => AnyEvent::Impl::UV::], # switched from libev, added back all bugs imaginable |
1360 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1373 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1361 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1374 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1362 | [Wx:: => AnyEvent::Impl::POE::], |
1375 | [Wx:: => AnyEvent::Impl::POE::], |
1363 | [Prima:: => AnyEvent::Impl::POE::], |
1376 | [Prima:: => AnyEvent::Impl::POE::], |
1364 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect |
1377 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect |
… | |
… | |
1396 | |
1409 | |
1397 | # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it |
1410 | # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it |
1398 | # the author knows about the problems and what it does to AnyEvent as a whole |
1411 | # the author knows about the problems and what it does to AnyEvent as a whole |
1399 | # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent |
1412 | # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent |
1400 | # anyway. |
1413 | # anyway. |
1401 | AnyEvent::log fatal => "AnyEvent: IO::Async::Loop::AnyEvent detected - that module is broken by\n" |
1414 | AnyEvent::log fatal => "IO::Async::Loop::AnyEvent detected - that module is broken by\n" |
1402 | . "design, abuses internals and breaks AnyEvent - will not continue." |
1415 | . "design, abuses internals and breaks AnyEvent - will not continue." |
1403 | if exists $INC{"IO/Async/Loop/AnyEvent.pm"}; |
1416 | if exists $INC{"IO/Async/Loop/AnyEvent.pm"}; |
1404 | |
1417 | |
1405 | local $!; # for good measure |
1418 | local $!; # for good measure |
1406 | local $SIG{__DIE__}; # we use eval |
1419 | local $SIG{__DIE__}; # we use eval |
… | |
… | |
1417 | |
1430 | |
1418 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1431 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1419 | my $model = $1; |
1432 | my $model = $1; |
1420 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1433 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1421 | if (eval "require $model") { |
1434 | if (eval "require $model") { |
1422 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1435 | AnyEvent::log 7 => "Loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."; |
1423 | $MODEL = $model; |
1436 | $MODEL = $model; |
1424 | } else { |
1437 | } else { |
1425 | AnyEvent::log 4 => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1438 | AnyEvent::log 4 => "Unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1426 | } |
1439 | } |
1427 | } |
1440 | } |
1428 | |
1441 | |
1429 | # check for already loaded models |
1442 | # check for already loaded models |
1430 | unless ($MODEL) { |
1443 | unless ($MODEL) { |
1431 | for (@REGISTRY, @models) { |
1444 | for (@REGISTRY, @models) { |
1432 | my ($package, $model) = @$_; |
1445 | my ($package, $model) = @$_; |
1433 | if (${"$package\::VERSION"} > 0) { |
1446 | if (${"$package\::VERSION"} > 0) { |
1434 | if (eval "require $model") { |
1447 | if (eval "require $model") { |
1435 | AnyEvent::log 7 => "autodetected model '$model', using it."; |
1448 | AnyEvent::log 7 => "Autodetected model '$model', using it."; |
1436 | $MODEL = $model; |
1449 | $MODEL = $model; |
1437 | last; |
1450 | last; |
1438 | } else { |
1451 | } else { |
1439 | AnyEvent::log 8 => "detected event loop $package, but cannot load '$model', skipping: $@"; |
1452 | AnyEvent::log 8 => "Detected event loop $package, but cannot load '$model', skipping: $@"; |
1440 | } |
1453 | } |
1441 | } |
1454 | } |
1442 | } |
1455 | } |
1443 | |
1456 | |
1444 | unless ($MODEL) { |
1457 | unless ($MODEL) { |
… | |
… | |
1448 | if ( |
1461 | if ( |
1449 | eval "require $package" |
1462 | eval "require $package" |
1450 | and ${"$package\::VERSION"} > 0 |
1463 | and ${"$package\::VERSION"} > 0 |
1451 | and eval "require $model" |
1464 | and eval "require $model" |
1452 | ) { |
1465 | ) { |
1453 | AnyEvent::log 7 => "autoloaded model '$model', using it."; |
1466 | AnyEvent::log 7 => "Autoloaded model '$model', using it."; |
1454 | $MODEL = $model; |
1467 | $MODEL = $model; |
1455 | last; |
1468 | last; |
1456 | } |
1469 | } |
1457 | } |
1470 | } |
1458 | |
1471 | |
1459 | $MODEL |
1472 | $MODEL |
1460 | or AnyEvent::log fatal => "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1473 | or AnyEvent::log fatal => "Backend autodetection failed - did you properly install AnyEvent?"; |
1461 | } |
1474 | } |
1462 | } |
1475 | } |
1463 | |
1476 | |
1464 | # free memory only needed for probing |
1477 | # free memory only needed for probing |
1465 | undef @models; |
1478 | undef @models; |
… | |
… | |
1553 | package AE; |
1566 | package AE; |
1554 | |
1567 | |
1555 | our $VERSION = $AnyEvent::VERSION; |
1568 | our $VERSION = $AnyEvent::VERSION; |
1556 | |
1569 | |
1557 | sub _reset() { |
1570 | sub _reset() { |
1558 | eval q{ |
1571 | eval q{ |
1559 | # fall back to the main API by default - backends and AnyEvent::Base |
1572 | # fall back to the main API by default - backends and AnyEvent::Base |
1560 | # implementations can overwrite these. |
1573 | # implementations can overwrite these. |
1561 | |
1574 | |
1562 | sub io($$$) { |
1575 | sub io($$$) { |
1563 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1576 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
… | |
… | |
1612 | # probe for availability of Time::HiRes |
1625 | # probe for availability of Time::HiRes |
1613 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1626 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1614 | *time = sub { Time::HiRes::time () }; |
1627 | *time = sub { Time::HiRes::time () }; |
1615 | *AE::time = \& Time::HiRes::time ; |
1628 | *AE::time = \& Time::HiRes::time ; |
1616 | *now = \&time; |
1629 | *now = \&time; |
1617 | AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy."; |
1630 | AnyEvent::log 8 => "using Time::HiRes for sub-second timing accuracy."; |
1618 | # if (eval "use POSIX (); (POSIX::times())... |
1631 | # if (eval "use POSIX (); (POSIX::times())... |
1619 | } else { |
1632 | } else { |
1620 | *time = sub { CORE::time }; |
1633 | *time = sub { CORE::time }; |
1621 | *AE::time = sub (){ CORE::time }; |
1634 | *AE::time = sub (){ CORE::time }; |
1622 | *now = \&time; |
1635 | *now = \&time; |
1623 | AnyEvent::log 3 => "using built-in time(), WARNING, no sub-second resolution!"; |
1636 | AnyEvent::log 3 => "Using built-in time(), no sub-second resolution!"; |
1624 | } |
1637 | } |
1625 | }; |
1638 | }; |
1626 | die if $@; |
1639 | die if $@; |
1627 | |
1640 | |
1628 | &time |
1641 | &time |
… | |
… | |
1722 | |
1735 | |
1723 | sub signal { |
1736 | sub signal { |
1724 | eval q{ # poor man's autoloading {} |
1737 | eval q{ # poor man's autoloading {} |
1725 | # probe for availability of Async::Interrupt |
1738 | # probe for availability of Async::Interrupt |
1726 | if (_have_async_interrupt) { |
1739 | if (_have_async_interrupt) { |
1727 | AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling."; |
1740 | AnyEvent::log 8 => "Using Async::Interrupt for race-free signal handling."; |
1728 | |
1741 | |
1729 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1742 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1730 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1743 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1731 | |
1744 | |
1732 | } else { |
1745 | } else { |
1733 | AnyEvent::log 8 => "using emulated perl signal handling with latency timer."; |
1746 | AnyEvent::log 8 => "Using emulated perl signal handling with latency timer."; |
1734 | |
1747 | |
1735 | if (AnyEvent::WIN32) { |
1748 | if (AnyEvent::WIN32) { |
1736 | require AnyEvent::Util; |
1749 | require AnyEvent::Util; |
1737 | |
1750 | |
1738 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1751 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
… | |
… | |
2178 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2191 | For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you |
2179 | could start your program like this: |
2192 | could start your program like this: |
2180 | |
2193 | |
2181 | PERL_ANYEVENT_MODEL=Perl perl ... |
2194 | PERL_ANYEVENT_MODEL=Perl perl ... |
2182 | |
2195 | |
|
|
2196 | =item C<PERL_ANYEVENT_IO_MODEL> |
|
|
2197 | |
|
|
2198 | The current file I/O model - see L<AnyEvent::IO> for more info. |
|
|
2199 | |
|
|
2200 | At the moment, only C<Perl> (small, pure-perl, synchronous) and |
|
|
2201 | C<IOAIO> (truly asynchronous) are supported. The default is C<IOAIO> if |
|
|
2202 | L<AnyEvent::AIO> can be loaded, otherwise it is C<Perl>. |
|
|
2203 | |
2183 | =item C<PERL_ANYEVENT_PROTOCOLS> |
2204 | =item C<PERL_ANYEVENT_PROTOCOLS> |
2184 | |
2205 | |
2185 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
2206 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
2186 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
2207 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
2187 | of auto probing). |
2208 | of auto probing). |
… | |
… | |
2191 | used, and preference will be given to protocols mentioned earlier in the |
2212 | used, and preference will be given to protocols mentioned earlier in the |
2192 | list. |
2213 | list. |
2193 | |
2214 | |
2194 | This variable can effectively be used for denial-of-service attacks |
2215 | This variable can effectively be used for denial-of-service attacks |
2195 | against local programs (e.g. when setuid), although the impact is likely |
2216 | against local programs (e.g. when setuid), although the impact is likely |
2196 | small, as the program has to handle conenction and other failures anyways. |
2217 | small, as the program has to handle connection and other failures anyways. |
2197 | |
2218 | |
2198 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
2219 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
2199 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
2220 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
2200 | - only support IPv4, never try to resolve or contact IPv6 |
2221 | - only support IPv4, never try to resolve or contact IPv6 |
2201 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
2222 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
… | |
… | |
2913 | This module is part of perl since release 5.008. It will be used when the |
2934 | This module is part of perl since release 5.008. It will be used when the |
2914 | chosen event library does not come with a timing source of its own. The |
2935 | chosen event library does not come with a timing source of its own. The |
2915 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2936 | pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to |
2916 | try to use a monotonic clock for timing stability. |
2937 | try to use a monotonic clock for timing stability. |
2917 | |
2938 | |
|
|
2939 | =item L<AnyEvent::AIO> (and L<IO::AIO>) |
|
|
2940 | |
|
|
2941 | The default implementation of L<AnyEvent::IO> is to do I/O synchronously, |
|
|
2942 | stopping programs while they access the disk, which is fine for a lot of |
|
|
2943 | programs. |
|
|
2944 | |
|
|
2945 | Installing AnyEvent::AIO (and its IO::AIO dependency) makes it switch to |
|
|
2946 | a true asynchronous implementation, so event processing can continue even |
|
|
2947 | while waiting for disk I/O. |
|
|
2948 | |
2918 | =back |
2949 | =back |
2919 | |
2950 | |
2920 | |
2951 | |
2921 | =head1 FORK |
2952 | =head1 FORK |
2922 | |
2953 | |
… | |
… | |
2933 | usually happens when the first AnyEvent watcher is created, or the library |
2964 | usually happens when the first AnyEvent watcher is created, or the library |
2934 | is loaded). |
2965 | is loaded). |
2935 | |
2966 | |
2936 | If you have to fork, you must either do so I<before> creating your first |
2967 | If you have to fork, you must either do so I<before> creating your first |
2937 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2968 | watcher OR you must not use AnyEvent at all in the child OR you must do |
2938 | something completely out of the scope of AnyEvent. |
2969 | something completely out of the scope of AnyEvent (see below). |
2939 | |
2970 | |
2940 | The problem of doing event processing in the parent I<and> the child |
2971 | The problem of doing event processing in the parent I<and> the child |
2941 | is much more complicated: even for backends that I<are> fork-aware or |
2972 | is much more complicated: even for backends that I<are> fork-aware or |
2942 | fork-safe, their behaviour is not usually what you want: fork clones all |
2973 | fork-safe, their behaviour is not usually what you want: fork clones all |
2943 | watchers, that means all timers, I/O watchers etc. are active in both |
2974 | watchers, that means all timers, I/O watchers etc. are active in both |
2944 | parent and child, which is almost never what you want. USing C<exec> |
2975 | parent and child, which is almost never what you want. Using C<exec> |
2945 | to start worker children from some kind of manage rprocess is usually |
2976 | to start worker children from some kind of manage prrocess is usually |
2946 | preferred, because it is much easier and cleaner, at the expense of having |
2977 | preferred, because it is much easier and cleaner, at the expense of having |
2947 | to have another binary. |
2978 | to have another binary. |
|
|
2979 | |
|
|
2980 | In addition to logical problems with fork, there are also implementation |
|
|
2981 | problems. For example, on POSIX systems, you cannot fork at all in Perl |
|
|
2982 | code if a thread (I am talking of pthreads here) was ever created in the |
|
|
2983 | process, and this is just the tip of the iceberg. In general, using fork |
|
|
2984 | from Perl is difficult, and attempting to use fork without an exec to |
|
|
2985 | implement some kind of parallel processing is almost certainly doomed. |
|
|
2986 | |
|
|
2987 | To safely fork and exec, you should use a module such as |
|
|
2988 | L<Proc::FastSpawn> that let's you safely fork and exec new processes. |
|
|
2989 | |
|
|
2990 | If you want to do multiprocessing using processes, you can |
|
|
2991 | look at the L<AnyEvent::Fork> module (and some related modules |
|
|
2992 | such as L<AnyEvent::Fork::RPC>, L<AnyEvent::Fork::Pool> and |
|
|
2993 | L<AnyEvent::Fork::Remote>). This module allows you to safely create |
|
|
2994 | subprocesses without any limitations - you can use X11 toolkits or |
|
|
2995 | AnyEvent in the children created by L<AnyEvent::Fork> safely and without |
|
|
2996 | any special precautions. |
2948 | |
2997 | |
2949 | |
2998 | |
2950 | =head1 SECURITY CONSIDERATIONS |
2999 | =head1 SECURITY CONSIDERATIONS |
2951 | |
3000 | |
2952 | AnyEvent can be forced to load any event model via |
3001 | AnyEvent can be forced to load any event model via |
… | |
… | |
3005 | L<AnyEvent::Impl::FLTK>. |
3054 | L<AnyEvent::Impl::FLTK>. |
3006 | |
3055 | |
3007 | Non-blocking handles, pipes, stream sockets, TCP clients and |
3056 | Non-blocking handles, pipes, stream sockets, TCP clients and |
3008 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
3057 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
3009 | |
3058 | |
|
|
3059 | Asynchronous File I/O: L<AnyEvent::IO>. |
|
|
3060 | |
3010 | Asynchronous DNS: L<AnyEvent::DNS>. |
3061 | Asynchronous DNS: L<AnyEvent::DNS>. |
3011 | |
3062 | |
3012 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
3063 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
3013 | |
3064 | |
3014 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
3065 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
… | |
… | |
3016 | |
3067 | |
3017 | |
3068 | |
3018 | =head1 AUTHOR |
3069 | =head1 AUTHOR |
3019 | |
3070 | |
3020 | Marc Lehmann <schmorp@schmorp.de> |
3071 | Marc Lehmann <schmorp@schmorp.de> |
3021 | http://home.schmorp.de/ |
3072 | http://anyevent.schmorp.de |
3022 | |
3073 | |
3023 | =cut |
3074 | =cut |
3024 | |
3075 | |
3025 | 1 |
3076 | 1 |
3026 | |
3077 | |