… | |
… | |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
9 | |
9 | |
10 | use AnyEvent; |
10 | use AnyEvent; |
11 | |
11 | |
|
|
12 | # if you prefer function calls, look at the L<AE> manpage for |
|
|
13 | # an alternative API. |
|
|
14 | |
12 | # file descriptor readable |
15 | # file handle or descriptor readable |
13 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
16 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
14 | |
17 | |
15 | # one-shot or repeating timers |
18 | # one-shot or repeating timers |
16 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
19 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
17 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
20 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
… | |
… | |
606 | eventually calls C<< -> send >>, and the "consumer side", which waits |
609 | eventually calls C<< -> send >>, and the "consumer side", which waits |
607 | for the send to occur. |
610 | for the send to occur. |
608 | |
611 | |
609 | Example: wait for a timer. |
612 | Example: wait for a timer. |
610 | |
613 | |
611 | # wait till the result is ready |
614 | # condition: "wait till the timer is fired" |
612 | my $result_ready = AnyEvent->condvar; |
615 | my $timer_fired = AnyEvent->condvar; |
613 | |
616 | |
614 | # do something such as adding a timer |
617 | # create the timer - we could wait for, say |
615 | # or socket watcher the calls $result_ready->send |
618 | # a handle becomign ready, or even an |
616 | # when the "result" is ready. |
619 | # AnyEvent::HTTP request to finish, but |
617 | # in this case, we simply use a timer: |
620 | # in this case, we simply use a timer: |
618 | my $w = AnyEvent->timer ( |
621 | my $w = AnyEvent->timer ( |
619 | after => 1, |
622 | after => 1, |
620 | cb => sub { $result_ready->send }, |
623 | cb => sub { $timer_fired->send }, |
621 | ); |
624 | ); |
622 | |
625 | |
623 | # this "blocks" (while handling events) till the callback |
626 | # this "blocks" (while handling events) till the callback |
624 | # calls ->send |
627 | # calls ->send |
625 | $result_ready->recv; |
628 | $timer_fired->recv; |
626 | |
629 | |
627 | Example: wait for a timer, but take advantage of the fact that condition |
630 | Example: wait for a timer, but take advantage of the fact that condition |
628 | variables are also callable directly. |
631 | variables are also callable directly. |
629 | |
632 | |
630 | my $done = AnyEvent->condvar; |
633 | my $done = AnyEvent->condvar; |
… | |
… | |
1156 | |
1159 | |
1157 | BEGIN { AnyEvent::common_sense } |
1160 | BEGIN { AnyEvent::common_sense } |
1158 | |
1161 | |
1159 | use Carp (); |
1162 | use Carp (); |
1160 | |
1163 | |
1161 | our $VERSION = '5.251'; |
1164 | our $VERSION = '5.26'; |
1162 | our $MODEL; |
1165 | our $MODEL; |
1163 | |
1166 | |
1164 | our $AUTOLOAD; |
1167 | our $AUTOLOAD; |
1165 | our @ISA; |
1168 | our @ISA; |
1166 | |
1169 | |
… | |
… | |
1169 | our $VERBOSE; |
1172 | our $VERBOSE; |
1170 | |
1173 | |
1171 | BEGIN { |
1174 | BEGIN { |
1172 | require "AnyEvent/constants.pl"; |
1175 | require "AnyEvent/constants.pl"; |
1173 | |
1176 | |
1174 | eval "sub TAINT (){" . (${^TAINT} *1) . "}"; |
1177 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1175 | |
1178 | |
1176 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1179 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1177 | if ${^TAINT}; |
1180 | if ${^TAINT}; |
1178 | |
1181 | |
1179 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1182 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
… | |
… | |
1222 | our @post_detect; |
1225 | our @post_detect; |
1223 | |
1226 | |
1224 | sub post_detect(&) { |
1227 | sub post_detect(&) { |
1225 | my ($cb) = @_; |
1228 | my ($cb) = @_; |
1226 | |
1229 | |
1227 | if ($MODEL) { |
|
|
1228 | $cb->(); |
|
|
1229 | |
|
|
1230 | undef |
|
|
1231 | } else { |
|
|
1232 | push @post_detect, $cb; |
1230 | push @post_detect, $cb; |
1233 | |
1231 | |
1234 | defined wantarray |
1232 | defined wantarray |
1235 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1233 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1236 | : () |
1234 | : () |
1237 | } |
|
|
1238 | } |
1235 | } |
1239 | |
1236 | |
1240 | sub AnyEvent::Util::postdetect::DESTROY { |
1237 | sub AnyEvent::Util::postdetect::DESTROY { |
1241 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1238 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1242 | } |
1239 | } |
… | |
… | |
1295 | @models = (); # free probe data |
1292 | @models = (); # free probe data |
1296 | |
1293 | |
1297 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1294 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1298 | unshift @ISA, $MODEL; |
1295 | unshift @ISA, $MODEL; |
1299 | |
1296 | |
|
|
1297 | # now nuke some methods that are overriden by the backend. |
|
|
1298 | # SUPER is not allowed. |
|
|
1299 | for (qw(time signal child idle)) { |
|
|
1300 | undef &{"AnyEvent::Base::$_"} |
|
|
1301 | if defined &{"$MODEL\::$_"}; |
|
|
1302 | } |
|
|
1303 | |
1300 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1304 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1301 | |
1305 | |
1302 | (shift @post_detect)->() while @post_detect; |
1306 | (shift @post_detect)->() while @post_detect; |
|
|
1307 | |
|
|
1308 | *post_detect = sub(&) { |
|
|
1309 | shift->(); |
|
|
1310 | |
|
|
1311 | undef |
|
|
1312 | }; |
1303 | |
1313 | |
1304 | $MODEL |
1314 | $MODEL |
1305 | } |
1315 | } |
1306 | |
1316 | |
1307 | sub AUTOLOAD { |
1317 | sub AUTOLOAD { |
… | |
… | |
1335 | |
1345 | |
1336 | =head1 SIMPLIFIED AE API |
1346 | =head1 SIMPLIFIED AE API |
1337 | |
1347 | |
1338 | Starting with version 5.0, AnyEvent officially supports a second, much |
1348 | Starting with version 5.0, AnyEvent officially supports a second, much |
1339 | simpler, API that is designed to reduce the calling, typing and memory |
1349 | simpler, API that is designed to reduce the calling, typing and memory |
1340 | overhead. |
1350 | overhead by using function call syntax and a fixed number of parameters. |
1341 | |
1351 | |
1342 | See the L<AE> manpage for details. |
1352 | See the L<AE> manpage for details. |
1343 | |
1353 | |
1344 | =cut |
1354 | =cut |
1345 | |
1355 | |
1346 | package AE; |
1356 | package AE; |
1347 | |
1357 | |
1348 | our $VERSION = $AnyEvent::VERSION; |
1358 | our $VERSION = $AnyEvent::VERSION; |
|
|
1359 | |
|
|
1360 | # fall back to the main API by default - backends and AnyEvent::Base |
|
|
1361 | # implementations can overwrite these. |
1349 | |
1362 | |
1350 | sub io($$$) { |
1363 | sub io($$$) { |
1351 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1364 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1352 | } |
1365 | } |
1353 | |
1366 | |
… | |
… | |
1385 | |
1398 | |
1386 | package AnyEvent::Base; |
1399 | package AnyEvent::Base; |
1387 | |
1400 | |
1388 | # default implementations for many methods |
1401 | # default implementations for many methods |
1389 | |
1402 | |
1390 | sub _time() { |
1403 | sub time { |
1391 | eval q{ # poor man's autoloading |
1404 | eval q{ # poor man's autoloading {} |
1392 | # probe for availability of Time::HiRes |
1405 | # probe for availability of Time::HiRes |
1393 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1406 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1394 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1407 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1395 | *_time = \&Time::HiRes::time; |
1408 | *AE::time = \&Time::HiRes::time; |
1396 | # if (eval "use POSIX (); (POSIX::times())... |
1409 | # if (eval "use POSIX (); (POSIX::times())... |
1397 | } else { |
1410 | } else { |
1398 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1411 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1399 | *_time = sub (){ time }; # epic fail |
1412 | *AE::time = sub (){ time }; # epic fail |
1400 | } |
1413 | } |
|
|
1414 | |
|
|
1415 | *time = sub { AE::time }; # different prototypes |
1401 | }; |
1416 | }; |
1402 | die if $@; |
1417 | die if $@; |
1403 | |
1418 | |
1404 | &_time |
1419 | &time |
1405 | } |
1420 | } |
1406 | |
1421 | |
1407 | sub time { _time } |
1422 | *now = \&time; |
1408 | sub now { _time } |
1423 | |
1409 | sub now_update { } |
1424 | sub now_update { } |
1410 | |
1425 | |
1411 | # default implementation for ->condvar |
1426 | # default implementation for ->condvar |
1412 | |
1427 | |
1413 | sub condvar { |
1428 | sub condvar { |
|
|
1429 | eval q{ # poor man's autoloading {} |
|
|
1430 | *condvar = sub { |
1414 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1431 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
|
|
1432 | }; |
|
|
1433 | |
|
|
1434 | *AE::cv = sub (;&) { |
|
|
1435 | bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar" |
|
|
1436 | }; |
|
|
1437 | }; |
|
|
1438 | die if $@; |
|
|
1439 | |
|
|
1440 | &condvar |
1415 | } |
1441 | } |
1416 | |
1442 | |
1417 | # default implementation for ->signal |
1443 | # default implementation for ->signal |
1418 | |
1444 | |
1419 | our $HAVE_ASYNC_INTERRUPT; |
1445 | our $HAVE_ASYNC_INTERRUPT; |
… | |
… | |
1449 | undef $SIG_TW |
1475 | undef $SIG_TW |
1450 | unless --$SIG_COUNT; |
1476 | unless --$SIG_COUNT; |
1451 | } |
1477 | } |
1452 | |
1478 | |
1453 | our $_sig_name_init; $_sig_name_init = sub { |
1479 | our $_sig_name_init; $_sig_name_init = sub { |
1454 | eval q{ # poor man's autoloading |
1480 | eval q{ # poor man's autoloading {} |
1455 | undef $_sig_name_init; |
1481 | undef $_sig_name_init; |
1456 | |
1482 | |
1457 | if (_have_async_interrupt) { |
1483 | if (_have_async_interrupt) { |
1458 | *sig2num = \&Async::Interrupt::sig2num; |
1484 | *sig2num = \&Async::Interrupt::sig2num; |
1459 | *sig2name = \&Async::Interrupt::sig2name; |
1485 | *sig2name = \&Async::Interrupt::sig2name; |
… | |
… | |
1513 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1539 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1514 | |
1540 | |
1515 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1541 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1516 | } |
1542 | } |
1517 | |
1543 | |
1518 | *signal = sub { |
1544 | *signal = $HAVE_ASYNC_INTERRUPT |
|
|
1545 | ? sub { |
1519 | my (undef, %arg) = @_; |
1546 | my (undef, %arg) = @_; |
1520 | |
1547 | |
1521 | my $signal = uc $arg{signal} |
|
|
1522 | or Carp::croak "required option 'signal' is missing"; |
|
|
1523 | |
|
|
1524 | if ($HAVE_ASYNC_INTERRUPT) { |
|
|
1525 | # async::interrupt |
1548 | # async::interrupt |
1526 | |
|
|
1527 | $signal = sig2num $signal; |
1549 | my $signal = sig2num $arg{signal}; |
1528 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1550 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1529 | |
1551 | |
1530 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1552 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1531 | cb => sub { undef $SIG_EV{$signal} }, |
1553 | cb => sub { undef $SIG_EV{$signal} }, |
1532 | signal => $signal, |
1554 | signal => $signal, |
1533 | pipe => [$SIGPIPE_R->filenos], |
1555 | pipe => [$SIGPIPE_R->filenos], |
1534 | pipe_autodrain => 0, |
1556 | pipe_autodrain => 0, |
1535 | ; |
1557 | ; |
1536 | |
1558 | |
1537 | } else { |
1559 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1560 | } |
|
|
1561 | : sub { |
|
|
1562 | my (undef, %arg) = @_; |
|
|
1563 | |
1538 | # pure perl |
1564 | # pure perl |
1539 | |
|
|
1540 | # AE::Util has been loaded in signal |
|
|
1541 | $signal = sig2name $signal; |
1565 | my $signal = sig2name $arg{signal}; |
1542 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1566 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1543 | |
1567 | |
1544 | $SIG{$signal} ||= sub { |
1568 | $SIG{$signal} ||= sub { |
1545 | local $!; |
1569 | local $!; |
1546 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1570 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1547 | undef $SIG_EV{$signal}; |
1571 | undef $SIG_EV{$signal}; |
1548 | }; |
1572 | }; |
1549 | |
1573 | |
1550 | # can't do signal processing without introducing races in pure perl, |
1574 | # can't do signal processing without introducing races in pure perl, |
1551 | # so limit the signal latency. |
1575 | # so limit the signal latency. |
1552 | _sig_add; |
1576 | _sig_add; |
1553 | } |
|
|
1554 | |
1577 | |
1555 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1578 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1579 | } |
1556 | }; |
1580 | ; |
1557 | |
1581 | |
1558 | *AnyEvent::Base::signal::DESTROY = sub { |
1582 | *AnyEvent::Base::signal::DESTROY = sub { |
1559 | my ($signal, $cb) = @{$_[0]}; |
1583 | my ($signal, $cb) = @{$_[0]}; |
1560 | |
1584 | |
1561 | _sig_del; |
1585 | _sig_del; |
… | |
… | |
2057 | |
2081 | |
2058 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2082 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2059 | that occurred during request processing. The C<result> method detects |
2083 | that occurred during request processing. The C<result> method detects |
2060 | whether an exception as thrown (it is stored inside the $txn object) |
2084 | whether an exception as thrown (it is stored inside the $txn object) |
2061 | and just throws the exception, which means connection errors and other |
2085 | and just throws the exception, which means connection errors and other |
2062 | problems get reported tot he code that tries to use the result, not in a |
2086 | problems get reported to the code that tries to use the result, not in a |
2063 | random callback. |
2087 | random callback. |
2064 | |
2088 | |
2065 | All of this enables the following usage styles: |
2089 | All of this enables the following usage styles: |
2066 | |
2090 | |
2067 | 1. Blocking: |
2091 | 1. Blocking: |