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