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