1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - events independent of event loop implementation |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt and POE are various supported |
6 | event loops. |
6 | event loops. |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
… | |
… | |
40 | =head1 INTRODUCTION/TUTORIAL |
40 | =head1 INTRODUCTION/TUTORIAL |
41 | |
41 | |
42 | This manpage is mainly a reference manual. If you are interested |
42 | This manpage is mainly a reference manual. If you are interested |
43 | in a tutorial or some gentle introduction, have a look at the |
43 | in a tutorial or some gentle introduction, have a look at the |
44 | L<AnyEvent::Intro> manpage. |
44 | L<AnyEvent::Intro> manpage. |
|
|
45 | |
|
|
46 | =head1 SUPPORT |
|
|
47 | |
|
|
48 | There is a mailinglist for discussing all things AnyEvent, and an IRC |
|
|
49 | channel, too. |
|
|
50 | |
|
|
51 | See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software |
|
|
52 | Respository>, at L<http://anyevent.schmorp.de>, for more info. |
45 | |
53 | |
46 | =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) |
54 | =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) |
47 | |
55 | |
48 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
56 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
49 | nowadays. So what is different about AnyEvent? |
57 | nowadays. So what is different about AnyEvent? |
… | |
… | |
368 | |
376 | |
369 | This watcher might use C<%SIG> (depending on the event loop used), |
377 | This watcher might use C<%SIG> (depending on the event loop used), |
370 | so programs overwriting those signals directly will likely not work |
378 | so programs overwriting those signals directly will likely not work |
371 | correctly. |
379 | correctly. |
372 | |
380 | |
|
|
381 | Example: exit on SIGINT |
|
|
382 | |
|
|
383 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
384 | |
|
|
385 | =head3 Signal Races, Delays and Workarounds |
|
|
386 | |
373 | Also note that many event loops (e.g. Glib, Tk, Qt, IO::Async) do not |
387 | Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching |
374 | support attaching callbacks to signals, which is a pity, as you cannot do |
388 | callbacks to signals in a generic way, which is a pity, as you cannot do |
375 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
389 | race-free signal handling in perl. AnyEvent will try to do it's best, but |
376 | in some cases, signals will be delayed. The maximum time a signal might |
390 | in some cases, signals will be delayed. The maximum time a signal might |
377 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
391 | be delayed is specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 |
378 | seconds). This variable can be changed only before the first signal |
392 | seconds). This variable can be changed only before the first signal |
379 | watcher is created, and should be left alone otherwise. Higher values |
393 | watcher is created, and should be left alone otherwise. Higher values |
380 | will cause fewer spurious wake-ups, which is better for power and CPU |
394 | will cause fewer spurious wake-ups, which is better for power and CPU |
381 | saving. All these problems can be avoided by installing the optional |
395 | saving. All these problems can be avoided by installing the optional |
382 | L<Async::Interrupt> module. |
396 | L<Async::Interrupt> module. This will not work with inherently broken |
383 | |
397 | event loops such as L<Event> or L<Event::Lib> (and not with L<POE> |
384 | Example: exit on SIGINT |
398 | currently, as POE does it's own workaround with one-second latency). With |
385 | |
399 | those, you just have to suffer the delays. |
386 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
387 | |
400 | |
388 | =head2 CHILD PROCESS WATCHERS |
401 | =head2 CHILD PROCESS WATCHERS |
389 | |
402 | |
390 | You can also watch on a child process exit and catch its exit status. |
403 | You can also watch on a child process exit and catch its exit status. |
391 | |
404 | |
… | |
… | |
504 | Condition variables are similar to callbacks, except that you can |
517 | Condition variables are similar to callbacks, except that you can |
505 | optionally wait for them. They can also be called merge points - points |
518 | optionally wait for them. They can also be called merge points - points |
506 | in time where multiple outstanding events have been processed. And yet |
519 | in time where multiple outstanding events have been processed. And yet |
507 | another way to call them is transactions - each condition variable can be |
520 | another way to call them is transactions - each condition variable can be |
508 | used to represent a transaction, which finishes at some point and delivers |
521 | used to represent a transaction, which finishes at some point and delivers |
509 | a result. |
522 | a result. And yet some people know them as "futures" - a promise to |
|
|
523 | compute/deliver something that you can wait for. |
510 | |
524 | |
511 | Condition variables are very useful to signal that something has finished, |
525 | Condition variables are very useful to signal that something has finished, |
512 | for example, if you write a module that does asynchronous http requests, |
526 | for example, if you write a module that does asynchronous http requests, |
513 | then a condition variable would be the ideal candidate to signal the |
527 | then a condition variable would be the ideal candidate to signal the |
514 | availability of results. The user can either act when the callback is |
528 | availability of results. The user can either act when the callback is |
… | |
… | |
1053 | |
1067 | |
1054 | BEGIN { AnyEvent::common_sense } |
1068 | BEGIN { AnyEvent::common_sense } |
1055 | |
1069 | |
1056 | use Carp (); |
1070 | use Carp (); |
1057 | |
1071 | |
1058 | our $VERSION = 4.83; |
1072 | our $VERSION = 4.86; |
1059 | our $MODEL; |
1073 | our $MODEL; |
1060 | |
1074 | |
1061 | our $AUTOLOAD; |
1075 | our $AUTOLOAD; |
1062 | our @ISA; |
1076 | our @ISA; |
1063 | |
1077 | |
… | |
… | |
1270 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1284 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1271 | } |
1285 | } |
1272 | } |
1286 | } |
1273 | } |
1287 | } |
1274 | |
1288 | |
|
|
1289 | # install a dumym wakeupw atcher to reduce signal catching latency |
|
|
1290 | sub _sig_add() { |
|
|
1291 | unless ($SIG_COUNT++) { |
|
|
1292 | # try to align timer on a full-second boundary, if possible |
|
|
1293 | my $NOW = AnyEvent->now; |
|
|
1294 | |
|
|
1295 | $SIG_TW = AnyEvent->timer ( |
|
|
1296 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
|
|
1297 | interval => $MAX_SIGNAL_LATENCY, |
|
|
1298 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
|
|
1299 | ); |
|
|
1300 | } |
|
|
1301 | } |
|
|
1302 | |
|
|
1303 | sub _sig_del { |
|
|
1304 | undef $SIG_TW |
|
|
1305 | unless --$SIG_COUNT; |
|
|
1306 | } |
|
|
1307 | |
1275 | sub _signal { |
1308 | sub _signal { |
1276 | my (undef, %arg) = @_; |
1309 | my (undef, %arg) = @_; |
1277 | |
1310 | |
1278 | my $signal = uc $arg{signal} |
1311 | my $signal = uc $arg{signal} |
1279 | or Carp::croak "required option 'signal' is missing"; |
1312 | or Carp::croak "required option 'signal' is missing"; |
… | |
… | |
1303 | undef $SIG_EV{$signal}; |
1336 | undef $SIG_EV{$signal}; |
1304 | }; |
1337 | }; |
1305 | |
1338 | |
1306 | # can't do signal processing without introducing races in pure perl, |
1339 | # can't do signal processing without introducing races in pure perl, |
1307 | # so limit the signal latency. |
1340 | # so limit the signal latency. |
1308 | ++$SIG_COUNT; |
1341 | _sig_add; |
1309 | $SIG_TW ||= AnyEvent->timer ( |
|
|
1310 | after => $MAX_SIGNAL_LATENCY, |
|
|
1311 | interval => $MAX_SIGNAL_LATENCY, |
|
|
1312 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
|
|
1313 | ); |
|
|
1314 | } |
1342 | } |
1315 | |
1343 | |
1316 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1344 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1317 | } |
1345 | } |
1318 | |
1346 | |
… | |
… | |
1357 | } |
1385 | } |
1358 | |
1386 | |
1359 | sub AnyEvent::Base::signal::DESTROY { |
1387 | sub AnyEvent::Base::signal::DESTROY { |
1360 | my ($signal, $cb) = @{$_[0]}; |
1388 | my ($signal, $cb) = @{$_[0]}; |
1361 | |
1389 | |
1362 | undef $SIG_TW |
1390 | _sig_del; |
1363 | unless --$SIG_COUNT; |
|
|
1364 | |
1391 | |
1365 | delete $SIG_CB{$signal}{$cb}; |
1392 | delete $SIG_CB{$signal}{$cb}; |
1366 | |
1393 | |
|
|
1394 | $HAVE_ASYNC_INTERRUPT |
|
|
1395 | ? delete $SIG_ASY{$signal} |
1367 | # delete doesn't work with older perls - they then |
1396 | : # delete doesn't work with older perls - they then |
1368 | # print weird messages, or just unconditionally exit |
1397 | # print weird messages, or just unconditionally exit |
1369 | # instead of getting the default action. |
1398 | # instead of getting the default action. |
1370 | undef $SIG{$signal} |
1399 | undef $SIG{$signal} |
1371 | unless keys %{ $SIG_CB{$signal} }; |
1400 | unless keys %{ $SIG_CB{$signal} }; |
1372 | } |
1401 | } |
1373 | |
1402 | |
1374 | # default implementation for ->child |
1403 | # default implementation for ->child |
1375 | |
1404 | |
… | |
… | |
1566 | C<PERL_ANYEVENT_MODEL>. |
1595 | C<PERL_ANYEVENT_MODEL>. |
1567 | |
1596 | |
1568 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1597 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1569 | model it chooses. |
1598 | model it chooses. |
1570 | |
1599 | |
|
|
1600 | When set to C<8> or higher, then AnyEvent will report extra information on |
|
|
1601 | which optional modules it loads and how it implements certain features. |
|
|
1602 | |
1571 | =item C<PERL_ANYEVENT_STRICT> |
1603 | =item C<PERL_ANYEVENT_STRICT> |
1572 | |
1604 | |
1573 | AnyEvent does not do much argument checking by default, as thorough |
1605 | AnyEvent does not do much argument checking by default, as thorough |
1574 | argument checking is very costly. Setting this variable to a true value |
1606 | argument checking is very costly. Setting this variable to a true value |
1575 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
1607 | will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly |
… | |
… | |
1651 | |
1683 | |
1652 | When neither C<ca_file> nor C<ca_path> was specified during |
1684 | When neither C<ca_file> nor C<ca_path> was specified during |
1653 | L<AnyEvent::TLS> context creation, and either of these environment |
1685 | L<AnyEvent::TLS> context creation, and either of these environment |
1654 | variables exist, they will be used to specify CA certificate locations |
1686 | variables exist, they will be used to specify CA certificate locations |
1655 | instead of a system-dependent default. |
1687 | instead of a system-dependent default. |
|
|
1688 | |
|
|
1689 | =item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT> |
|
|
1690 | |
|
|
1691 | When these are set to C<1>, then the respective modules are not |
|
|
1692 | loaded. Mostly good for testing AnyEvent itself. |
1656 | |
1693 | |
1657 | =back |
1694 | =back |
1658 | |
1695 | |
1659 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1696 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1660 | |
1697 | |
… | |
… | |
2247 | |
2284 | |
2248 | This slightly arcane module is used to implement fast signal handling: To |
2285 | This slightly arcane module is used to implement fast signal handling: To |
2249 | my knowledge, there is no way to do completely race-free and quick |
2286 | my knowledge, there is no way to do completely race-free and quick |
2250 | signal handling in pure perl. To ensure that signals still get |
2287 | signal handling in pure perl. To ensure that signals still get |
2251 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2288 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2252 | catch the signals) with soemd elay (default is 10 seconds, look for |
2289 | catch the signals) with some delay (default is 10 seconds, look for |
2253 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2290 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2254 | |
2291 | |
2255 | If this module is available, then it will be used to implement signal |
2292 | If this module is available, then it will be used to implement signal |
2256 | catching, which means that signals will not be delayed, and the event loop |
2293 | catching, which means that signals will not be delayed, and the event loop |
2257 | will not be interrupted regularly, which is more efficient (And good for |
2294 | will not be interrupted regularly, which is more efficient (And good for |
2258 | battery life on laptops). |
2295 | battery life on laptops). |
2259 | |
2296 | |
2260 | This affects not just the pure-perl event loop, but also other event loops |
2297 | This affects not just the pure-perl event loop, but also other event loops |
2261 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
2298 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
|
|
2299 | |
|
|
2300 | Some event loops (POE, Event, Event::Lib) offer signal watchers natively, |
|
|
2301 | and either employ their own workarounds (POE) or use AnyEvent's workaround |
|
|
2302 | (using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L<Async::Interrupt> |
|
|
2303 | does nothing for those backends. |
2262 | |
2304 | |
2263 | =item L<EV> |
2305 | =item L<EV> |
2264 | |
2306 | |
2265 | This module isn't really "optional", as it is simply one of the backend |
2307 | This module isn't really "optional", as it is simply one of the backend |
2266 | event loops that AnyEvent can use. However, it is simply the best event |
2308 | event loops that AnyEvent can use. However, it is simply the best event |
… | |
… | |
2280 | |
2322 | |
2281 | =item L<JSON> and L<JSON::XS> |
2323 | =item L<JSON> and L<JSON::XS> |
2282 | |
2324 | |
2283 | This module is required when you want to read or write JSON data via |
2325 | This module is required when you want to read or write JSON data via |
2284 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2326 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2285 | advantage of the ulta-high-speed L<JSON::XS> module when it is installed. |
2327 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2286 | |
2328 | |
2287 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2329 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2288 | installed. |
2330 | installed. |
2289 | |
2331 | |
2290 | =item L<Net::SSLeay> |
2332 | =item L<Net::SSLeay> |