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.84 by root, Fri Apr 25 13:48:42 2008 UTC vs.
Revision 1.90 by root, Fri Apr 25 14:24:29 2008 UTC

141=head2 I/O WATCHERS 141=head2 I/O WATCHERS
142 142
143You can create an I/O watcher by calling the C<< AnyEvent->io >> method 143You can create an I/O watcher by calling the C<< AnyEvent->io >> method
144with the following mandatory key-value pairs as arguments: 144with the following mandatory key-value pairs as arguments:
145 145
146C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for 146C<fh> the Perl I<file handle> (I<not> file descriptor) to watch
147events. C<poll> must be a string that is either C<r> or C<w>, which 147for events. C<poll> must be a string that is either C<r> or C<w>,
148creates a watcher waiting for "r"eadable or "w"ritable events, 148which creates 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
152Although the callback might get passed parameters, their value and
153presence is undefined and you cannot rely on them. Portable AnyEvent
154callbacks cannot use arguments passed to I/O watcher callbacks.
151 155
152The I/O watcher might use the underlying file descriptor or a copy of it. 156The I/O watcher might use the underlying file descriptor or a copy of it.
153You must not close a file handle as long as any watcher is active on the 157You must not close a file handle as long as any watcher is active on the
154underlying file descriptor. 158underlying file descriptor.
155 159
156Some event loops issue spurious readyness notifications, so you should 160Some event loops issue spurious readyness notifications, so you should
157always use non-blocking calls when reading/writing from/to your file 161always use non-blocking calls when reading/writing from/to your file
158handles. 162handles.
159
160Although the callback might get passed parameters, their value and
161presence is undefined and you cannot rely on them. Portable AnyEvent
162callbacks cannot use arguments passed to I/O watcher callbacks.
163 163
164Example: 164Example:
165 165
166 # wait for readability of STDIN, then read a line and disable the watcher 166 # wait for readability of STDIN, then read a line and disable the watcher
167 my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { 167 my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub {
174 174
175You can create a time watcher by calling the C<< AnyEvent->timer >> 175You can create a time watcher by calling the C<< AnyEvent->timer >>
176method with the following mandatory arguments: 176method with the following mandatory arguments:
177 177
178C<after> specifies after how many seconds (fractional values are 178C<after> specifies after how many seconds (fractional values are
179supported) should the timer activate. C<cb> the callback to invoke in that 179supported) the callback should be invoked. C<cb> is the callback to invoke
180case. 180in that case.
181
182Although the callback might get passed parameters, their value and
183presence is undefined and you cannot rely on them. Portable AnyEvent
184callbacks cannot use arguments passed to time watcher callbacks.
181 185
182The timer callback will be invoked at most once: if you want a repeating 186The timer callback will be invoked at most once: if you want a repeating
183timer you have to create a new watcher (this is a limitation by both Tk 187timer you have to create a new watcher (this is a limitation by both Tk
184and Glib). 188and Glib).
185
186Although the callback might get passed parameters, their value and
187presence is undefined and you cannot rely on them. Portable AnyEvent
188callbacks cannot use arguments passed to time watcher callbacks.
189 189
190Example: 190Example:
191 191
192 # fire an event after 7.7 seconds 192 # fire an event after 7.7 seconds
193 my $w = AnyEvent->timer (after => 7.7, cb => sub { 193 my $w = AnyEvent->timer (after => 7.7, cb => sub {
234 234
235You can watch for signals using a signal watcher, C<signal> is the signal 235You can watch for signals using a signal watcher, C<signal> is the signal
236I<name> without any C<SIG> prefix, C<cb> is the Perl callback to 236I<name> without any C<SIG> prefix, C<cb> is the Perl callback to
237be invoked whenever a signal occurs. 237be invoked whenever a signal occurs.
238 238
239Although the callback might get passed parameters, their value and
240presence is undefined and you cannot rely on them. Portable AnyEvent
241callbacks cannot use arguments passed to signal watcher callbacks.
242
239Multiple signal occurances can be clumped together into one callback 243Multiple signal occurances can be clumped together into one callback
240invocation, and callback invocation will be synchronous. synchronous means 244invocation, and callback invocation will be synchronous. synchronous means
241that it might take a while until the signal gets handled by the process, 245that it might take a while until the signal gets handled by the process,
242but it is guarenteed not to interrupt any other callbacks. 246but it is guarenteed not to interrupt any other callbacks.
243 247
257 261
258The child process is specified by the C<pid> argument (if set to C<0>, it 262The child process is specified by the C<pid> argument (if set to C<0>, it
259watches for any child process exit). The watcher will trigger as often 263watches for any child process exit). The watcher will trigger as often
260as status change for the child are received. This works by installing a 264as status change for the child are received. This works by installing a
261signal handler for C<SIGCHLD>. The callback will be called with the pid 265signal handler for C<SIGCHLD>. The callback will be called with the pid
262and exit status (as returned by waitpid). 266and exit status (as returned by waitpid), so unlike other watcher types,
267you I<can> rely on child watcher callback arguments.
263 268
264There is a slight catch to child watchers, however: you usually start them 269There is a slight catch to child watchers, however: you usually start them
265I<after> the child process was created, and this means the process could 270I<after> the child process was created, and this means the process could
266have exited already (and no SIGCHLD will be sent anymore). 271have exited already (and no SIGCHLD will be sent anymore).
267 272
959maximal/minimal, respectively. Even when going through AnyEvent, it uses 964maximal/minimal, respectively. Even when going through AnyEvent, it uses
960far less memory than any other event loop and is still faster than Event 965far less memory than any other event loop and is still faster than Event
961natively. 966natively.
962 967
963The pure perl implementation is hit in a few sweet spots (both the 968The pure perl implementation is hit in a few sweet spots (both the
964zero timeout and the use of a single fd hit optimisations in the perl 969constant timeout and the use of a single fd hit optimisations in the perl
965interpreter and the backend itself, and all watchers become ready at the 970interpreter and the backend itself). Nevertheless this shows that it
966same time). Nevertheless this shows that it adds very little overhead in 971adds very little overhead in itself. Like any select-based backend its
967itself. Like any select-based backend its performance becomes really bad 972performance becomes really bad with lots of file descriptors (and few of
968with lots of file descriptors (and few of them active), of course, but 973them active), of course, but this was not subject of this benchmark.
969this was not subject of this benchmark.
970 974
971The C<Event> module has a relatively high setup and callback invocation cost, 975The C<Event> module has a relatively high setup and callback invocation
972but overall scores on the third place. 976cost, but overall scores in on the third place.
973 977
974C<Glib>'s memory usage is quite a bit bit higher, but it features a 978C<Glib>'s memory usage is quite a bit higher, but it features a
975faster callback invocation and overall ends up in the same class as 979faster callback invocation and overall ends up in the same class as
976C<Event>. However, Glib scales extremely badly, doubling the number of 980C<Event>. However, Glib scales extremely badly, doubling the number of
977watchers increases the processing time by more than a factor of four, 981watchers increases the processing time by more than a factor of four,
978making it completely unusable when using larger numbers of watchers 982making it completely unusable when using larger numbers of watchers
979(note that only a single file descriptor was used in the benchmark, so 983(note that only a single file descriptor was used in the benchmark, so
982The C<Tk> adaptor works relatively well. The fact that it crashes with 986The C<Tk> adaptor works relatively well. The fact that it crashes with
983more than 2000 watchers is a big setback, however, as correctness takes 987more than 2000 watchers is a big setback, however, as correctness takes
984precedence over speed. Nevertheless, its performance is surprising, as the 988precedence over speed. Nevertheless, its performance is surprising, as the
985file descriptor is dup()ed for each watcher. This shows that the dup() 989file descriptor is dup()ed for each watcher. This shows that the dup()
986employed by some adaptors is not a big performance issue (it does incur a 990employed by some adaptors is not a big performance issue (it does incur a
987hidden memory cost inside the kernel, though, that is not reflected in the 991hidden memory cost inside the kernel which is not reflected in the figures
988figures above). 992above).
989 993
990C<POE>, regardless of underlying event loop (wether using its pure perl 994C<POE>, regardless of underlying event loop (whether using its pure
991select-based backend or the Event module) shows abysmal performance and 995perl select-based backend or the Event module, the POE-EV backend
996couldn't be tested because it wasn't working) shows abysmal performance
992memory usage: Watchers use almost 30 times as much memory as EV watchers, 997and memory usage: Watchers use almost 30 times as much memory as
993and 10 times as much memory as both Event or EV via AnyEvent. Watcher 998EV watchers, and 10 times as much memory as Event (the high memory
999requirements are caused by requiring a session for each watcher). Watcher
994invocation is almost 900 times slower than with AnyEvent's pure perl 1000invocation speed is almost 900 times slower than with AnyEvent's pure perl
995implementation. The design of the POE adaptor class in AnyEvent can not 1001implementation. The design of the POE adaptor class in AnyEvent can not
996really account for this, as session creation overhead is small compared 1002really account for this, as session creation overhead is small compared
997to execution of the state machine, which is coded pretty optimally within 1003to execution of the state machine, which is coded pretty optimally within
998L<AnyEvent::Impl::POE>. POE simply seems to be abysmally slow. 1004L<AnyEvent::Impl::POE>. POE simply seems to be abysmally slow.
999 1005
1000=head2 Summary 1006=head2 Summary
1001 1007
1008=over 4
1009
1002Using EV through AnyEvent is faster than any other event loop, but most 1010=item * Using EV through AnyEvent is faster than any other event loop
1003event loops have acceptable performance with or without AnyEvent. 1011(even when used without AnyEvent), but most event loops have acceptable
1012performance with or without AnyEvent.
1004 1013
1005The overhead AnyEvent adds is usually much smaller than the overhead of 1014=item * The overhead AnyEvent adds is usually much smaller than the overhead of
1006the actual event loop, only with extremely fast event loops such as the EV 1015the actual event loop, only with extremely fast event loops such as EV
1007adds AnyEvent significant overhead. 1016adds AnyEvent significant overhead.
1008 1017
1009And you should simply avoid POE like the plague if you want performance or 1018=item * You should avoid POE like the plague if you want performance or
1010reasonable memory usage. 1019reasonable memory usage.
1020
1021=back
1011 1022
1012 1023
1013=head1 FORK 1024=head1 FORK
1014 1025
1015Most event libraries are not fork-safe. The ones who are usually are 1026Most event libraries are not fork-safe. The ones who are usually are

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines