… | |
… | |
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. |
… | |
… | |
906 | |
926 | |
907 | =head2 Results |
927 | =head2 Results |
908 | |
928 | |
909 | name watchers bytes create invoke destroy comment |
929 | name watchers bytes create invoke destroy comment |
910 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
930 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
911 | EV/Any 100000 610 3.52 0.91 0.75 EV + AnyEvent watchers |
931 | EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers |
912 | CoroEV/Any 100000 610 3.49 0.92 0.75 coroutines + Coro::Signal |
932 | CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal |
913 | Perl/Any 100000 513 4.91 0.92 1.15 pure perl implementation |
933 | Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation |
914 | Event/Event 16000 523 28.05 21.38 0.86 Event native interface |
934 | Event/Event 16000 516 31.88 31.30 0.85 Event native interface |
915 | Event/Any 16000 943 34.43 20.48 1.39 Event + AnyEvent watchers |
935 | Event/Any 16000 936 39.17 33.63 1.43 Event + AnyEvent watchers |
916 | Glib/Any 16000 1357 96.99 12.55 55.51 quadratic behaviour |
936 | Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour |
917 | Tk/Any 2000 1855 27.01 66.61 14.03 SEGV with >> 2000 watchers |
937 | Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers |
918 | POE/Event 2000 6644 108.15 768.19 14.33 via POE::Loop::Event |
938 | POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event |
919 | POE/Select 2000 6343 94.69 807.65 562.69 via POE::Loop::Select |
939 | POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select |
920 | |
940 | |
921 | =head2 Discussion |
941 | =head2 Discussion |
922 | |
942 | |
923 | The benchmark does I<not> measure scalability of the event loop very |
943 | The benchmark does I<not> measure scalability of the event loop very |
924 | well. For example, a select-based event loop (such as the pure perl one) |
944 | well. For example, a select-based event loop (such as the pure perl one) |