ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.306 by root, Tue Dec 15 05:49:13 2009 UTC vs.
Revision 1.316 by root, Mon Mar 15 18:51:30 2010 UTC

503 503
504=head2 IDLE WATCHERS 504=head2 IDLE WATCHERS
505 505
506 $w = AnyEvent->idle (cb => <callback>); 506 $w = AnyEvent->idle (cb => <callback>);
507 507
508Sometimes there is a need to do something, but it is not so important 508Repeatedly invoke the callback after the process becomes idle, until
509to do it instantly, but only when there is nothing better to do. This 509either the watcher is destroyed or new events have been detected.
510"nothing better to do" is usually defined to be "no other events need
511attention by the event loop".
512 510
513Idle watchers ideally get invoked when the event loop has nothing 511Idle watchers are useful when there is a need to do something, but it
514better to do, just before it would block the process to wait for new 512is not so important (or wise) to do it instantly. The callback will be
515events. Instead of blocking, the idle watcher is invoked. 513invoked only when there is "nothing better to do", which is usually
514defined as "all outstanding events have been handled and no new events
515have been detected". That means that idle watchers ideally get invoked
516when the event loop has just polled for new events but none have been
517detected. Instead of blocking to wait for more events, the idle watchers
518will be invoked.
516 519
517Most event loops unfortunately do not really support idle watchers (only 520Unfortunately, most event loops do not really support idle watchers (only
518EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent 521EV, Event and Glib do it in a usable fashion) - for the rest, AnyEvent
519will simply call the callback "from time to time". 522will simply call the callback "from time to time".
520 523
521Example: read lines from STDIN, but only process them when the 524Example: read lines from STDIN, but only process them when the
522program is otherwise idle: 525program is otherwise idle:
1153 1156
1154BEGIN { AnyEvent::common_sense } 1157BEGIN { AnyEvent::common_sense }
1155 1158
1156use Carp (); 1159use Carp ();
1157 1160
1158our $VERSION = '5.22'; 1161our $VERSION = '5.251';
1159our $MODEL; 1162our $MODEL;
1160 1163
1161our $AUTOLOAD; 1164our $AUTOLOAD;
1162our @ISA; 1165our @ISA;
1163 1166
1164our @REGISTRY; 1167our @REGISTRY;
1165 1168
1166our $VERBOSE; 1169our $VERBOSE;
1167 1170
1168BEGIN { 1171BEGIN {
1169 eval "sub WIN32(){ " . (($^O =~ /mswin32/i)*1) ." }"; 1172 require "AnyEvent/constants.pl";
1173
1170 eval "sub TAINT(){ " . (${^TAINT}*1) . " }"; 1174 eval "sub TAINT (){" . (${^TAINT} *1) . "}";
1171 1175
1172 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1176 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1173 if ${^TAINT}; 1177 if ${^TAINT};
1174 1178
1175 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1179 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1236sub AnyEvent::Util::postdetect::DESTROY { 1240sub AnyEvent::Util::postdetect::DESTROY {
1237 @post_detect = grep $_ != ${$_[0]}, @post_detect; 1241 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1238} 1242}
1239 1243
1240sub detect() { 1244sub detect() {
1245 # free some memory
1246 *detect = sub () { $MODEL };
1247
1248 local $!; # for good measure
1249 local $SIG{__DIE__};
1250
1251 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
1252 my $model = "AnyEvent::Impl::$1";
1253 if (eval "require $model") {
1254 $MODEL = $model;
1255 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2;
1256 } else {
1257 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE;
1258 }
1259 }
1260
1261 # check for already loaded models
1241 unless ($MODEL) { 1262 unless ($MODEL) {
1242 local $SIG{__DIE__}; 1263 for (@REGISTRY, @models) {
1243 1264 my ($package, $model) = @$_;
1244 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1265 if (${"$package\::VERSION"} > 0) {
1245 my $model = "AnyEvent::Impl::$1";
1246 if (eval "require $model") { 1266 if (eval "require $model") {
1247 $MODEL = $model; 1267 $MODEL = $model;
1248 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; 1268 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2;
1249 } else { 1269 last;
1250 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1270 }
1251 } 1271 }
1252 } 1272 }
1253 1273
1254 # check for already loaded models
1255 unless ($MODEL) { 1274 unless ($MODEL) {
1275 # try to autoload a model
1256 for (@REGISTRY, @models) { 1276 for (@REGISTRY, @models) {
1257 my ($package, $model) = @$_; 1277 my ($package, $model, $autoload) = @$_;
1278 if (
1279 $autoload
1280 and eval "require $package"
1258 if (${"$package\::VERSION"} > 0) { 1281 and ${"$package\::VERSION"} > 0
1259 if (eval "require $model") { 1282 and eval "require $model"
1283 ) {
1260 $MODEL = $model; 1284 $MODEL = $model;
1261 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; 1285 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2;
1262 last; 1286 last;
1263 }
1264 } 1287 }
1265 } 1288 }
1266 1289
1267 unless ($MODEL) {
1268 # try to autoload a model
1269 for (@REGISTRY, @models) {
1270 my ($package, $model, $autoload) = @$_;
1271 if (
1272 $autoload
1273 and eval "require $package"
1274 and ${"$package\::VERSION"} > 0
1275 and eval "require $model"
1276 ) {
1277 $MODEL = $model;
1278 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2;
1279 last;
1280 }
1281 }
1282
1283 $MODEL 1290 $MODEL
1284 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; 1291 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n";
1285 }
1286 } 1292 }
1287
1288 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1289
1290 unshift @ISA, $MODEL;
1291
1292 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
1293
1294 (shift @post_detect)->() while @post_detect;
1295 } 1293 }
1294
1295 @models = (); # free probe data
1296
1297 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1298 unshift @ISA, $MODEL;
1299
1300 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
1301
1302 (shift @post_detect)->() while @post_detect;
1296 1303
1297 $MODEL 1304 $MODEL
1298} 1305}
1299 1306
1300sub AUTOLOAD { 1307sub AUTOLOAD {
1301 (my $func = $AUTOLOAD) =~ s/.*://; 1308 (my $func = $AUTOLOAD) =~ s/.*://;
1302 1309
1303 $method{$func} 1310 $method{$func}
1304 or Carp::croak "$func: not a valid method for AnyEvent objects"; 1311 or Carp::croak "$func: not a valid AnyEvent class method";
1305 1312
1306 detect unless $MODEL; 1313 detect;
1307 1314
1308 my $class = shift; 1315 my $class = shift;
1309 $class->$func (@_); 1316 $class->$func (@_);
1310} 1317}
1311 1318
1379package AnyEvent::Base; 1386package AnyEvent::Base;
1380 1387
1381# default implementations for many methods 1388# default implementations for many methods
1382 1389
1383sub _time() { 1390sub _time() {
1391 eval q{ # poor man's autoloading
1384 # probe for availability of Time::HiRes 1392 # probe for availability of Time::HiRes
1385 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1393 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1386 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1394 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8;
1387 *_time = \&Time::HiRes::time; 1395 *_time = \&Time::HiRes::time;
1388 # if (eval "use POSIX (); (POSIX::times())... 1396 # if (eval "use POSIX (); (POSIX::times())...
1389 } else { 1397 } else {
1390 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1398 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE;
1391 *_time = sub { time }; # epic fail 1399 *_time = sub (){ time }; # epic fail
1400 }
1392 } 1401 };
1402 die if $@;
1393 1403
1394 &_time 1404 &_time
1395} 1405}
1396 1406
1397sub time { _time } 1407sub time { _time }
1418 1428
1419our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO); 1429our ($SIGPIPE_R, $SIGPIPE_W, %SIG_CB, %SIG_EV, $SIG_IO);
1420our (%SIG_ASY, %SIG_ASY_W); 1430our (%SIG_ASY, %SIG_ASY_W);
1421our ($SIG_COUNT, $SIG_TW); 1431our ($SIG_COUNT, $SIG_TW);
1422 1432
1423sub _signal_exec {
1424 $HAVE_ASYNC_INTERRUPT
1425 ? $SIGPIPE_R->drain
1426 : sysread $SIGPIPE_R, (my $dummy), 9;
1427
1428 while (%SIG_EV) {
1429 for (keys %SIG_EV) {
1430 delete $SIG_EV{$_};
1431 $_->() for values %{ $SIG_CB{$_} || {} };
1432 }
1433 }
1434}
1435
1436# install a dummy wakeup watcher to reduce signal catching latency 1433# install a dummy wakeup watcher to reduce signal catching latency
1434# used by Impls
1437sub _sig_add() { 1435sub _sig_add() {
1438 unless ($SIG_COUNT++) { 1436 unless ($SIG_COUNT++) {
1439 # try to align timer on a full-second boundary, if possible 1437 # try to align timer on a full-second boundary, if possible
1440 my $NOW = AE::now; 1438 my $NOW = AE::now;
1441 1439
1493 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1491 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1494 1492
1495 } else { 1493 } else {
1496 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1494 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8;
1497 1495
1498 require Fcntl;
1499
1500 if (AnyEvent::WIN32) { 1496 if (AnyEvent::WIN32) {
1501 require AnyEvent::Util; 1497 require AnyEvent::Util;
1502 1498
1503 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1499 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1504 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R; 1500 AnyEvent::Util::fh_nonblocking ($SIGPIPE_R, 1) if $SIGPIPE_R;
1505 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case 1501 AnyEvent::Util::fh_nonblocking ($SIGPIPE_W, 1) if $SIGPIPE_W; # just in case
1506 } else { 1502 } else {
1507 pipe $SIGPIPE_R, $SIGPIPE_W; 1503 pipe $SIGPIPE_R, $SIGPIPE_W;
1508 fcntl $SIGPIPE_R, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_R; 1504 fcntl $SIGPIPE_R, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_R;
1509 fcntl $SIGPIPE_W, &Fcntl::F_SETFL, &Fcntl::O_NONBLOCK if $SIGPIPE_W; # just in case 1505 fcntl $SIGPIPE_W, AnyEvent::F_SETFL, AnyEvent::O_NONBLOCK if $SIGPIPE_W; # just in case
1510 1506
1511 # not strictly required, as $^F is normally 2, but let's make sure... 1507 # not strictly required, as $^F is normally 2, but let's make sure...
1512 fcntl $SIGPIPE_R, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; 1508 fcntl $SIGPIPE_R, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC;
1513 fcntl $SIGPIPE_W, &Fcntl::F_SETFD, &Fcntl::FD_CLOEXEC; 1509 fcntl $SIGPIPE_W, AnyEvent::F_SETFD, AnyEvent::FD_CLOEXEC;
1514 } 1510 }
1515 1511
1516 $SIGPIPE_R 1512 $SIGPIPE_R
1517 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n"; 1513 or Carp::croak "AnyEvent: unable to create a signal reporting pipe: $!\n";
1518 1514
1572 # print weird messages, or just unconditionally exit 1568 # print weird messages, or just unconditionally exit
1573 # instead of getting the default action. 1569 # instead of getting the default action.
1574 undef $SIG{$signal} 1570 undef $SIG{$signal}
1575 unless keys %{ $SIG_CB{$signal} }; 1571 unless keys %{ $SIG_CB{$signal} };
1576 }; 1572 };
1573
1574 *_signal_exec = sub {
1575 $HAVE_ASYNC_INTERRUPT
1576 ? $SIGPIPE_R->drain
1577 : sysread $SIGPIPE_R, (my $dummy), 9;
1578
1579 while (%SIG_EV) {
1580 for (keys %SIG_EV) {
1581 delete $SIG_EV{$_};
1582 $_->() for values %{ $SIG_CB{$_} || {} };
1583 }
1584 }
1585 };
1577 }; 1586 };
1578 die if $@; 1587 die if $@;
1588
1579 &signal 1589 &signal
1580} 1590}
1581 1591
1582# default implementation for ->child 1592# default implementation for ->child
1583 1593
1584our %PID_CB; 1594our %PID_CB;
1585our $CHLD_W; 1595our $CHLD_W;
1586our $CHLD_DELAY_W; 1596our $CHLD_DELAY_W;
1587our $WNOHANG; 1597our $WNOHANG;
1588 1598
1599# used by many Impl's
1589sub _emit_childstatus($$) { 1600sub _emit_childstatus($$) {
1590 my (undef, $rpid, $rstatus) = @_; 1601 my (undef, $rpid, $rstatus) = @_;
1591 1602
1592 $_->($rpid, $rstatus) 1603 $_->($rpid, $rstatus)
1593 for values %{ $PID_CB{$rpid} || {} }, 1604 for values %{ $PID_CB{$rpid} || {} },
1594 values %{ $PID_CB{0} || {} }; 1605 values %{ $PID_CB{0} || {} };
1595} 1606}
1596 1607
1597sub _sigchld {
1598 my $pid;
1599
1600 AnyEvent->_emit_childstatus ($pid, $?)
1601 while ($pid = waitpid -1, $WNOHANG) > 0;
1602}
1603
1604sub child { 1608sub child {
1609 eval q{ # poor man's autoloading {}
1610 *_sigchld = sub {
1611 my $pid;
1612
1613 AnyEvent->_emit_childstatus ($pid, $?)
1614 while ($pid = waitpid -1, $WNOHANG) > 0;
1615 };
1616
1617 *child = sub {
1605 my (undef, %arg) = @_; 1618 my (undef, %arg) = @_;
1606 1619
1607 defined (my $pid = $arg{pid} + 0) 1620 defined (my $pid = $arg{pid} + 0)
1608 or Carp::croak "required option 'pid' is missing"; 1621 or Carp::croak "required option 'pid' is missing";
1609 1622
1610 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1623 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
1611 1624
1612 # WNOHANG is almost cetrainly 1 everywhere 1625 # WNOHANG is almost cetrainly 1 everywhere
1613 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ 1626 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1614 ? 1 1627 ? 1
1615 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; 1628 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1616 1629
1617 unless ($CHLD_W) { 1630 unless ($CHLD_W) {
1618 $CHLD_W = AE::signal CHLD => \&_sigchld; 1631 $CHLD_W = AE::signal CHLD => \&_sigchld;
1619 # child could be a zombie already, so make at least one round 1632 # child could be a zombie already, so make at least one round
1620 &_sigchld; 1633 &_sigchld;
1621 } 1634 }
1622 1635
1623 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1636 bless [$pid, $arg{cb}], "AnyEvent::Base::child"
1624} 1637 };
1625 1638
1626sub AnyEvent::Base::child::DESTROY { 1639 *AnyEvent::Base::child::DESTROY = sub {
1627 my ($pid, $cb) = @{$_[0]}; 1640 my ($pid, $cb) = @{$_[0]};
1628 1641
1629 delete $PID_CB{$pid}{$cb}; 1642 delete $PID_CB{$pid}{$cb};
1630 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 1643 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
1631 1644
1632 undef $CHLD_W unless keys %PID_CB; 1645 undef $CHLD_W unless keys %PID_CB;
1646 };
1647 };
1648 die if $@;
1649
1650 &child
1633} 1651}
1634 1652
1635# idle emulation is done by simply using a timer, regardless 1653# idle emulation is done by simply using a timer, regardless
1636# of whether the process is idle or not, and not letting 1654# of whether the process is idle or not, and not letting
1637# the callback use more than 50% of the time. 1655# the callback use more than 50% of the time.
1638sub idle { 1656sub idle {
1657 eval q{ # poor man's autoloading {}
1658 *idle = sub {
1639 my (undef, %arg) = @_; 1659 my (undef, %arg) = @_;
1640 1660
1641 my ($cb, $w, $rcb) = $arg{cb}; 1661 my ($cb, $w, $rcb) = $arg{cb};
1642 1662
1643 $rcb = sub { 1663 $rcb = sub {
1644 if ($cb) { 1664 if ($cb) {
1645 $w = _time; 1665 $w = _time;
1646 &$cb; 1666 &$cb;
1647 $w = _time - $w; 1667 $w = _time - $w;
1648 1668
1649 # never use more then 50% of the time for the idle watcher, 1669 # never use more then 50% of the time for the idle watcher,
1650 # within some limits 1670 # within some limits
1651 $w = 0.0001 if $w < 0.0001; 1671 $w = 0.0001 if $w < 0.0001;
1652 $w = 5 if $w > 5; 1672 $w = 5 if $w > 5;
1653 1673
1654 $w = AE::timer $w, 0, $rcb; 1674 $w = AE::timer $w, 0, $rcb;
1655 } else { 1675 } else {
1656 # clean up... 1676 # clean up...
1657 undef $w; 1677 undef $w;
1658 undef $rcb; 1678 undef $rcb;
1679 }
1680 };
1681
1682 $w = AE::timer 0.05, 0, $rcb;
1683
1684 bless \\$cb, "AnyEvent::Base::idle"
1659 } 1685 };
1686
1687 *AnyEvent::Base::idle::DESTROY = sub {
1688 undef $${$_[0]};
1689 };
1660 }; 1690 };
1691 die if $@;
1661 1692
1662 $w = AE::timer 0.05, 0, $rcb; 1693 &idle
1663
1664 bless \\$cb, "AnyEvent::Base::idle"
1665}
1666
1667sub AnyEvent::Base::idle::DESTROY {
1668 undef $${$_[0]};
1669} 1694}
1670 1695
1671package AnyEvent::CondVar; 1696package AnyEvent::CondVar;
1672 1697
1673our @ISA = AnyEvent::CondVar::Base::; 1698our @ISA = AnyEvent::CondVar::Base::;
2499automatic timer adjustments even when no monotonic clock is available, 2524automatic timer adjustments even when no monotonic clock is available,
2500can take avdantage of advanced kernel interfaces such as C<epoll> and 2525can take avdantage of advanced kernel interfaces such as C<epoll> and
2501C<kqueue>, and is the fastest backend I<by far>. You can even embed 2526C<kqueue>, and is the fastest backend I<by far>. You can even embed
2502L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>). 2527L<Glib>/L<Gtk2> in it (or vice versa, see L<EV::Glib> and L<Glib::EV>).
2503 2528
2529If you only use backends that rely on another event loop (e.g. C<Tk>),
2530then this module will do nothing for you.
2531
2504=item L<Guard> 2532=item L<Guard>
2505 2533
2506The guard module, when used, will be used to implement 2534The guard module, when used, will be used to implement
2507C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a 2535C<AnyEvent::Util::guard>. This speeds up guards considerably (and uses a
2508lot less memory), but otherwise doesn't affect guard operation much. It is 2536lot less memory), but otherwise doesn't affect guard operation much. It is
2509purely used for performance. 2537purely used for performance.
2510 2538
2511=item L<JSON> and L<JSON::XS> 2539=item L<JSON> and L<JSON::XS>
2512 2540
2513One of these modules is required when you want to read or write JSON data 2541One of these modules is required when you want to read or write JSON data
2514via L<AnyEvent::Handle>. It is also written in pure-perl, but can take 2542via L<AnyEvent::Handle>. L<JSON> is also written in pure-perl, but can take
2515advantage of the ultra-high-speed L<JSON::XS> module when it is installed. 2543advantage of the ultra-high-speed L<JSON::XS> module when it is installed.
2516
2517In fact, L<AnyEvent::Handle> will use L<JSON::XS> by default if it is
2518installed.
2519 2544
2520=item L<Net::SSLeay> 2545=item L<Net::SSLeay>
2521 2546
2522Implementing TLS/SSL in Perl is certainly interesting, but not very 2547Implementing TLS/SSL in Perl is certainly interesting, but not very
2523worthwhile: If this module is installed, then L<AnyEvent::Handle> (with 2548worthwhile: If this module is installed, then L<AnyEvent::Handle> (with
2534 2559
2535 2560
2536=head1 FORK 2561=head1 FORK
2537 2562
2538Most event libraries are not fork-safe. The ones who are usually are 2563Most event libraries are not fork-safe. The ones who are usually are
2539because they rely on inefficient but fork-safe C<select> or C<poll> 2564because they rely on inefficient but fork-safe C<select> or C<poll> calls
2540calls. Only L<EV> is fully fork-aware. 2565- higher performance APIs such as BSD's kqueue or the dreaded Linux epoll
2566are usually badly thought-out hacks that are incompatible with fork in
2567one way or another. Only L<EV> is fully fork-aware and ensures that you
2568continue event-processing in both parent and child (or both, if you know
2569what you are doing).
2541 2570
2542This means that, in general, you cannot fork and do event processing 2571This means that, in general, you cannot fork and do event processing in
2543in the child if a watcher was created before the fork (which in turn 2572the child if the event library was initialised before the fork (which
2544initialises the event library). 2573usually happens when the first AnyEvent watcher is created, or the library
2574is loaded).
2545 2575
2546If you have to fork, you must either do so I<before> creating your first 2576If you have to fork, you must either do so I<before> creating your first
2547watcher OR you must not use AnyEvent at all in the child OR you must do 2577watcher OR you must not use AnyEvent at all in the child OR you must do
2548something completely out of the scope of AnyEvent. 2578something completely out of the scope of AnyEvent.
2549 2579
2550The problem of doing event processing in the parent I<and> the child 2580The problem of doing event processing in the parent I<and> the child
2551is much more complicated: even for backends that I<are> fork-aware or 2581is much more complicated: even for backends that I<are> fork-aware or
2552fork-safe, their behaviour is not usually what you want: fork clones all 2582fork-safe, their behaviour is not usually what you want: fork clones all
2553watchers, that means all timers, I/O watchers etc. are active in both 2583watchers, that means all timers, I/O watchers etc. are active in both
2554parent and child, which is almost never what you want. 2584parent and child, which is almost never what you want. USing C<exec>
2585to start worker children from some kind of manage rprocess is usually
2586preferred, because it is much easier and cleaner, at the expense of having
2587to have another binary.
2555 2588
2556 2589
2557=head1 SECURITY CONSIDERATIONS 2590=head1 SECURITY CONSIDERATIONS
2558 2591
2559AnyEvent can be forced to load any event model via 2592AnyEvent can be forced to load any event model via

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines