… | |
… | |
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 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 => ... |
… | |
… | |
553 | will actively watch for new events and call your callbacks. |
556 | will actively watch for new events and call your callbacks. |
554 | |
557 | |
555 | AnyEvent is slightly different: it expects somebody else to run the event |
558 | AnyEvent is slightly different: it expects somebody else to run the event |
556 | loop and will only block when necessary (usually when told by the user). |
559 | loop and will only block when necessary (usually when told by the user). |
557 | |
560 | |
558 | The instrument to do that is called a "condition variable", so called |
561 | The tool to do that is called a "condition variable", so called because |
559 | because they represent a condition that must become true. |
562 | they represent a condition that must become true. |
560 | |
563 | |
561 | Now is probably a good time to look at the examples further below. |
564 | Now is probably a good time to look at the examples further below. |
562 | |
565 | |
563 | Condition variables can be created by calling the C<< AnyEvent->condvar |
566 | Condition variables can be created by calling the C<< AnyEvent->condvar |
564 | >> method, usually without arguments. The only argument pair allowed is |
567 | >> method, usually without arguments. The only argument pair allowed is |
… | |
… | |
569 | After creation, the condition variable is "false" until it becomes "true" |
572 | After creation, the condition variable is "false" until it becomes "true" |
570 | by calling the C<send> method (or calling the condition variable as if it |
573 | by calling the C<send> method (or calling the condition variable as if it |
571 | were a callback, read about the caveats in the description for the C<< |
574 | were a callback, read about the caveats in the description for the C<< |
572 | ->send >> method). |
575 | ->send >> method). |
573 | |
576 | |
574 | Condition variables are similar to callbacks, except that you can |
577 | Since condition variables are the most complex part of the AnyEvent API, here are |
575 | optionally wait for them. They can also be called merge points - points |
578 | some different mental models of what they are - pick the ones you can connect to: |
576 | in time where multiple outstanding events have been processed. And yet |
579 | |
577 | another way to call them is transactions - each condition variable can be |
580 | =over 4 |
578 | used to represent a transaction, which finishes at some point and delivers |
581 | |
579 | a result. And yet some people know them as "futures" - a promise to |
582 | =item * Condition variables are like callbacks - you can call them (and pass them instead |
580 | compute/deliver something that you can wait for. |
583 | of callbacks). Unlike callbacks however, you can also wait for them to be called. |
|
|
584 | |
|
|
585 | =item * Condition variables are signals - one side can emit or send them, |
|
|
586 | the other side can wait for them, or install a handler that is called when |
|
|
587 | the signal fires. |
|
|
588 | |
|
|
589 | =item * Condition variables are like "Merge Points" - points in your program |
|
|
590 | where you merge multiple independent results/control flows into one. |
|
|
591 | |
|
|
592 | =item * Condition variables represent a transaction - function that start |
|
|
593 | some kind of transaction can return them, leaving the caller the choice |
|
|
594 | between waiting in a blocking fashion, or setting a callback. |
|
|
595 | |
|
|
596 | =item * Condition variables represent future values, or promises to deliver |
|
|
597 | some result, long before the result is available. |
|
|
598 | |
|
|
599 | =back |
581 | |
600 | |
582 | Condition variables are very useful to signal that something has finished, |
601 | Condition variables are very useful to signal that something has finished, |
583 | for example, if you write a module that does asynchronous http requests, |
602 | for example, if you write a module that does asynchronous http requests, |
584 | then a condition variable would be the ideal candidate to signal the |
603 | then a condition variable would be the ideal candidate to signal the |
585 | availability of results. The user can either act when the callback is |
604 | availability of results. The user can either act when the callback is |
… | |
… | |
606 | eventually calls C<< -> send >>, and the "consumer side", which waits |
625 | eventually calls C<< -> send >>, and the "consumer side", which waits |
607 | for the send to occur. |
626 | for the send to occur. |
608 | |
627 | |
609 | Example: wait for a timer. |
628 | Example: wait for a timer. |
610 | |
629 | |
611 | # wait till the result is ready |
630 | # condition: "wait till the timer is fired" |
612 | my $result_ready = AnyEvent->condvar; |
631 | my $timer_fired = AnyEvent->condvar; |
613 | |
632 | |
614 | # do something such as adding a timer |
633 | # create the timer - we could wait for, say |
615 | # or socket watcher the calls $result_ready->send |
634 | # a handle becomign ready, or even an |
616 | # when the "result" is ready. |
635 | # AnyEvent::HTTP request to finish, but |
617 | # in this case, we simply use a timer: |
636 | # in this case, we simply use a timer: |
618 | my $w = AnyEvent->timer ( |
637 | my $w = AnyEvent->timer ( |
619 | after => 1, |
638 | after => 1, |
620 | cb => sub { $result_ready->send }, |
639 | cb => sub { $timer_fired->send }, |
621 | ); |
640 | ); |
622 | |
641 | |
623 | # this "blocks" (while handling events) till the callback |
642 | # this "blocks" (while handling events) till the callback |
624 | # calls ->send |
643 | # calls ->send |
625 | $result_ready->recv; |
644 | $timer_fired->recv; |
626 | |
645 | |
627 | Example: wait for a timer, but take advantage of the fact that condition |
646 | Example: wait for a timer, but take advantage of the fact that condition |
628 | variables are also callable directly. |
647 | variables are also callable directly. |
629 | |
648 | |
630 | my $done = AnyEvent->condvar; |
649 | my $done = AnyEvent->condvar; |
… | |
… | |
1054 | =head1 OTHER MODULES |
1073 | =head1 OTHER MODULES |
1055 | |
1074 | |
1056 | The following is a non-exhaustive list of additional modules that use |
1075 | The following is a non-exhaustive list of additional modules that use |
1057 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1076 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1058 | modules and other event loops in the same program. Some of the modules |
1077 | modules and other event loops in the same program. Some of the modules |
1059 | come with AnyEvent, most are available via CPAN. |
1078 | come as part of AnyEvent, the others are available via CPAN. |
1060 | |
1079 | |
1061 | =over 4 |
1080 | =over 4 |
1062 | |
1081 | |
1063 | =item L<AnyEvent::Util> |
1082 | =item L<AnyEvent::Util> |
1064 | |
1083 | |
… | |
… | |
1079 | |
1098 | |
1080 | =item L<AnyEvent::DNS> |
1099 | =item L<AnyEvent::DNS> |
1081 | |
1100 | |
1082 | Provides rich asynchronous DNS resolver capabilities. |
1101 | Provides rich asynchronous DNS resolver capabilities. |
1083 | |
1102 | |
|
|
1103 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
|
|
1104 | |
|
|
1105 | Implement event-based interfaces to the protocols of the same name (for |
|
|
1106 | the curious, IGS is the International Go Server and FCP is the Freenet |
|
|
1107 | Client Protocol). |
|
|
1108 | |
|
|
1109 | =item L<AnyEvent::Handle::UDP> |
|
|
1110 | |
|
|
1111 | Here be danger! |
|
|
1112 | |
|
|
1113 | As Pauli would put it, "Not only is it not right, it's not even wrong!" - |
|
|
1114 | there are so many things wrong with AnyEvent::Handle::UDP, most notably |
|
|
1115 | it's use of a stream-based API with a protocol that isn't streamable, that |
|
|
1116 | the only way to improve it is to delete it. |
|
|
1117 | |
|
|
1118 | It features data corruption (but typically only under load) and general |
|
|
1119 | confusion. On top, the author is not only clueless about UDP but also |
|
|
1120 | fact-resistant - some gems of his understanding: "connect doesn't work |
|
|
1121 | with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not |
|
|
1122 | packets", "I don't need to implement proper error checking as UDP doesn't |
|
|
1123 | support error checking" and so on - he doesn't even understand what's |
|
|
1124 | wrong with his module when it is explained to him. |
|
|
1125 | |
1084 | =item L<AnyEvent::HTTP> |
1126 | =item L<AnyEvent::DBI> |
1085 | |
1127 | |
1086 | A simple-to-use HTTP library that is capable of making a lot of concurrent |
1128 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1087 | HTTP requests. |
1129 | notifying you in an event-bnased way when the operation is finished. |
|
|
1130 | |
|
|
1131 | =item L<AnyEvent::AIO> |
|
|
1132 | |
|
|
1133 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
|
|
1134 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
|
|
1135 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
|
|
1136 | file I/O, and much more. |
1088 | |
1137 | |
1089 | =item L<AnyEvent::HTTPD> |
1138 | =item L<AnyEvent::HTTPD> |
1090 | |
1139 | |
1091 | Provides a simple web application server framework. |
1140 | A simple embedded webserver. |
1092 | |
1141 | |
1093 | =item L<AnyEvent::FastPing> |
1142 | =item L<AnyEvent::FastPing> |
1094 | |
1143 | |
1095 | The fastest ping in the west. |
1144 | The fastest ping in the west. |
1096 | |
|
|
1097 | =item L<AnyEvent::DBI> |
|
|
1098 | |
|
|
1099 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
1100 | |
|
|
1101 | =item L<AnyEvent::AIO> |
|
|
1102 | |
|
|
1103 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
1104 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
1105 | together. |
|
|
1106 | |
|
|
1107 | =item L<AnyEvent::BDB> |
|
|
1108 | |
|
|
1109 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
1110 | L<BDB> and AnyEvent together. |
|
|
1111 | |
|
|
1112 | =item L<AnyEvent::GPSD> |
|
|
1113 | |
|
|
1114 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
1115 | |
|
|
1116 | =item L<AnyEvent::IRC> |
|
|
1117 | |
|
|
1118 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
|
|
1119 | |
|
|
1120 | =item L<AnyEvent::XMPP> |
|
|
1121 | |
|
|
1122 | AnyEvent based XMPP (Jabber protocol) module family (replacing the older |
|
|
1123 | Net::XMPP2>. |
|
|
1124 | |
|
|
1125 | =item L<AnyEvent::IGS> |
|
|
1126 | |
|
|
1127 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
1128 | L<App::IGS>). |
|
|
1129 | |
|
|
1130 | =item L<Net::FCP> |
|
|
1131 | |
|
|
1132 | AnyEvent-based implementation of the Freenet Client Protocol, birthplace |
|
|
1133 | of AnyEvent. |
|
|
1134 | |
|
|
1135 | =item L<Event::ExecFlow> |
|
|
1136 | |
|
|
1137 | High level API for event-based execution flow control. |
|
|
1138 | |
1145 | |
1139 | =item L<Coro> |
1146 | =item L<Coro> |
1140 | |
1147 | |
1141 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1148 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1142 | |
1149 | |
… | |
… | |
1156 | |
1163 | |
1157 | BEGIN { AnyEvent::common_sense } |
1164 | BEGIN { AnyEvent::common_sense } |
1158 | |
1165 | |
1159 | use Carp (); |
1166 | use Carp (); |
1160 | |
1167 | |
1161 | our $VERSION = '5.24'; |
1168 | our $VERSION = '5.27'; |
1162 | our $MODEL; |
1169 | our $MODEL; |
1163 | |
1170 | |
1164 | our $AUTOLOAD; |
1171 | our $AUTOLOAD; |
1165 | our @ISA; |
1172 | our @ISA; |
1166 | |
1173 | |
1167 | our @REGISTRY; |
1174 | our @REGISTRY; |
1168 | |
1175 | |
1169 | our $VERBOSE; |
1176 | our $VERBOSE; |
1170 | |
1177 | |
1171 | BEGIN { |
1178 | BEGIN { |
1172 | eval "sub CYGWIN(){" . (($^O =~ /cygwin/i) *1) . "}"; |
1179 | require "AnyEvent/constants.pl"; |
1173 | eval "sub WIN32 (){" . (($^O =~ /mswin32/i)*1) . "}"; |
1180 | |
1174 | eval "sub TAINT (){" . (${^TAINT} *1) . "}"; |
1181 | eval "sub TAINT (){" . (${^TAINT}*1) . "}"; |
1175 | |
1182 | |
1176 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1183 | delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} |
1177 | if ${^TAINT}; |
1184 | if ${^TAINT}; |
1178 | |
1185 | |
1179 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
1186 | $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
… | |
… | |
1222 | our @post_detect; |
1229 | our @post_detect; |
1223 | |
1230 | |
1224 | sub post_detect(&) { |
1231 | sub post_detect(&) { |
1225 | my ($cb) = @_; |
1232 | my ($cb) = @_; |
1226 | |
1233 | |
1227 | if ($MODEL) { |
|
|
1228 | $cb->(); |
|
|
1229 | |
|
|
1230 | undef |
|
|
1231 | } else { |
|
|
1232 | push @post_detect, $cb; |
1234 | push @post_detect, $cb; |
1233 | |
1235 | |
1234 | defined wantarray |
1236 | defined wantarray |
1235 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1237 | ? bless \$cb, "AnyEvent::Util::postdetect" |
1236 | : () |
1238 | : () |
1237 | } |
|
|
1238 | } |
1239 | } |
1239 | |
1240 | |
1240 | sub AnyEvent::Util::postdetect::DESTROY { |
1241 | sub AnyEvent::Util::postdetect::DESTROY { |
1241 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1242 | @post_detect = grep $_ != ${$_[0]}, @post_detect; |
1242 | } |
1243 | } |
… | |
… | |
1295 | @models = (); # free probe data |
1296 | @models = (); # free probe data |
1296 | |
1297 | |
1297 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1298 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1298 | unshift @ISA, $MODEL; |
1299 | unshift @ISA, $MODEL; |
1299 | |
1300 | |
|
|
1301 | # now nuke some methods that are overriden by the backend. |
|
|
1302 | # SUPER is not allowed. |
|
|
1303 | for (qw(time signal child idle)) { |
|
|
1304 | undef &{"AnyEvent::Base::$_"} |
|
|
1305 | if defined &{"$MODEL\::$_"}; |
|
|
1306 | } |
|
|
1307 | |
1300 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1308 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1301 | |
1309 | |
1302 | (shift @post_detect)->() while @post_detect; |
1310 | (shift @post_detect)->() while @post_detect; |
|
|
1311 | |
|
|
1312 | *post_detect = sub(&) { |
|
|
1313 | shift->(); |
|
|
1314 | |
|
|
1315 | undef |
|
|
1316 | }; |
1303 | |
1317 | |
1304 | $MODEL |
1318 | $MODEL |
1305 | } |
1319 | } |
1306 | |
1320 | |
1307 | sub AUTOLOAD { |
1321 | sub AUTOLOAD { |
… | |
… | |
1335 | |
1349 | |
1336 | =head1 SIMPLIFIED AE API |
1350 | =head1 SIMPLIFIED AE API |
1337 | |
1351 | |
1338 | Starting with version 5.0, AnyEvent officially supports a second, much |
1352 | Starting with version 5.0, AnyEvent officially supports a second, much |
1339 | simpler, API that is designed to reduce the calling, typing and memory |
1353 | simpler, API that is designed to reduce the calling, typing and memory |
1340 | overhead. |
1354 | overhead by using function call syntax and a fixed number of parameters. |
1341 | |
1355 | |
1342 | See the L<AE> manpage for details. |
1356 | See the L<AE> manpage for details. |
1343 | |
1357 | |
1344 | =cut |
1358 | =cut |
1345 | |
1359 | |
1346 | package AE; |
1360 | package AE; |
1347 | |
1361 | |
1348 | our $VERSION = $AnyEvent::VERSION; |
1362 | our $VERSION = $AnyEvent::VERSION; |
|
|
1363 | |
|
|
1364 | # fall back to the main API by default - backends and AnyEvent::Base |
|
|
1365 | # implementations can overwrite these. |
1349 | |
1366 | |
1350 | sub io($$$) { |
1367 | sub io($$$) { |
1351 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1368 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1352 | } |
1369 | } |
1353 | |
1370 | |
… | |
… | |
1385 | |
1402 | |
1386 | package AnyEvent::Base; |
1403 | package AnyEvent::Base; |
1387 | |
1404 | |
1388 | # default implementations for many methods |
1405 | # default implementations for many methods |
1389 | |
1406 | |
1390 | sub _time() { |
1407 | sub time { |
1391 | eval q{ # poor man's autoloading |
1408 | eval q{ # poor man's autoloading {} |
1392 | # probe for availability of Time::HiRes |
1409 | # probe for availability of Time::HiRes |
1393 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1410 | if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { |
1394 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1411 | warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; |
1395 | *_time = \&Time::HiRes::time; |
1412 | *AE::time = \&Time::HiRes::time; |
1396 | # if (eval "use POSIX (); (POSIX::times())... |
1413 | # if (eval "use POSIX (); (POSIX::times())... |
1397 | } else { |
1414 | } else { |
1398 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1415 | warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; |
1399 | *_time = sub (){ time }; # epic fail |
1416 | *AE::time = sub (){ time }; # epic fail |
1400 | } |
1417 | } |
|
|
1418 | |
|
|
1419 | *time = sub { AE::time }; # different prototypes |
1401 | }; |
1420 | }; |
1402 | die if $@; |
1421 | die if $@; |
1403 | |
1422 | |
1404 | &_time |
1423 | &time |
1405 | } |
1424 | } |
1406 | |
1425 | |
1407 | sub time { _time } |
1426 | *now = \&time; |
1408 | sub now { _time } |
1427 | |
1409 | sub now_update { } |
1428 | sub now_update { } |
1410 | |
1429 | |
1411 | # default implementation for ->condvar |
1430 | # default implementation for ->condvar |
1412 | |
1431 | |
1413 | sub condvar { |
1432 | sub condvar { |
|
|
1433 | eval q{ # poor man's autoloading {} |
|
|
1434 | *condvar = sub { |
1414 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
1435 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" |
|
|
1436 | }; |
|
|
1437 | |
|
|
1438 | *AE::cv = sub (;&) { |
|
|
1439 | bless { @_ ? (_ae_cb => shift) : () }, "AnyEvent::CondVar" |
|
|
1440 | }; |
|
|
1441 | }; |
|
|
1442 | die if $@; |
|
|
1443 | |
|
|
1444 | &condvar |
1415 | } |
1445 | } |
1416 | |
1446 | |
1417 | # default implementation for ->signal |
1447 | # default implementation for ->signal |
1418 | |
1448 | |
1419 | our $HAVE_ASYNC_INTERRUPT; |
1449 | our $HAVE_ASYNC_INTERRUPT; |
… | |
… | |
1449 | undef $SIG_TW |
1479 | undef $SIG_TW |
1450 | unless --$SIG_COUNT; |
1480 | unless --$SIG_COUNT; |
1451 | } |
1481 | } |
1452 | |
1482 | |
1453 | our $_sig_name_init; $_sig_name_init = sub { |
1483 | our $_sig_name_init; $_sig_name_init = sub { |
1454 | eval q{ # poor man's autoloading |
1484 | eval q{ # poor man's autoloading {} |
1455 | undef $_sig_name_init; |
1485 | undef $_sig_name_init; |
1456 | |
1486 | |
1457 | if (_have_async_interrupt) { |
1487 | if (_have_async_interrupt) { |
1458 | *sig2num = \&Async::Interrupt::sig2num; |
1488 | *sig2num = \&Async::Interrupt::sig2num; |
1459 | *sig2name = \&Async::Interrupt::sig2name; |
1489 | *sig2name = \&Async::Interrupt::sig2name; |
… | |
… | |
1491 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1521 | $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; |
1492 | |
1522 | |
1493 | } else { |
1523 | } else { |
1494 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1524 | warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; |
1495 | |
1525 | |
1496 | require Fcntl; |
|
|
1497 | |
|
|
1498 | if (AnyEvent::WIN32) { |
1526 | if (AnyEvent::WIN32) { |
1499 | require AnyEvent::Util; |
1527 | require AnyEvent::Util; |
1500 | |
1528 | |
1501 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1529 | ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); |
1502 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1530 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; |
1503 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case |
1531 | AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case |
1504 | } else { |
1532 | } else { |
1505 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1533 | pipe $SIGPIPE_R, $SIGPIPE_W; |
1506 | fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; |
1534 | fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R; |
1507 | fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case |
1535 | fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case |
1508 | |
1536 | |
1509 | # not strictly required, as $^F is normally 2, but let's make sure... |
1537 | # not strictly required, as $^F is normally 2, but let's make sure... |
1510 | fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1538 | fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1511 | fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; |
1539 | fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC; |
1512 | } |
1540 | } |
1513 | |
1541 | |
1514 | $SIGPIPE_R |
1542 | $SIGPIPE_R |
1515 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1543 | or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; |
1516 | |
1544 | |
1517 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1545 | $SIG_IO = AE::io $SIGPIPE_R, 0, \&_signal_exec; |
1518 | } |
1546 | } |
1519 | |
1547 | |
1520 | *signal = sub { |
1548 | *signal = $HAVE_ASYNC_INTERRUPT |
|
|
1549 | ? sub { |
1521 | my (undef, %arg) = @_; |
1550 | my (undef, %arg) = @_; |
1522 | |
1551 | |
1523 | my $signal = uc $arg{signal} |
|
|
1524 | or Carp::croak "required option 'signal' is missing"; |
|
|
1525 | |
|
|
1526 | if ($HAVE_ASYNC_INTERRUPT) { |
|
|
1527 | # async::interrupt |
1552 | # async::interrupt |
1528 | |
|
|
1529 | $signal = sig2num $signal; |
1553 | my $signal = sig2num $arg{signal}; |
1530 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1554 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1531 | |
1555 | |
1532 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1556 | $SIG_ASY{$signal} ||= new Async::Interrupt |
1533 | cb => sub { undef $SIG_EV{$signal} }, |
1557 | cb => sub { undef $SIG_EV{$signal} }, |
1534 | signal => $signal, |
1558 | signal => $signal, |
1535 | pipe => [$SIGPIPE_R->filenos], |
1559 | pipe => [$SIGPIPE_R->filenos], |
1536 | pipe_autodrain => 0, |
1560 | pipe_autodrain => 0, |
1537 | ; |
1561 | ; |
1538 | |
1562 | |
1539 | } else { |
1563 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1564 | } |
|
|
1565 | : sub { |
|
|
1566 | my (undef, %arg) = @_; |
|
|
1567 | |
1540 | # pure perl |
1568 | # pure perl |
1541 | |
|
|
1542 | # AE::Util has been loaded in signal |
|
|
1543 | $signal = sig2name $signal; |
1569 | my $signal = sig2name $arg{signal}; |
1544 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1570 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
1545 | |
1571 | |
1546 | $SIG{$signal} ||= sub { |
1572 | $SIG{$signal} ||= sub { |
1547 | local $!; |
1573 | local $!; |
1548 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1574 | syswrite $SIGPIPE_W, "\x00", 1 unless %SIG_EV; |
1549 | undef $SIG_EV{$signal}; |
1575 | undef $SIG_EV{$signal}; |
1550 | }; |
1576 | }; |
1551 | |
1577 | |
1552 | # can't do signal processing without introducing races in pure perl, |
1578 | # can't do signal processing without introducing races in pure perl, |
1553 | # so limit the signal latency. |
1579 | # so limit the signal latency. |
1554 | _sig_add; |
1580 | _sig_add; |
1555 | } |
|
|
1556 | |
1581 | |
1557 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1582 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
|
|
1583 | } |
1558 | }; |
1584 | ; |
1559 | |
1585 | |
1560 | *AnyEvent::Base::signal::DESTROY = sub { |
1586 | *AnyEvent::Base::signal::DESTROY = sub { |
1561 | my ($signal, $cb) = @{$_[0]}; |
1587 | my ($signal, $cb) = @{$_[0]}; |
1562 | |
1588 | |
1563 | _sig_del; |
1589 | _sig_del; |
… | |
… | |
2059 | |
2085 | |
2060 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2086 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2061 | that occurred during request processing. The C<result> method detects |
2087 | that occurred during request processing. The C<result> method detects |
2062 | whether an exception as thrown (it is stored inside the $txn object) |
2088 | whether an exception as thrown (it is stored inside the $txn object) |
2063 | and just throws the exception, which means connection errors and other |
2089 | and just throws the exception, which means connection errors and other |
2064 | problems get reported tot he code that tries to use the result, not in a |
2090 | problems get reported to the code that tries to use the result, not in a |
2065 | random callback. |
2091 | random callback. |
2066 | |
2092 | |
2067 | All of this enables the following usage styles: |
2093 | All of this enables the following usage styles: |
2068 | |
2094 | |
2069 | 1. Blocking: |
2095 | 1. Blocking: |
… | |
… | |
2526 | automatic timer adjustments even when no monotonic clock is available, |
2552 | automatic timer adjustments even when no monotonic clock is available, |
2527 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2553 | can take avdantage of advanced kernel interfaces such as C<epoll> and |
2528 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2554 | C<kqueue>, and is the fastest backend I<by far>. You can even embed |
2529 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2555 | L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). |
2530 | |
2556 | |
|
|
2557 | If you only use backends that rely on another event loop (e.g. C<Tk>), |
|
|
2558 | then this module will do nothing for you. |
|
|
2559 | |
2531 | =item L<Guard> |
2560 | =item L<Guard> |
2532 | |
2561 | |
2533 | The guard module, when used, will be used to implement |
2562 | The guard module, when used, will be used to implement |
2534 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2563 | C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a |
2535 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2564 | lot less memory), but otherwise doesn't affect guard operation much. It is |
2536 | purely used for performance. |
2565 | purely used for performance. |
2537 | |
2566 | |
2538 | =item L<JSON> and L<JSON::XS> |
2567 | =item L<JSON> and L<JSON::XS> |
2539 | |
2568 | |
2540 | One of these modules is required when you want to read or write JSON data |
2569 | One of these modules is required when you want to read or write JSON data |
2541 | via L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2570 | via L<AnyEvent::Handle>. L<JSON> is also written in pure-perl, but can take |
2542 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2571 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2543 | |
|
|
2544 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
|
|
2545 | installed. |
|
|
2546 | |
2572 | |
2547 | =item L<Net::SSLeay> |
2573 | =item L<Net::SSLeay> |
2548 | |
2574 | |
2549 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2575 | Implementing TLS/SSL in Perl is certainly interesting, but not very |
2550 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |
2576 | worthwhile: If this module is installed, then L<AnyEvent::Handle> (with |