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.20 by root, Mon Dec 11 01:16:09 2006 UTC vs.
Revision 1.31 by root, Fri Nov 2 19:20:36 2007 UTC

161 161
162=head2 SIGNAL WATCHERS 162=head2 SIGNAL WATCHERS
163 163
164You can listen for signals using a signal watcher, C<signal> is the signal 164You can listen for signals using a signal watcher, C<signal> is the signal
165I<name> without any C<SIG> prefix. Multiple signals events can be clumped 165I<name> without any C<SIG> prefix. Multiple signals events can be clumped
166together into one callback invocation, and callbakc invocation might or 166together into one callback invocation, and callback invocation might or
167might not be asynchronous. 167might not be asynchronous.
168 168
169These watchers might use C<%SIG>, so programs overwriting those signals 169These watchers might use C<%SIG>, so programs overwriting those signals
170directly will likely not work correctly. 170directly will likely not work correctly.
171 171
174 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); 174 my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 });
175 175
176=head2 CHILD PROCESS WATCHERS 176=head2 CHILD PROCESS WATCHERS
177 177
178You can also listen for the status of a child process specified by the 178You can also listen for the status of a child process specified by the
179C<pid> argument. The watcher will only trigger once. This works by 179C<pid> argument (or any child if the pid argument is 0). The watcher will
180trigger as often as status change for the child are received. This works
180installing a signal handler for C<SIGCHLD>. 181by installing a signal handler for C<SIGCHLD>.
181 182
182Example: wait for pid 1333 183Example: wait for pid 1333
183 184
184 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); 185 my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" });
185 186
195C<AnyEvent::Impl:xxx> modules, but can be any other class in the case 196C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
196AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). 197AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
197 198
198The known classes so far are: 199The known classes so far are:
199 200
201 EV::AnyEvent based on EV (an interface to libev, best choice)
200 AnyEvent::Impl::Coro based on Coro::Event, best choise. 202 AnyEvent::Impl::Coro based on Coro::Event, second best choice.
201 AnyEvent::Impl::Event based on Event, also best choice :) 203 AnyEvent::Impl::Event based on Event, also second best choice :)
202 AnyEvent::Impl::Glib based on Glib, second-best choice. 204 AnyEvent::Impl::Glib based on Glib, second-best choice.
203 AnyEvent::Impl::Tk based on Tk, very bad choice. 205 AnyEvent::Impl::Tk based on Tk, very bad choice.
204 AnyEvent::Impl::Perl pure-perl implementation, inefficient. 206 AnyEvent::Impl::Perl pure-perl implementation, inefficient.
205 207
206=item AnyEvent::detect 208=item AnyEvent::detect
245 247
246package AnyEvent; 248package AnyEvent;
247 249
248no warnings; 250no warnings;
249use strict; 251use strict;
252
250use Carp; 253use Carp;
251 254
252our $VERSION = '2.5'; 255our $VERSION = '2.55';
253our $MODEL; 256our $MODEL;
254 257
255our $AUTOLOAD; 258our $AUTOLOAD;
256our @ISA; 259our @ISA;
257 260
259 262
260our @REGISTRY; 263our @REGISTRY;
261 264
262my @models = ( 265my @models = (
263 [Coro::Event:: => AnyEvent::Impl::Coro::], 266 [Coro::Event:: => AnyEvent::Impl::Coro::],
267 [EV:: => EV::AnyEvent::],
264 [Event:: => AnyEvent::Impl::Event::], 268 [Event:: => AnyEvent::Impl::Event::],
265 [Glib:: => AnyEvent::Impl::Glib::], 269 [Glib:: => AnyEvent::Impl::Glib::],
266 [Tk:: => AnyEvent::Impl::Tk::], 270 [Tk:: => AnyEvent::Impl::Tk::],
267 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 271 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
268); 272);
288 unless ($MODEL) { 292 unless ($MODEL) {
289 # try to load a model 293 # try to load a model
290 294
291 for (@REGISTRY, @models) { 295 for (@REGISTRY, @models) {
292 my ($package, $model) = @$_; 296 my ($package, $model) = @$_;
297 if (eval "require $package"
298 and ${"$package\::VERSION"} > 0
293 if (eval "require $model") { 299 and eval "require $model") {
294 $MODEL = $model; 300 $MODEL = $model;
295 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 301 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
296 last; 302 last;
297 } 303 }
298 } 304 }
369our $PID_IDLE; 375our $PID_IDLE;
370our $WNOHANG; 376our $WNOHANG;
371 377
372sub _child_wait { 378sub _child_wait {
373 while (0 < (my $pid = waitpid -1, $WNOHANG)) { 379 while (0 < (my $pid = waitpid -1, $WNOHANG)) {
374 $_->() for values %{ (delete $PID_CB{$pid}) || {} }; 380 $_->() for (values %{ $PID_CB{$pid} || {} }),
381 (values %{ $PID_CB{0} || {} });
375 } 382 }
376 383
377 undef $PID_IDLE; 384 undef $PID_IDLE;
378} 385}
379 386
380sub child { 387sub child {
381 my (undef, %arg) = @_; 388 my (undef, %arg) = @_;
382 389
383 my $pid = uc $arg{pid} 390 defined (my $pid = $arg{pid} + 0)
384 or Carp::croak "required option 'pid' is missing"; 391 or Carp::croak "required option 'pid' is missing";
385 392
386 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 393 $PID_CB{$pid}{$arg{cb}} = $arg{cb};
387 394
388 unless ($WNOHANG) { 395 unless ($WNOHANG) {
389 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
390 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; 396 $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1;
391 } 397 }
392 398
399 unless ($CHLD_W) {
400 $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait);
393 # child could be a zombie already 401 # child could be a zombie already
394 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); 402 $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait);
403 }
395 404
396 bless [$pid, $arg{cb}], "AnyEvent::Base::Child" 405 bless [$pid, $arg{cb}], "AnyEvent::Base::Child"
397} 406}
398 407
399sub AnyEvent::Base::Child::DESTROY { 408sub AnyEvent::Base::Child::DESTROY {
435I<rxvt-unicode> distribution. 444I<rxvt-unicode> distribution.
436 445
437I<rxvt-unicode> also cheats a bit by not providing blocking access to 446I<rxvt-unicode> also cheats a bit by not providing blocking access to
438condition variables: code blocking while waiting for a condition will 447condition variables: code blocking while waiting for a condition will
439C<die>. This still works with most modules/usages, and blocking calls must 448C<die>. This still works with most modules/usages, and blocking calls must
440not be in an interactive appliation, so it makes sense. 449not be in an interactive application, so it makes sense.
441 450
442=head1 ENVIRONMENT VARIABLES 451=head1 ENVIRONMENT VARIABLES
443 452
444The following environment variables are used by this module: 453The following environment variables are used by this module:
445 454

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines