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 | |
… | |
… | |
1270 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1283 | $_->() for values %{ $SIG_CB{$_} || {} }; |
1271 | } |
1284 | } |
1272 | } |
1285 | } |
1273 | } |
1286 | } |
1274 | |
1287 | |
|
|
1288 | # install a dumym wakeupw atcher to reduce signal catching latency |
|
|
1289 | sub _sig_add() { |
|
|
1290 | unless ($SIG_COUNT++) { |
|
|
1291 | # try to align timer on a full-second boundary, if possible |
|
|
1292 | my $NOW = AnyEvent->now; |
|
|
1293 | |
|
|
1294 | $SIG_TW = AnyEvent->timer ( |
|
|
1295 | after => $MAX_SIGNAL_LATENCY - ($NOW - int $NOW), |
|
|
1296 | interval => $MAX_SIGNAL_LATENCY, |
|
|
1297 | cb => sub { }, # just for the PERL_ASYNC_CHECK |
|
|
1298 | ); |
|
|
1299 | } |
|
|
1300 | } |
|
|
1301 | |
|
|
1302 | sub _sig_del { |
|
|
1303 | undef $SIG_TW |
|
|
1304 | unless --$SIG_COUNT; |
|
|
1305 | } |
|
|
1306 | |
1275 | sub _signal { |
1307 | sub _signal { |
1276 | my (undef, %arg) = @_; |
1308 | my (undef, %arg) = @_; |
1277 | |
1309 | |
1278 | my $signal = uc $arg{signal} |
1310 | my $signal = uc $arg{signal} |
1279 | or Carp::croak "required option 'signal' is missing"; |
1311 | or Carp::croak "required option 'signal' is missing"; |
… | |
… | |
1303 | undef $SIG_EV{$signal}; |
1335 | undef $SIG_EV{$signal}; |
1304 | }; |
1336 | }; |
1305 | |
1337 | |
1306 | # can't do signal processing without introducing races in pure perl, |
1338 | # can't do signal processing without introducing races in pure perl, |
1307 | # so limit the signal latency. |
1339 | # so limit the signal latency. |
1308 | ++$SIG_COUNT; |
1340 | _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 | } |
1341 | } |
1315 | |
1342 | |
1316 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1343 | bless [$signal, $arg{cb}], "AnyEvent::Base::signal" |
1317 | } |
1344 | } |
1318 | |
1345 | |
… | |
… | |
1357 | } |
1384 | } |
1358 | |
1385 | |
1359 | sub AnyEvent::Base::signal::DESTROY { |
1386 | sub AnyEvent::Base::signal::DESTROY { |
1360 | my ($signal, $cb) = @{$_[0]}; |
1387 | my ($signal, $cb) = @{$_[0]}; |
1361 | |
1388 | |
1362 | undef $SIG_TW |
1389 | _sig_del; |
1363 | unless --$SIG_COUNT; |
|
|
1364 | |
1390 | |
1365 | delete $SIG_CB{$signal}{$cb}; |
1391 | delete $SIG_CB{$signal}{$cb}; |
1366 | |
1392 | |
1367 | $HAVE_ASYNC_INTERRUPT |
1393 | $HAVE_ASYNC_INTERRUPT |
1368 | ? delete $SIG_ASY{$signal} |
1394 | ? delete $SIG_ASY{$signal} |
… | |
… | |
2257 | |
2283 | |
2258 | This slightly arcane module is used to implement fast signal handling: To |
2284 | This slightly arcane module is used to implement fast signal handling: To |
2259 | my knowledge, there is no way to do completely race-free and quick |
2285 | my knowledge, there is no way to do completely race-free and quick |
2260 | signal handling in pure perl. To ensure that signals still get |
2286 | signal handling in pure perl. To ensure that signals still get |
2261 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2287 | delivered, AnyEvent will start an interval timer to wake up perl (and |
2262 | catch the signals) with soemd elay (default is 10 seconds, look for |
2288 | catch the signals) with some delay (default is 10 seconds, look for |
2263 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2289 | C<$AnyEvent::MAX_SIGNAL_LATENCY>). |
2264 | |
2290 | |
2265 | If this module is available, then it will be used to implement signal |
2291 | If this module is available, then it will be used to implement signal |
2266 | catching, which means that signals will not be delayed, and the event loop |
2292 | catching, which means that signals will not be delayed, and the event loop |
2267 | will not be interrupted regularly, which is more efficient (And good for |
2293 | will not be interrupted regularly, which is more efficient (And good for |
2268 | battery life on laptops). |
2294 | battery life on laptops). |
2269 | |
2295 | |
2270 | This affects not just the pure-perl event loop, but also other event loops |
2296 | This affects not just the pure-perl event loop, but also other event loops |
2271 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
2297 | that have no signal handling on their own (e.g. Glib, Tk, Qt). |
|
|
2298 | |
|
|
2299 | Some event loops (POE, Event, Event::Lib) offer signal watchers natively, |
|
|
2300 | and either employ their own workarounds (POE) or use AnyEvent's workaround |
|
|
2301 | (using C<$AnyEvent::MAX_SIGNAL_LATENCY>). Installing L<Async::Interrupt> |
|
|
2302 | does nothing for those backends. |
2272 | |
2303 | |
2273 | =item L<EV> |
2304 | =item L<EV> |
2274 | |
2305 | |
2275 | This module isn't really "optional", as it is simply one of the backend |
2306 | This module isn't really "optional", as it is simply one of the backend |
2276 | event loops that AnyEvent can use. However, it is simply the best event |
2307 | event loops that AnyEvent can use. However, it is simply the best event |
… | |
… | |
2290 | |
2321 | |
2291 | =item L<JSON> and L<JSON::XS> |
2322 | =item L<JSON> and L<JSON::XS> |
2292 | |
2323 | |
2293 | This module is required when you want to read or write JSON data via |
2324 | This module is required when you want to read or write JSON data via |
2294 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2325 | L<AnyEvent::Handle>. It is also written in pure-perl, but can take |
2295 | advantage of the ulta-high-speed L<JSON::XS> module when it is installed. |
2326 | advantage of the ultra-high-speed L<JSON::XS> module when it is installed. |
2296 | |
2327 | |
2297 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2328 | In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is |
2298 | installed. |
2329 | installed. |
2299 | |
2330 | |
2300 | =item L<Net::SSLeay> |
2331 | =item L<Net::SSLeay> |