… | |
… | |
1049 | It should use C<postpone>: |
1049 | It should use C<postpone>: |
1050 | |
1050 | |
1051 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
1051 | AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later |
1052 | if $some_error_condition; |
1052 | if $some_error_condition; |
1053 | |
1053 | |
|
|
1054 | =item AnyEvent::log $level, $msg[, @args] |
|
|
1055 | |
|
|
1056 | Log the given C<$msg> at the given C<$level>. |
|
|
1057 | |
|
|
1058 | Loads AnyEvent::Log on first use and calls C<AnyEvent::Log::log> - |
|
|
1059 | consequently, look at the L<AnyEvent::Log> documentation for details. |
|
|
1060 | |
1054 | =back |
1061 | =back |
1055 | |
1062 | |
1056 | =head1 WHAT TO DO IN A MODULE |
1063 | =head1 WHAT TO DO IN A MODULE |
1057 | |
1064 | |
1058 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
1065 | As a module author, you should C<use AnyEvent> and call AnyEvent methods |
… | |
… | |
1224 | |
1231 | |
1225 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1232 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1226 | if ${^TAINT}; |
1233 | if ${^TAINT}; |
1227 | |
1234 | |
1228 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1235 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1229 | |
|
|
1230 | } |
1236 | } |
1231 | |
1237 | |
1232 | our $MAX_SIGNAL_LATENCY = 10; |
1238 | our $MAX_SIGNAL_LATENCY = 10; |
1233 | |
1239 | |
1234 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
1240 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
… | |
… | |
1270 | push @POSTPONE, shift; |
1276 | push @POSTPONE, shift; |
1271 | |
1277 | |
1272 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
1278 | $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); |
1273 | |
1279 | |
1274 | () |
1280 | () |
|
|
1281 | } |
|
|
1282 | |
|
|
1283 | sub log($$;@) { |
|
|
1284 | require AnyEvent::Log; |
|
|
1285 | # AnyEvent::Log overwrites this function |
|
|
1286 | goto &log; |
1275 | } |
1287 | } |
1276 | |
1288 | |
1277 | our @models = ( |
1289 | our @models = ( |
1278 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1290 | [EV:: => AnyEvent::Impl::EV:: , 1], |
1279 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
1291 | [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], |
… | |
… | |
1295 | ); |
1307 | ); |
1296 | |
1308 | |
1297 | our @isa_hook; |
1309 | our @isa_hook; |
1298 | |
1310 | |
1299 | sub _isa_set { |
1311 | sub _isa_set { |
1300 | my @pkg = ("AnyEvent", (grep defined, map $_->[0], @isa_hook), $MODEL); |
1312 | my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL); |
1301 | |
1313 | |
1302 | @{"$pkg[$_-1]::ISA"} = $pkg[$_] |
1314 | @{"$pkg[$_-1]::ISA"} = $pkg[$_] |
1303 | for 1 .. $#pkg; |
1315 | for 1 .. $#pkg; |
1304 | |
1316 | |
1305 | grep $_->[1], @isa_hook |
1317 | grep $_ && $_->[1], @isa_hook |
1306 | and AE::_reset (); |
1318 | and AE::_reset (); |
1307 | } |
1319 | } |
1308 | |
1320 | |
1309 | # used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy |
1321 | # used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy |
1310 | sub _isa_hook($$;$) { |
1322 | sub _isa_hook($$;$) { |
1311 | my ($i, $pkg, $reset_ae) = @_; |
1323 | my ($i, $pkg, $reset_ae) = @_; |
1312 | |
1324 | |
1313 | $isa_hook[$i] = [$pkg, $reset_ae]; |
1325 | $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef; |
1314 | |
1326 | |
1315 | _isa_set; |
1327 | _isa_set; |
1316 | } |
1328 | } |
1317 | |
1329 | |
1318 | # all autoloaded methods reserve the complete glob, not just the method slot. |
1330 | # all autoloaded methods reserve the complete glob, not just the method slot. |
1319 | # due to bugs in perls method cache implementation. |
1331 | # due to bugs in perls method cache implementation. |
1320 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1332 | our @methods = qw(io timer time now now_update signal child idle condvar); |
1321 | |
1333 | |
1322 | sub detect() { |
1334 | sub detect() { |
|
|
1335 | return $MODEL if $MODEL; # some programs keep references to detect |
|
|
1336 | |
1323 | local $!; # for good measure |
1337 | local $!; # for good measure |
1324 | local $SIG{__DIE__}; # we use eval |
1338 | local $SIG{__DIE__}; # we use eval |
1325 | |
1339 | |
1326 | # free some memory |
1340 | # free some memory |
1327 | *detect = sub () { $MODEL }; |
1341 | *detect = sub () { $MODEL }; |
… | |
… | |
1336 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1350 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { |
1337 | my $model = $1; |
1351 | my $model = $1; |
1338 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1352 | $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; |
1339 | if (eval "require $model") { |
1353 | if (eval "require $model") { |
1340 | $MODEL = $model; |
1354 | $MODEL = $model; |
1341 | warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; |
1355 | AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it." |
|
|
1356 | if $VERBOSE >= 7; |
1342 | } else { |
1357 | } else { |
1343 | warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; |
1358 | AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@"; |
1344 | } |
1359 | } |
1345 | } |
1360 | } |
1346 | |
1361 | |
1347 | # check for already loaded models |
1362 | # check for already loaded models |
1348 | unless ($MODEL) { |
1363 | unless ($MODEL) { |
1349 | for (@REGISTRY, @models) { |
1364 | for (@REGISTRY, @models) { |
1350 | my ($package, $model) = @$_; |
1365 | my ($package, $model) = @$_; |
1351 | if (${"$package\::VERSION"} > 0) { |
1366 | if (${"$package\::VERSION"} > 0) { |
1352 | if (eval "require $model") { |
1367 | if (eval "require $model") { |
1353 | $MODEL = $model; |
1368 | $MODEL = $model; |
1354 | warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; |
1369 | AnyEvent::log 7 => "autodetected model '$model', using it." |
|
|
1370 | if $VERBOSE >= 7; |
1355 | last; |
1371 | last; |
1356 | } |
1372 | } |
1357 | } |
1373 | } |
1358 | } |
1374 | } |
1359 | |
1375 | |
… | |
… | |
1366 | and eval "require $package" |
1382 | and eval "require $package" |
1367 | and ${"$package\::VERSION"} > 0 |
1383 | and ${"$package\::VERSION"} > 0 |
1368 | and eval "require $model" |
1384 | and eval "require $model" |
1369 | ) { |
1385 | ) { |
1370 | $MODEL = $model; |
1386 | $MODEL = $model; |
1371 | warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; |
1387 | AnyEvent::log 7 => "autoloaded model '$model', using it." |
|
|
1388 | if $VERBOSE >= 7; |
1372 | last; |
1389 | last; |
1373 | } |
1390 | } |
1374 | } |
1391 | } |
1375 | |
1392 | |
1376 | $MODEL |
1393 | $MODEL |
1377 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1394 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?"; |
1378 | } |
1395 | } |
1379 | } |
1396 | } |
1380 | |
1397 | |
1381 | # free memory only needed for probing |
1398 | # free memory only needed for probing |
1382 | undef @models; |
1399 | undef @models; |
… | |
… | |
1506 | sub time() { |
1523 | sub time() { |
1507 | AnyEvent->time |
1524 | AnyEvent->time |
1508 | } |
1525 | } |
1509 | |
1526 | |
1510 | *postpone = \&AnyEvent::postpone; |
1527 | *postpone = \&AnyEvent::postpone; |
|
|
1528 | *log = \&AnyEvent::log; |
1511 | }; |
1529 | }; |
1512 | die if $@; |
1530 | die if $@; |
1513 | } |
1531 | } |
1514 | |
1532 | |
1515 | BEGIN { _reset } |
1533 | BEGIN { _reset } |
… | |
… | |
1520 | |
1538 | |
1521 | sub time { |
1539 | sub time { |
1522 | eval q{ # poor man's autoloading {} |
1540 | eval q{ # poor man's autoloading {} |
1523 | # probe for availability of Time::HiRes |
1541 | # probe for availability of Time::HiRes |
1524 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1542 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1525 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1543 | AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy." |
|
|
1544 | if $AnyEvent::VERBOSE >= 8; |
1526 | *time = sub { Time::HiRes::time () }; |
1545 | *time = sub { Time::HiRes::time () }; |
1527 | *AE::time = \& Time::HiRes::time ; |
1546 | *AE::time = \& Time::HiRes::time ; |
1528 | # if (eval "use POSIX (); (POSIX::times())... |
1547 | # if (eval "use POSIX (); (POSIX::times())... |
1529 | } else { |
1548 | } else { |
1530 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1549 | AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!"; |
1531 | *time = sub { CORE::time }; |
1550 | *time = sub { CORE::time }; |
1532 | *AE::time = sub (){ CORE::time }; |
1551 | *AE::time = sub (){ CORE::time }; |
1533 | } |
1552 | } |
1534 | |
1553 | |
1535 | *now = \&time; |
1554 | *now = \&time; |
… | |
… | |
1633 | |
1652 | |
1634 | sub signal { |
1653 | sub signal { |
1635 | eval q{ # poor man's autoloading {} |
1654 | eval q{ # poor man's autoloading {} |
1636 | # probe for availability of Async::Interrupt |
1655 | # probe for availability of Async::Interrupt |
1637 | if (_have_async_interrupt) { |
1656 | if (_have_async_interrupt) { |
1638 | warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; |
1657 | AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling." |
|
|
1658 | if $AnyEvent::VERBOSE >= 8; |
1639 | |
1659 | |
1640 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1660 | $SIGPIPE_R = new Async::Interrupt::EventPipe; |
1641 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1661 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1642 | |
1662 | |
1643 | } else { |
1663 | } else { |
1644 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1664 | AnyEvent::log 8 => "using emulated perl signal handling with latency timer." |
|
|
1665 | if $AnyEvent::VERBOSE >= 8; |
1645 | |
1666 | |
1646 | if (AnyEvent::WIN32) { |
1667 | if (AnyEvent::WIN32) { |
1647 | require AnyEvent::Util; |
1668 | require AnyEvent::Util; |
1648 | |
1669 | |
1649 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1670 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
… | |
… | |
1958 | |
1979 | |
1959 | By default, AnyEvent will be completely silent except in fatal |
1980 | By default, AnyEvent will be completely silent except in fatal |
1960 | conditions. You can set this environment variable to make AnyEvent more |
1981 | conditions. You can set this environment variable to make AnyEvent more |
1961 | talkative. |
1982 | talkative. |
1962 | |
1983 | |
1963 | When set to C<1> or higher, causes AnyEvent to warn about unexpected |
1984 | When set to C<5> or higher, causes AnyEvent to warn about unexpected |
1964 | conditions, such as not being able to load the event model specified by |
1985 | conditions, such as not being able to load the event model specified by |
1965 | C<PERL_ANYEVENT_MODEL>. |
1986 | C<PERL_ANYEVENT_MODEL>. |
1966 | |
1987 | |
1967 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1988 | When set to C<7> or higher, cause AnyEvent to report to STDERR which event |
1968 | model it chooses. |
1989 | model it chooses. |
1969 | |
1990 | |
1970 | When set to C<8> or higher, then AnyEvent will report extra information on |
1991 | When set to C<8> or higher, then AnyEvent will report extra information on |
1971 | which optional modules it loads and how it implements certain features. |
1992 | which optional modules it loads and how it implements certain features. |
1972 | |
1993 | |
… | |
… | |
1995 | This takes place when the first watcher is created. |
2016 | This takes place when the first watcher is created. |
1996 | |
2017 | |
1997 | For example, to bind a debug shell on a unix domain socket in |
2018 | For example, to bind a debug shell on a unix domain socket in |
1998 | F<< /tmp/debug<pid>.sock >>, you could use this: |
2019 | F<< /tmp/debug<pid>.sock >>, you could use this: |
1999 | |
2020 | |
2000 | PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug\$\$.sock perlprog |
2021 | PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog |
2001 | |
2022 | |
2002 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
2023 | Note that creating sockets in F</tmp> is very unsafe on multiuser |
2003 | systems. |
2024 | systems. |
2004 | |
2025 | |
2005 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
2026 | =item C<PERL_ANYEVENT_DEBUG_WRAP> |
… | |
… | |
2420 | (even when used without AnyEvent), but most event loops have acceptable |
2441 | (even when used without AnyEvent), but most event loops have acceptable |
2421 | performance with or without AnyEvent. |
2442 | performance with or without AnyEvent. |
2422 | |
2443 | |
2423 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2444 | =item * The overhead AnyEvent adds is usually much smaller than the overhead of |
2424 | the actual event loop, only with extremely fast event loops such as EV |
2445 | the actual event loop, only with extremely fast event loops such as EV |
2425 | adds AnyEvent significant overhead. |
2446 | does AnyEvent add significant overhead. |
2426 | |
2447 | |
2427 | =item * You should avoid POE like the plague if you want performance or |
2448 | =item * You should avoid POE like the plague if you want performance or |
2428 | reasonable memory usage. |
2449 | reasonable memory usage. |
2429 | |
2450 | |
2430 | =back |
2451 | =back |
… | |
… | |
2805 | |
2826 | |
2806 | Tutorial/Introduction: L<AnyEvent::Intro>. |
2827 | Tutorial/Introduction: L<AnyEvent::Intro>. |
2807 | |
2828 | |
2808 | FAQ: L<AnyEvent::FAQ>. |
2829 | FAQ: L<AnyEvent::FAQ>. |
2809 | |
2830 | |
2810 | Utility functions: L<AnyEvent::Util>. |
2831 | Utility functions: L<AnyEvent::Util> (misc. grab-bag), L<AnyEvent::Log> |
|
|
2832 | (simply logging). |
2811 | |
2833 | |
|
|
2834 | Development/Debugging: L<AnyEvent::Strict> (stricter checking), |
|
|
2835 | L<AnyEvent::Debug> (interactive shell, watcher tracing). |
|
|
2836 | |
2812 | Event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
2837 | Supported event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, |
2813 | L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2838 | L<Glib::EV>, L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, |
|
|
2839 | L<Qt>, L<POE>, L<FLTK>. |
2814 | |
2840 | |
2815 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2841 | Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, |
2816 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2842 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, |
2817 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2843 | L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, |
2818 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. |
2844 | L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>, |
|
|
2845 | L<AnyEvent::Impl::FLTK>. |
2819 | |
2846 | |
2820 | Non-blocking file handles, sockets, TCP clients and |
2847 | Non-blocking handles, pipes, stream sockets, TCP clients and |
2821 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2848 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2822 | |
2849 | |
2823 | Asynchronous DNS: L<AnyEvent::DNS>. |
2850 | Asynchronous DNS: L<AnyEvent::DNS>. |
2824 | |
2851 | |
2825 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
2852 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |