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.81 by root, Fri Apr 25 13:05:17 2008 UTC vs.
Revision 1.82 by root, Fri Apr 25 13:32:39 2008 UTC

147events. C<poll> must be a string that is either C<r> or C<w>, which 147events. C<poll> must be a string that is either C<r> or C<w>, which
148creates a watcher waiting for "r"eadable or "w"ritable events, 148creates a watcher waiting for "r"eadable or "w"ritable events,
149respectively. C<cb> is the callback to invoke each time the file handle 149respectively. C<cb> is the callback to invoke each time the file handle
150becomes ready. 150becomes ready.
151 151
152As long as the I/O watcher exists it will keep the file descriptor or a 152The I/O watcher might use the underlying file descriptor or a copy of it.
153copy of it alive/open.
154
155It is not allowed to close a file handle as long as any watcher is active 153It is not allowed to close a file handle as long as any watcher is active
156on the underlying file descriptor. 154on the underlying file descriptor.
157 155
158Some event loops issue spurious readyness notifications, so you should 156Some event loops issue spurious readyness notifications, so you should
159always use non-blocking calls when reading/writing from/to your file 157always use non-blocking calls when reading/writing from/to your file
253watches for any child process exit). The watcher will trigger as often 251watches for any child process exit). The watcher will trigger as often
254as status change for the child are received. This works by installing a 252as status change for the child are received. This works by installing a
255signal handler for C<SIGCHLD>. The callback will be called with the pid 253signal handler for C<SIGCHLD>. The callback will be called with the pid
256and exit status (as returned by waitpid). 254and exit status (as returned by waitpid).
257 255
258Example: wait for pid 1333 256There is a slight catch to child watchers, however: you usually start them
257I<after> the child process was created, and this means the process could
258have exited already (and no SIGCHLD will be sent anymore).
259
260Not all event models handle this correctly (POE doesn't), but even for
261event models that I<do> handle this correctly, they usually need to be
262loaded before the process exits (i.e. before you fork in the first place).
263
264This means you cannot create a child watcher as the very first thing in an
265AnyEvent program, you I<have> to create at least one watcher before you
266C<fork> the child (alternatively, you can call C<AnyEvent::detect>).
267
268Example: fork a process and wait for it
269
270 my $done = AnyEvent->condvar;
271
272 AnyEvent::detect; # force event module to be initialised
273
274 my $pid = fork or exit 5;
259 275
260 my $w = AnyEvent->child ( 276 my $w = AnyEvent->child (
261 pid => 1333, 277 pid => $pid,
262 cb => sub { 278 cb => sub {
263 my ($pid, $status) = @_; 279 my ($pid, $status) = @_;
264 warn "pid $pid exited with status $status"; 280 warn "pid $pid exited with status $status";
281 $done->broadcast;
265 }, 282 },
266 ); 283 );
284
285 # do something else, then wait for process exit
286 $done->wait;
267 287
268=head2 CONDITION VARIABLES 288=head2 CONDITION VARIABLES
269 289
270Condition variables can be created by calling the C<< AnyEvent->condvar >> 290Condition variables can be created by calling the C<< AnyEvent->condvar >>
271method without any arguments. 291method without any arguments.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines