… | |
… | |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
71 | the following mandatory arguments: |
71 | the following mandatory arguments: |
72 | |
72 | |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback |
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback |
76 | to invoke everytime the filehandle becomes ready. |
76 | to invoke everytime the filehandle becomes ready. |
77 | |
77 | |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
79 | a socket you can have one r + one w, not any more (limitation comes from |
79 | a socket you can have one r + one w, not any more (limitation comes from |
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
… | |
… | |
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. |
200 | EV::AnyEvent based on EV (an interface to libev, best choice) |
203 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
201 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
204 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
202 | AnyEvent::Impl::Event based on Event, also second best choice :) |
205 | AnyEvent::Impl::Event based on Event, also second best choice :) |
203 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
206 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
204 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
207 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
205 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
208 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
… | |
… | |
249 | no warnings; |
252 | no warnings; |
250 | use strict; |
253 | use strict; |
251 | |
254 | |
252 | use Carp; |
255 | use Carp; |
253 | |
256 | |
254 | our $VERSION = '2.55'; |
257 | our $VERSION = '2.6'; |
255 | our $MODEL; |
258 | our $MODEL; |
256 | |
259 | |
257 | our $AUTOLOAD; |
260 | our $AUTOLOAD; |
258 | our @ISA; |
261 | our @ISA; |
259 | |
262 | |
260 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
263 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
261 | |
264 | |
262 | our @REGISTRY; |
265 | our @REGISTRY; |
263 | |
266 | |
264 | my @models = ( |
267 | my @models = ( |
|
|
268 | [Coro::EV:: => AnyEvent::Impl::CoroEV::], |
|
|
269 | [EV:: => AnyEvent::Impl::EV::], |
265 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
270 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
266 | [EV:: => EV::AnyEvent::], |
|
|
267 | [Event:: => AnyEvent::Impl::Event::], |
271 | [Event:: => AnyEvent::Impl::Event::], |
268 | [Glib:: => AnyEvent::Impl::Glib::], |
272 | [Glib:: => AnyEvent::Impl::Glib::], |
269 | [Tk:: => AnyEvent::Impl::Tk::], |
273 | [Tk:: => AnyEvent::Impl::Tk::], |
270 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
274 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
271 | ); |
275 | ); |
… | |
… | |
301 | last; |
305 | last; |
302 | } |
306 | } |
303 | } |
307 | } |
304 | |
308 | |
305 | $MODEL |
309 | $MODEL |
306 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; |
310 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk."; |
307 | } |
311 | } |
308 | |
312 | |
309 | unshift @ISA, $MODEL; |
313 | unshift @ISA, $MODEL; |
310 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
314 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
311 | } |
315 | } |
… | |
… | |
373 | our $CHLD_W; |
377 | our $CHLD_W; |
374 | our $PID_IDLE; |
378 | our $PID_IDLE; |
375 | our $WNOHANG; |
379 | our $WNOHANG; |
376 | |
380 | |
377 | sub _child_wait { |
381 | sub _child_wait { |
378 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
382 | while (0 <= (my $pid = waitpid -1, $WNOHANG)) { |
379 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
383 | $_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
|
|
384 | (values %{ $PID_CB{0} || {} }); |
380 | } |
385 | } |
381 | |
386 | |
382 | undef $PID_IDLE; |
387 | undef $PID_IDLE; |
383 | } |
388 | } |
384 | |
389 | |
385 | sub child { |
390 | sub child { |
386 | my (undef, %arg) = @_; |
391 | my (undef, %arg) = @_; |
387 | |
392 | |
388 | my $pid = uc $arg{pid} |
393 | defined (my $pid = $arg{pid} + 0) |
389 | or Carp::croak "required option 'pid' is missing"; |
394 | or Carp::croak "required option 'pid' is missing"; |
390 | |
395 | |
391 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
396 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
392 | |
397 | |
393 | unless ($WNOHANG) { |
398 | unless ($WNOHANG) { |