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.179 by root, Thu Sep 4 10:58:58 2008 UTC vs.
Revision 1.182 by root, Sun Sep 7 15:27:17 2008 UTC

340=head2 CHILD PROCESS WATCHERS 340=head2 CHILD PROCESS WATCHERS
341 341
342You can also watch on a child process exit and catch its exit status. 342You can also watch on a child process exit and catch its exit status.
343 343
344The child process is specified by the C<pid> argument (if set to C<0>, it 344The child process is specified by the C<pid> argument (if set to C<0>, it
345watches for any child process exit). The watcher will trigger as often 345watches for any child process exit). The watcher will triggered only when
346as status change for the child are received. This works by installing a 346the child process has finished and an exit status is available, not on
347signal handler for C<SIGCHLD>. The callback will be called with the pid 347any trace events (stopped/continued).
348and exit status (as returned by waitpid), so unlike other watcher types, 348
349you I<can> rely on child watcher callback arguments. 349The callback will be called with the pid and exit status (as returned by
350waitpid), so unlike other watcher types, you I<can> rely on child watcher
351callback arguments.
352
353This watcher type works by installing a signal handler for C<SIGCHLD>,
354and since it cannot be shared, nothing else should use SIGCHLD or reap
355random child processes (waiting for specific child processes, e.g. inside
356C<system>, is just fine).
350 357
351There is a slight catch to child watchers, however: you usually start them 358There is a slight catch to child watchers, however: you usually start them
352I<after> the child process was created, and this means the process could 359I<after> the child process was created, and this means the process could
353have exited already (and no SIGCHLD will be sent anymore). 360have exited already (and no SIGCHLD will be sent anymore).
354 361
850=cut 857=cut
851 858
852package AnyEvent; 859package AnyEvent;
853 860
854no warnings; 861no warnings;
855use strict; 862use strict qw(vars subs);
856 863
857use Carp; 864use Carp;
858 865
859our $VERSION = 4.233; 866our $VERSION = 4.234;
860our $MODEL; 867our $MODEL;
861 868
862our $AUTOLOAD; 869our $AUTOLOAD;
863our @ISA; 870our @ISA;
864 871
1021BEGIN { 1028BEGIN {
1022 if (eval "use Time::HiRes (); time (); 1") { 1029 if (eval "use Time::HiRes (); time (); 1") {
1023 *_time = \&Time::HiRes::time; 1030 *_time = \&Time::HiRes::time;
1024 # if (eval "use POSIX (); (POSIX::times())... 1031 # if (eval "use POSIX (); (POSIX::times())...
1025 } else { 1032 } else {
1026 *_time = \&CORE::time; # epic fail 1033 *_time = sub { time }; # epic fail
1027 } 1034 }
1028} 1035}
1029 1036
1030sub time { _time } 1037sub time { _time }
1031sub now { _time } 1038sub now { _time }
1175 1182
1176# undocumented/compatibility with pre-3.4 1183# undocumented/compatibility with pre-3.4
1177*broadcast = \&send; 1184*broadcast = \&send;
1178*wait = \&_wait; 1185*wait = \&_wait;
1179 1186
1187=head1 ERROR AND EXCEPTION HANDLING
1188
1189In general, AnyEvent does not do any error handling - it relies on the
1190caller to do that if required. The L<AnyEvent::Strict> module (see also
1191the C<PERL_ANYEVENT_STRICT> environment variable, below) provides strict
1192checking of all AnyEvent methods, however, which is highly useful during
1193development.
1194
1195As for exception handling (i.e. runtime errors and exceptions thrown while
1196executing a callback), this is not only highly event-loop specific, but
1197also not in any way wrapped by this module, as this is the job of the main
1198program.
1199
1200The pure perl event loop simply re-throws the exception (usually
1201within C<< condvar->recv >>), the L<Event> and L<EV> modules call C<<
1202$Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and
1203so on.
1204
1205=head1 ENVIRONMENT VARIABLES
1206
1207The following environment variables are used by this module or its
1208submodules:
1209
1210=over 4
1211
1212=item C<PERL_ANYEVENT_VERBOSE>
1213
1214By default, AnyEvent will be completely silent except in fatal
1215conditions. You can set this environment variable to make AnyEvent more
1216talkative.
1217
1218When set to C<1> or higher, causes AnyEvent to warn about unexpected
1219conditions, such as not being able to load the event model specified by
1220C<PERL_ANYEVENT_MODEL>.
1221
1222When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1223model it chooses.
1224
1225=item C<PERL_ANYEVENT_STRICT>
1226
1227AnyEvent does not do much argument checking by default, as thorough
1228argument checking is very costly. Setting this variable to a true value
1229will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly
1230check the arguments passed to most method calls. If it finds any problems
1231it will croak.
1232
1233In other words, enables "strict" mode.
1234
1235Unlike C<use strict>, it is definitely recommended ot keep it off in
1236production. Keeping C<PERL_ANYEVENT_STRICT=1> in your environment while
1237developing programs can be very useful, however.
1238
1239=item C<PERL_ANYEVENT_MODEL>
1240
1241This can be used to specify the event model to be used by AnyEvent, before
1242auto detection and -probing kicks in. It must be a string consisting
1243entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended
1244and the resulting module name is loaded and if the load was successful,
1245used as event model. If it fails to load AnyEvent will proceed with
1246auto detection and -probing.
1247
1248This functionality might change in future versions.
1249
1250For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
1251could start your program like this:
1252
1253 PERL_ANYEVENT_MODEL=Perl perl ...
1254
1255=item C<PERL_ANYEVENT_PROTOCOLS>
1256
1257Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1258for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1259of auto probing).
1260
1261Must be set to a comma-separated list of protocols or address families,
1262current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be
1263used, and preference will be given to protocols mentioned earlier in the
1264list.
1265
1266This variable can effectively be used for denial-of-service attacks
1267against local programs (e.g. when setuid), although the impact is likely
1268small, as the program has to handle connection errors already-
1269
1270Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6,
1271but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1272- only support IPv4, never try to resolve or contact IPv6
1273addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1274IPv6, but prefer IPv6 over IPv4.
1275
1276=item C<PERL_ANYEVENT_EDNS0>
1277
1278Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension
1279for DNS. This extension is generally useful to reduce DNS traffic, but
1280some (broken) firewalls drop such DNS packets, which is why it is off by
1281default.
1282
1283Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce
1284EDNS0 in its DNS requests.
1285
1286=item C<PERL_ANYEVENT_MAX_FORKS>
1287
1288The maximum number of child processes that C<AnyEvent::Util::fork_call>
1289will create in parallel.
1290
1291=back
1292
1180=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 1293=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
1181 1294
1182This is an advanced topic that you do not normally need to use AnyEvent in 1295This is an advanced topic that you do not normally need to use AnyEvent in
1183a module. This section is only of use to event loop authors who want to 1296a module. This section is only of use to event loop authors who want to
1184provide AnyEvent compatibility. 1297provide AnyEvent compatibility.
1217 1330
1218I<rxvt-unicode> also cheats a bit by not providing blocking access to 1331I<rxvt-unicode> also cheats a bit by not providing blocking access to
1219condition variables: code blocking while waiting for a condition will 1332condition variables: code blocking while waiting for a condition will
1220C<die>. This still works with most modules/usages, and blocking calls must 1333C<die>. This still works with most modules/usages, and blocking calls must
1221not be done in an interactive application, so it makes sense. 1334not be done in an interactive application, so it makes sense.
1222
1223=head1 ENVIRONMENT VARIABLES
1224
1225The following environment variables are used by this module:
1226
1227=over 4
1228
1229=item C<PERL_ANYEVENT_VERBOSE>
1230
1231By default, AnyEvent will be completely silent except in fatal
1232conditions. You can set this environment variable to make AnyEvent more
1233talkative.
1234
1235When set to C<1> or higher, causes AnyEvent to warn about unexpected
1236conditions, such as not being able to load the event model specified by
1237C<PERL_ANYEVENT_MODEL>.
1238
1239When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1240model it chooses.
1241
1242=item C<PERL_ANYEVENT_STRICT>
1243
1244AnyEvent does not do much argument checking by default, as thorough
1245argument checking is very costly. Setting this variable to a true value
1246will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly
1247check the arguments passed to most method calls. If it finds any problems
1248it will croak.
1249
1250In other words, enables "strict" mode.
1251
1252Unlike C<use strict> it is definitely recommended ot keep it off in
1253production.
1254
1255=item C<PERL_ANYEVENT_MODEL>
1256
1257This can be used to specify the event model to be used by AnyEvent, before
1258auto detection and -probing kicks in. It must be a string consisting
1259entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended
1260and the resulting module name is loaded and if the load was successful,
1261used as event model. If it fails to load AnyEvent will proceed with
1262auto detection and -probing.
1263
1264This functionality might change in future versions.
1265
1266For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
1267could start your program like this:
1268
1269 PERL_ANYEVENT_MODEL=Perl perl ...
1270
1271=item C<PERL_ANYEVENT_PROTOCOLS>
1272
1273Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1274for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1275of auto probing).
1276
1277Must be set to a comma-separated list of protocols or address families,
1278current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be
1279used, and preference will be given to protocols mentioned earlier in the
1280list.
1281
1282This variable can effectively be used for denial-of-service attacks
1283against local programs (e.g. when setuid), although the impact is likely
1284small, as the program has to handle connection errors already-
1285
1286Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6,
1287but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1288- only support IPv4, never try to resolve or contact IPv6
1289addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1290IPv6, but prefer IPv6 over IPv4.
1291
1292=item C<PERL_ANYEVENT_EDNS0>
1293
1294Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension
1295for DNS. This extension is generally useful to reduce DNS traffic, but
1296some (broken) firewalls drop such DNS packets, which is why it is off by
1297default.
1298
1299Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce
1300EDNS0 in its DNS requests.
1301
1302=item C<PERL_ANYEVENT_MAX_FORKS>
1303
1304The maximum number of child processes that C<AnyEvent::Util::fork_call>
1305will create in parallel.
1306
1307=back
1308 1335
1309=head1 EXAMPLE PROGRAM 1336=head1 EXAMPLE PROGRAM
1310 1337
1311The following program uses an I/O watcher to read data from STDIN, a timer 1338The following program uses an I/O watcher to read data from STDIN, a timer
1312to display a message once per second, and a condition variable to quit the 1339to display a message once per second, and a condition variable to quit the

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines