… | |
… | |
132 | These watchers are normal Perl objects with normal Perl lifetime. After |
132 | These watchers are normal Perl objects with normal Perl lifetime. After |
133 | creating a watcher it will immediately "watch" for events and invoke the |
133 | creating a watcher it will immediately "watch" for events and invoke the |
134 | callback when the event occurs (of course, only when the event model is |
134 | callback when the event occurs (of course, only when the event model is |
135 | in control). |
135 | in control). |
136 | |
136 | |
|
|
137 | Note that callbacks must not permanently change global variables |
|
|
138 | potentially in use by the event loop (such as $_ or $[) and that |
|
|
139 | callbacks must not "die". The former is good programming practise in |
|
|
140 | Perl and the latter stems from the fact that exception handling differs |
|
|
141 | widely between event loops. |
|
|
142 | |
137 | To disable the watcher you have to destroy it (e.g. by setting the |
143 | To disable the watcher you have to destroy it (e.g. by setting the |
138 | variable you store it in to "undef" or otherwise deleting all references |
144 | variable you store it in to "undef" or otherwise deleting all references |
139 | to it). |
145 | to it). |
140 | |
146 | |
141 | All watchers are created by calling a method on the "AnyEvent" class. |
147 | All watchers are created by calling a method on the "AnyEvent" class. |
… | |
… | |
156 | |
162 | |
157 | I/O WATCHERS |
163 | I/O WATCHERS |
158 | You can create an I/O watcher by calling the "AnyEvent->io" method with |
164 | You can create an I/O watcher by calling the "AnyEvent->io" method with |
159 | the following mandatory key-value pairs as arguments: |
165 | the following mandatory key-value pairs as arguments: |
160 | |
166 | |
161 | "fh" the Perl *file handle* (*not* file descriptor) to watch for events |
167 | "fh" is the Perl *file handle* (*not* file descriptor) to watch for |
162 | (AnyEvent might or might not keep a reference to this file handle). |
168 | events (AnyEvent might or might not keep a reference to this file |
|
|
169 | handle). Note that only file handles pointing to things for which |
|
|
170 | non-blocking operation makes sense are allowed. This includes sockets, |
|
|
171 | most character devices, pipes, fifos and so on, but not for example |
|
|
172 | files or block devices. |
|
|
173 | |
163 | "poll" must be a string that is either "r" or "w", which creates a |
174 | "poll" must be a string that is either "r" or "w", which creates a |
164 | watcher waiting for "r"eadable or "w"ritable events, respectively. "cb" |
175 | watcher waiting for "r"eadable or "w"ritable events, respectively. |
|
|
176 | |
165 | is the callback to invoke each time the file handle becomes ready. |
177 | "cb" is the callback to invoke each time the file handle becomes ready. |
166 | |
178 | |
167 | Although the callback might get passed parameters, their value and |
179 | Although the callback might get passed parameters, their value and |
168 | presence is undefined and you cannot rely on them. Portable AnyEvent |
180 | presence is undefined and you cannot rely on them. Portable AnyEvent |
169 | callbacks cannot use arguments passed to I/O watcher callbacks. |
181 | callbacks cannot use arguments passed to I/O watcher callbacks. |
170 | |
182 | |
… | |
… | |
870 | mentioned will be used, and preference will be given to protocols |
882 | mentioned will be used, and preference will be given to protocols |
871 | mentioned earlier in the list. |
883 | mentioned earlier in the list. |
872 | |
884 | |
873 | This variable can effectively be used for denial-of-service attacks |
885 | This variable can effectively be used for denial-of-service attacks |
874 | against local programs (e.g. when setuid), although the impact is |
886 | against local programs (e.g. when setuid), although the impact is |
875 | likely small, as the program has to handle connection errors |
887 | likely small, as the program has to handle conenction and other |
876 | already- |
888 | failures anyways. |
877 | |
889 | |
878 | Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over |
890 | Examples: "PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6" - prefer IPv4 over |
879 | IPv6, but support both and try to use both. |
891 | IPv6, but support both and try to use both. |
880 | "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to |
892 | "PERL_ANYEVENT_PROTOCOLS=ipv4" - only support IPv4, never try to |
881 | resolve or contact IPv6 addresses. |
893 | resolve or contact IPv6 addresses. |
… | |
… | |
1131 | *destroy* is the time, in microseconds, that it takes to destroy a |
1143 | *destroy* is the time, in microseconds, that it takes to destroy a |
1132 | single watcher. |
1144 | single watcher. |
1133 | |
1145 | |
1134 | Results |
1146 | Results |
1135 | name watchers bytes create invoke destroy comment |
1147 | name watchers bytes create invoke destroy comment |
1136 | EV/EV 400000 244 0.56 0.46 0.31 EV native interface |
1148 | EV/EV 400000 224 0.47 0.35 0.27 EV native interface |
1137 | EV/Any 100000 244 2.50 0.46 0.29 EV + AnyEvent watchers |
1149 | EV/Any 100000 224 2.88 0.34 0.27 EV + AnyEvent watchers |
1138 | CoroEV/Any 100000 244 2.49 0.44 0.29 coroutines + Coro::Signal |
1150 | CoroEV/Any 100000 224 2.85 0.35 0.28 coroutines + Coro::Signal |
1139 | Perl/Any 100000 513 4.92 0.87 1.12 pure perl implementation |
1151 | Perl/Any 100000 452 4.13 0.73 0.95 pure perl implementation |
1140 | Event/Event 16000 516 31.88 31.30 0.85 Event native interface |
1152 | Event/Event 16000 517 32.20 31.80 0.81 Event native interface |
1141 | Event/Any 16000 590 35.75 31.42 1.08 Event + AnyEvent watchers |
1153 | Event/Any 16000 590 35.85 31.55 1.06 Event + AnyEvent watchers |
1142 | Glib/Any 16000 1357 98.22 12.41 54.00 quadratic behaviour |
1154 | Glib/Any 16000 1357 102.33 12.31 51.00 quadratic behaviour |
1143 | Tk/Any 2000 1860 26.97 67.98 14.00 SEGV with >> 2000 watchers |
1155 | Tk/Any 2000 1860 27.20 66.31 14.00 SEGV with >> 2000 watchers |
1144 | POE/Event 2000 6644 108.64 736.02 14.73 via POE::Loop::Event |
1156 | POE/Event 2000 6328 109.99 751.67 14.02 via POE::Loop::Event |
1145 | POE/Select 2000 6343 94.13 809.12 565.96 via POE::Loop::Select |
1157 | POE/Select 2000 6027 94.54 809.13 579.80 via POE::Loop::Select |
1146 | |
1158 | |
1147 | Discussion |
1159 | Discussion |
1148 | The benchmark does *not* measure scalability of the event loop very |
1160 | The benchmark does *not* measure scalability of the event loop very |
1149 | well. For example, a select-based event loop (such as the pure perl one) |
1161 | well. For example, a select-based event loop (such as the pure perl one) |
1150 | can never compete with an event loop that uses epoll when the number of |
1162 | can never compete with an event loop that uses epoll when the number of |
… | |
… | |
1387 | |
1399 | |
1388 | BUGS |
1400 | BUGS |
1389 | Perl 5.8 has numerous memleaks that sometimes hit this module and are |
1401 | Perl 5.8 has numerous memleaks that sometimes hit this module and are |
1390 | hard to work around. If you suffer from memleaks, first upgrade to Perl |
1402 | hard to work around. If you suffer from memleaks, first upgrade to Perl |
1391 | 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other |
1403 | 5.10 and check wether the leaks still show up. (Perl 5.10.0 has other |
1392 | annoying mamleaks, such as leaking on "map" and "grep" but it is usually |
1404 | annoying memleaks, such as leaking on "map" and "grep" but it is usually |
1393 | not as pronounced). |
1405 | not as pronounced). |
1394 | |
1406 | |
1395 | SEE ALSO |
1407 | SEE ALSO |
1396 | Utility functions: AnyEvent::Util. |
1408 | Utility functions: AnyEvent::Util. |
1397 | |
1409 | |