1 | =head1 => NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - provide framework for multiple event loops |
3 | AnyEvent - provide framework for multiple event loops |
4 | |
4 | |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
5 | EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops |
6 | |
6 | |
… | |
… | |
18 | |
18 | |
19 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
19 | my $w = AnyEvent->condvar; # stores whether a condition was flagged |
20 | $w->send; # wake up current and all future recv's |
20 | $w->send; # wake up current and all future recv's |
21 | $w->recv; # enters "main loop" till $condvar gets ->send |
21 | $w->recv; # enters "main loop" till $condvar gets ->send |
22 | |
22 | |
|
|
23 | =head1 INTRODUCTION/TUTORIAL |
|
|
24 | |
|
|
25 | This manpage is mainly a reference manual. If you are interested |
|
|
26 | in a tutorial or some gentle introduction, have a look at the |
|
|
27 | L<AnyEvent::Intro> manpage. |
|
|
28 | |
23 | =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) |
29 | =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) |
24 | |
30 | |
25 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
31 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
26 | nowadays. So what is different about AnyEvent? |
32 | nowadays. So what is different about AnyEvent? |
27 | |
33 | |
28 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
34 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
29 | policy> and AnyEvent is I<small and efficient>. |
35 | policy> and AnyEvent is I<small and efficient>. |
30 | |
36 | |
31 | First and foremost, I<AnyEvent is not an event model> itself, it only |
37 | First and foremost, I<AnyEvent is not an event model> itself, it only |
32 | interfaces to whatever event model the main program happens to use in a |
38 | interfaces to whatever event model the main program happens to use, in a |
33 | pragmatic way. For event models and certain classes of immortals alike, |
39 | pragmatic way. For event models and certain classes of immortals alike, |
34 | the statement "there can only be one" is a bitter reality: In general, |
40 | the statement "there can only be one" is a bitter reality: In general, |
35 | only one event loop can be active at the same time in a process. AnyEvent |
41 | only one event loop can be active at the same time in a process. AnyEvent |
36 | helps hiding the differences between those event loops. |
42 | cannot change this, but it can hide the differences between those event |
|
|
43 | loops. |
37 | |
44 | |
38 | The goal of AnyEvent is to offer module authors the ability to do event |
45 | The goal of AnyEvent is to offer module authors the ability to do event |
39 | programming (waiting for I/O or timer events) without subscribing to a |
46 | programming (waiting for I/O or timer events) without subscribing to a |
40 | religion, a way of living, and most importantly: without forcing your |
47 | religion, a way of living, and most importantly: without forcing your |
41 | module users into the same thing by forcing them to use the same event |
48 | module users into the same thing by forcing them to use the same event |
42 | model you use. |
49 | model you use. |
43 | |
50 | |
44 | For modules like POE or IO::Async (which is a total misnomer as it is |
51 | For modules like POE or IO::Async (which is a total misnomer as it is |
45 | actually doing all I/O I<synchronously>...), using them in your module is |
52 | actually doing all I/O I<synchronously>...), using them in your module is |
46 | like joining a cult: After you joined, you are dependent on them and you |
53 | like joining a cult: After you joined, you are dependent on them and you |
47 | cannot use anything else, as it is simply incompatible to everything that |
54 | cannot use anything else, as they are simply incompatible to everything |
48 | isn't itself. What's worse, all the potential users of your module are |
55 | that isn't them. What's worse, all the potential users of your |
49 | I<also> forced to use the same event loop you use. |
56 | module are I<also> forced to use the same event loop you use. |
50 | |
57 | |
51 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
58 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
52 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
59 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
53 | with the rest: POE + IO::Async? no go. Tk + Event? no go. Again: if |
60 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
54 | your module uses one of those, every user of your module has to use it, |
61 | your module uses one of those, every user of your module has to use it, |
55 | too. But if your module uses AnyEvent, it works transparently with all |
62 | too. But if your module uses AnyEvent, it works transparently with all |
56 | event models it supports (including stuff like POE and IO::Async, as long |
63 | event models it supports (including stuff like IO::Async, as long as those |
57 | as those use one of the supported event loops. It is trivial to add new |
64 | use one of the supported event loops. It is trivial to add new event loops |
58 | event loops to AnyEvent, too, so it is future-proof). |
65 | to AnyEvent, too, so it is future-proof). |
59 | |
66 | |
60 | In addition to being free of having to use I<the one and only true event |
67 | In addition to being free of having to use I<the one and only true event |
61 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
68 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
62 | modules, you get an enourmous amount of code and strict rules you have to |
69 | modules, you get an enormous amount of code and strict rules you have to |
63 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
70 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
64 | offering the functionality that is necessary, in as thin as a wrapper as |
71 | offering the functionality that is necessary, in as thin as a wrapper as |
65 | technically possible. |
72 | technically possible. |
66 | |
73 | |
|
|
74 | Of course, AnyEvent comes with a big (and fully optional!) toolbox |
|
|
75 | of useful functionality, such as an asynchronous DNS resolver, 100% |
|
|
76 | non-blocking connects (even with TLS/SSL, IPv6 and on broken platforms |
|
|
77 | such as Windows) and lots of real-world knowledge and workarounds for |
|
|
78 | platform bugs and differences. |
|
|
79 | |
67 | Of course, if you want lots of policy (this can arguably be somewhat |
80 | Now, if you I<do want> lots of policy (this can arguably be somewhat |
68 | useful) and you want to force your users to use the one and only event |
81 | useful) and you want to force your users to use the one and only event |
69 | model, you should I<not> use this module. |
82 | model, you should I<not> use this module. |
70 | |
83 | |
71 | =head1 DESCRIPTION |
84 | =head1 DESCRIPTION |
72 | |
85 | |
… | |
… | |
102 | starts using it, all bets are off. Maybe you should tell their authors to |
115 | starts using it, all bets are off. Maybe you should tell their authors to |
103 | use AnyEvent so their modules work together with others seamlessly... |
116 | use AnyEvent so their modules work together with others seamlessly... |
104 | |
117 | |
105 | The pure-perl implementation of AnyEvent is called |
118 | The pure-perl implementation of AnyEvent is called |
106 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
119 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
107 | explicitly. |
120 | explicitly and enjoy the high availability of that event loop :) |
108 | |
121 | |
109 | =head1 WATCHERS |
122 | =head1 WATCHERS |
110 | |
123 | |
111 | AnyEvent has the central concept of a I<watcher>, which is an object that |
124 | AnyEvent has the central concept of a I<watcher>, which is an object that |
112 | stores relevant data for each kind of event you are waiting for, such as |
125 | stores relevant data for each kind of event you are waiting for, such as |
113 | the callback to call, the filehandle to watch, etc. |
126 | the callback to call, the file handle to watch, etc. |
114 | |
127 | |
115 | These watchers are normal Perl objects with normal Perl lifetime. After |
128 | These watchers are normal Perl objects with normal Perl lifetime. After |
116 | creating a watcher it will immediately "watch" for events and invoke the |
129 | creating a watcher it will immediately "watch" for events and invoke the |
117 | callback when the event occurs (of course, only when the event model |
130 | callback when the event occurs (of course, only when the event model |
118 | is in control). |
131 | is in control). |
… | |
… | |
126 | Many watchers either are used with "recursion" (repeating timers for |
139 | Many watchers either are used with "recursion" (repeating timers for |
127 | example), or need to refer to their watcher object in other ways. |
140 | example), or need to refer to their watcher object in other ways. |
128 | |
141 | |
129 | An any way to achieve that is this pattern: |
142 | An any way to achieve that is this pattern: |
130 | |
143 | |
131 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
144 | my $w; $w = AnyEvent->type (arg => value ..., cb => sub { |
132 | # you can use $w here, for example to undef it |
145 | # you can use $w here, for example to undef it |
133 | undef $w; |
146 | undef $w; |
134 | }); |
147 | }); |
135 | |
148 | |
136 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
149 | Note that C<my $w; $w => combination. This is necessary because in Perl, |
137 | my variables are only visible after the statement in which they are |
150 | my variables are only visible after the statement in which they are |
138 | declared. |
151 | declared. |
139 | |
152 | |
140 | =head2 I/O WATCHERS |
153 | =head2 I/O WATCHERS |
141 | |
154 | |
142 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
155 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
143 | with the following mandatory key-value pairs as arguments: |
156 | with the following mandatory key-value pairs as arguments: |
144 | |
157 | |
145 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch |
158 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for events |
146 | for events. C<poll> must be a string that is either C<r> or C<w>, |
159 | (AnyEvent might or might not keep a reference to this file handle). C<poll> |
147 | which creates a watcher waiting for "r"eadable or "w"ritable events, |
160 | must be a string that is either C<r> or C<w>, which creates a watcher |
148 | respectively. C<cb> is the callback to invoke each time the file handle |
161 | waiting for "r"eadable or "w"ritable events, respectively. C<cb> is the |
149 | becomes ready. |
162 | callback to invoke each time the file handle becomes ready. |
150 | |
163 | |
151 | Although the callback might get passed parameters, their value and |
164 | Although the callback might get passed parameters, their value and |
152 | presence is undefined and you cannot rely on them. Portable AnyEvent |
165 | presence is undefined and you cannot rely on them. Portable AnyEvent |
153 | callbacks cannot use arguments passed to I/O watcher callbacks. |
166 | callbacks cannot use arguments passed to I/O watcher callbacks. |
154 | |
167 | |
… | |
… | |
158 | |
171 | |
159 | Some event loops issue spurious readyness notifications, so you should |
172 | Some event loops issue spurious readyness notifications, so you should |
160 | always use non-blocking calls when reading/writing from/to your file |
173 | always use non-blocking calls when reading/writing from/to your file |
161 | handles. |
174 | handles. |
162 | |
175 | |
163 | Example: |
|
|
164 | |
|
|
165 | # wait for readability of STDIN, then read a line and disable the watcher |
176 | Example: wait for readability of STDIN, then read a line and disable the |
|
|
177 | watcher. |
|
|
178 | |
166 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
179 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
167 | chomp (my $input = <STDIN>); |
180 | chomp (my $input = <STDIN>); |
168 | warn "read: $input\n"; |
181 | warn "read: $input\n"; |
169 | undef $w; |
182 | undef $w; |
170 | }); |
183 | }); |
… | |
… | |
180 | |
193 | |
181 | Although the callback might get passed parameters, their value and |
194 | Although the callback might get passed parameters, their value and |
182 | presence is undefined and you cannot rely on them. Portable AnyEvent |
195 | presence is undefined and you cannot rely on them. Portable AnyEvent |
183 | callbacks cannot use arguments passed to time watcher callbacks. |
196 | callbacks cannot use arguments passed to time watcher callbacks. |
184 | |
197 | |
185 | The timer callback will be invoked at most once: if you want a repeating |
198 | The callback will normally be invoked once only. If you specify another |
186 | timer you have to create a new watcher (this is a limitation by both Tk |
199 | parameter, C<interval>, as a strictly positive number (> 0), then the |
187 | and Glib). |
200 | callback will be invoked regularly at that interval (in fractional |
|
|
201 | seconds) after the first invocation. If C<interval> is specified with a |
|
|
202 | false value, then it is treated as if it were missing. |
188 | |
203 | |
189 | Example: |
204 | The callback will be rescheduled before invoking the callback, but no |
|
|
205 | attempt is done to avoid timer drift in most backends, so the interval is |
|
|
206 | only approximate. |
190 | |
207 | |
191 | # fire an event after 7.7 seconds |
208 | Example: fire an event after 7.7 seconds. |
|
|
209 | |
192 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
210 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
193 | warn "timeout\n"; |
211 | warn "timeout\n"; |
194 | }); |
212 | }); |
195 | |
213 | |
196 | # to cancel the timer: |
214 | # to cancel the timer: |
197 | undef $w; |
215 | undef $w; |
198 | |
216 | |
199 | Example 2: |
|
|
200 | |
|
|
201 | # fire an event after 0.5 seconds, then roughly every second |
217 | Example 2: fire an event after 0.5 seconds, then roughly every second. |
202 | my $w; |
|
|
203 | |
218 | |
204 | my $cb = sub { |
|
|
205 | # cancel the old timer while creating a new one |
|
|
206 | $w = AnyEvent->timer (after => 1, cb => $cb); |
219 | my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { |
|
|
220 | warn "timeout\n"; |
207 | }; |
221 | }; |
208 | |
|
|
209 | # start the "loop" by creating the first watcher |
|
|
210 | $w = AnyEvent->timer (after => 0.5, cb => $cb); |
|
|
211 | |
222 | |
212 | =head3 TIMING ISSUES |
223 | =head3 TIMING ISSUES |
213 | |
224 | |
214 | There are two ways to handle timers: based on real time (relative, "fire |
225 | There are two ways to handle timers: based on real time (relative, "fire |
215 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
226 | in 10 seconds") and based on wallclock time (absolute, "fire at 12 |
… | |
… | |
227 | timers. |
238 | timers. |
228 | |
239 | |
229 | AnyEvent always prefers relative timers, if available, matching the |
240 | AnyEvent always prefers relative timers, if available, matching the |
230 | AnyEvent API. |
241 | AnyEvent API. |
231 | |
242 | |
|
|
243 | AnyEvent has two additional methods that return the "current time": |
|
|
244 | |
|
|
245 | =over 4 |
|
|
246 | |
|
|
247 | =item AnyEvent->time |
|
|
248 | |
|
|
249 | This returns the "current wallclock time" as a fractional number of |
|
|
250 | seconds since the Epoch (the same thing as C<time> or C<Time::HiRes::time> |
|
|
251 | return, and the result is guaranteed to be compatible with those). |
|
|
252 | |
|
|
253 | It progresses independently of any event loop processing, i.e. each call |
|
|
254 | will check the system clock, which usually gets updated frequently. |
|
|
255 | |
|
|
256 | =item AnyEvent->now |
|
|
257 | |
|
|
258 | This also returns the "current wallclock time", but unlike C<time>, above, |
|
|
259 | this value might change only once per event loop iteration, depending on |
|
|
260 | the event loop (most return the same time as C<time>, above). This is the |
|
|
261 | time that AnyEvent's timers get scheduled against. |
|
|
262 | |
|
|
263 | I<In almost all cases (in all cases if you don't care), this is the |
|
|
264 | function to call when you want to know the current time.> |
|
|
265 | |
|
|
266 | This function is also often faster then C<< AnyEvent->time >>, and |
|
|
267 | thus the preferred method if you want some timestamp (for example, |
|
|
268 | L<AnyEvent::Handle> uses this to update it's activity timeouts). |
|
|
269 | |
|
|
270 | The rest of this section is only of relevance if you try to be very exact |
|
|
271 | with your timing, you can skip it without bad conscience. |
|
|
272 | |
|
|
273 | For a practical example of when these times differ, consider L<Event::Lib> |
|
|
274 | and L<EV> and the following set-up: |
|
|
275 | |
|
|
276 | The event loop is running and has just invoked one of your callback at |
|
|
277 | time=500 (assume no other callbacks delay processing). In your callback, |
|
|
278 | you wait a second by executing C<sleep 1> (blocking the process for a |
|
|
279 | second) and then (at time=501) you create a relative timer that fires |
|
|
280 | after three seconds. |
|
|
281 | |
|
|
282 | With L<Event::Lib>, C<< AnyEvent->time >> and C<< AnyEvent->now >> will |
|
|
283 | both return C<501>, because that is the current time, and the timer will |
|
|
284 | be scheduled to fire at time=504 (C<501> + C<3>). |
|
|
285 | |
|
|
286 | With L<EV>, C<< AnyEvent->time >> returns C<501> (as that is the current |
|
|
287 | time), but C<< AnyEvent->now >> returns C<500>, as that is the time the |
|
|
288 | last event processing phase started. With L<EV>, your timer gets scheduled |
|
|
289 | to run at time=503 (C<500> + C<3>). |
|
|
290 | |
|
|
291 | In one sense, L<Event::Lib> is more exact, as it uses the current time |
|
|
292 | regardless of any delays introduced by event processing. However, most |
|
|
293 | callbacks do not expect large delays in processing, so this causes a |
|
|
294 | higher drift (and a lot more system calls to get the current time). |
|
|
295 | |
|
|
296 | In another sense, L<EV> is more exact, as your timer will be scheduled at |
|
|
297 | the same time, regardless of how long event processing actually took. |
|
|
298 | |
|
|
299 | In either case, if you care (and in most cases, you don't), then you |
|
|
300 | can get whatever behaviour you want with any event loop, by taking the |
|
|
301 | difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into |
|
|
302 | account. |
|
|
303 | |
|
|
304 | =back |
|
|
305 | |
232 | =head2 SIGNAL WATCHERS |
306 | =head2 SIGNAL WATCHERS |
233 | |
307 | |
234 | You can watch for signals using a signal watcher, C<signal> is the signal |
308 | You can watch for signals using a signal watcher, C<signal> is the signal |
235 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
309 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
236 | be invoked whenever a signal occurs. |
310 | callback to be invoked whenever a signal occurs. |
237 | |
311 | |
238 | Although the callback might get passed parameters, their value and |
312 | Although the callback might get passed parameters, their value and |
239 | presence is undefined and you cannot rely on them. Portable AnyEvent |
313 | presence is undefined and you cannot rely on them. Portable AnyEvent |
240 | callbacks cannot use arguments passed to signal watcher callbacks. |
314 | callbacks cannot use arguments passed to signal watcher callbacks. |
241 | |
315 | |
242 | Multiple signal occurances can be clumped together into one callback |
316 | Multiple signal occurrences can be clumped together into one callback |
243 | invocation, and callback invocation will be synchronous. synchronous means |
317 | invocation, and callback invocation will be synchronous. Synchronous means |
244 | that it might take a while until the signal gets handled by the process, |
318 | that it might take a while until the signal gets handled by the process, |
245 | but it is guarenteed not to interrupt any other callbacks. |
319 | but it is guaranteed not to interrupt any other callbacks. |
246 | |
320 | |
247 | The main advantage of using these watchers is that you can share a signal |
321 | The main advantage of using these watchers is that you can share a signal |
248 | between multiple watchers. |
322 | between multiple watchers. |
249 | |
323 | |
250 | This watcher might use C<%SIG>, so programs overwriting those signals |
324 | This watcher might use C<%SIG>, so programs overwriting those signals |
… | |
… | |
277 | AnyEvent program, you I<have> to create at least one watcher before you |
351 | AnyEvent program, you I<have> to create at least one watcher before you |
278 | C<fork> the child (alternatively, you can call C<AnyEvent::detect>). |
352 | C<fork> the child (alternatively, you can call C<AnyEvent::detect>). |
279 | |
353 | |
280 | Example: fork a process and wait for it |
354 | Example: fork a process and wait for it |
281 | |
355 | |
282 | my $done = AnyEvent->condvar; |
356 | my $done = AnyEvent->condvar; |
283 | |
357 | |
284 | my $pid = fork or exit 5; |
358 | my $pid = fork or exit 5; |
285 | |
359 | |
286 | my $w = AnyEvent->child ( |
360 | my $w = AnyEvent->child ( |
287 | pid => $pid, |
361 | pid => $pid, |
288 | cb => sub { |
362 | cb => sub { |
289 | my ($pid, $status) = @_; |
363 | my ($pid, $status) = @_; |
290 | warn "pid $pid exited with status $status"; |
364 | warn "pid $pid exited with status $status"; |
291 | $done->send; |
365 | $done->send; |
292 | }, |
366 | }, |
293 | ); |
367 | ); |
294 | |
368 | |
295 | # do something else, then wait for process exit |
369 | # do something else, then wait for process exit |
296 | $done->recv; |
370 | $done->recv; |
297 | |
371 | |
298 | =head2 CONDITION VARIABLES |
372 | =head2 CONDITION VARIABLES |
299 | |
373 | |
300 | If you are familiar with some event loops you will know that all of them |
374 | If you are familiar with some event loops you will know that all of them |
301 | require you to run some blocking "loop", "run" or similar function that |
375 | require you to run some blocking "loop", "run" or similar function that |
… | |
… | |
310 | Condition variables can be created by calling the C<< AnyEvent->condvar |
384 | Condition variables can be created by calling the C<< AnyEvent->condvar |
311 | >> method, usually without arguments. The only argument pair allowed is |
385 | >> method, usually without arguments. The only argument pair allowed is |
312 | C<cb>, which specifies a callback to be called when the condition variable |
386 | C<cb>, which specifies a callback to be called when the condition variable |
313 | becomes true. |
387 | becomes true. |
314 | |
388 | |
315 | After creation, the conditon variable is "false" until it becomes "true" |
389 | After creation, the condition variable is "false" until it becomes "true" |
316 | by calling the C<send> method. |
390 | by calling the C<send> method (or calling the condition variable as if it |
|
|
391 | were a callback, read about the caveats in the description for the C<< |
|
|
392 | ->send >> method). |
317 | |
393 | |
318 | Condition variables are similar to callbacks, except that you can |
394 | Condition variables are similar to callbacks, except that you can |
319 | optionally wait for them. They can also be called merge points - points |
395 | optionally wait for them. They can also be called merge points - points |
320 | in time where multiple outstandign events have been processed. And yet |
396 | in time where multiple outstanding events have been processed. And yet |
321 | another way to call them is transations - each condition variable can be |
397 | another way to call them is transactions - each condition variable can be |
322 | used to represent a transaction, which finishes at some point and delivers |
398 | used to represent a transaction, which finishes at some point and delivers |
323 | a result. |
399 | a result. |
324 | |
400 | |
325 | Condition variables are very useful to signal that something has finished, |
401 | Condition variables are very useful to signal that something has finished, |
326 | for example, if you write a module that does asynchronous http requests, |
402 | for example, if you write a module that does asynchronous http requests, |
… | |
… | |
332 | you can block your main program until an event occurs - for example, you |
408 | you can block your main program until an event occurs - for example, you |
333 | could C<< ->recv >> in your main program until the user clicks the Quit |
409 | could C<< ->recv >> in your main program until the user clicks the Quit |
334 | button of your app, which would C<< ->send >> the "quit" event. |
410 | button of your app, which would C<< ->send >> the "quit" event. |
335 | |
411 | |
336 | Note that condition variables recurse into the event loop - if you have |
412 | Note that condition variables recurse into the event loop - if you have |
337 | two pieces of code that call C<< ->recv >> in a round-robbin fashion, you |
413 | two pieces of code that call C<< ->recv >> in a round-robin fashion, you |
338 | lose. Therefore, condition variables are good to export to your caller, but |
414 | lose. Therefore, condition variables are good to export to your caller, but |
339 | you should avoid making a blocking wait yourself, at least in callbacks, |
415 | you should avoid making a blocking wait yourself, at least in callbacks, |
340 | as this asks for trouble. |
416 | as this asks for trouble. |
341 | |
417 | |
342 | Condition variables are represented by hash refs in perl, and the keys |
418 | Condition variables are represented by hash refs in perl, and the keys |
… | |
… | |
347 | |
423 | |
348 | There are two "sides" to a condition variable - the "producer side" which |
424 | There are two "sides" to a condition variable - the "producer side" which |
349 | eventually calls C<< -> send >>, and the "consumer side", which waits |
425 | eventually calls C<< -> send >>, and the "consumer side", which waits |
350 | for the send to occur. |
426 | for the send to occur. |
351 | |
427 | |
352 | Example: |
428 | Example: wait for a timer. |
353 | |
429 | |
354 | # wait till the result is ready |
430 | # wait till the result is ready |
355 | my $result_ready = AnyEvent->condvar; |
431 | my $result_ready = AnyEvent->condvar; |
356 | |
432 | |
357 | # do something such as adding a timer |
433 | # do something such as adding a timer |
… | |
… | |
365 | |
441 | |
366 | # this "blocks" (while handling events) till the callback |
442 | # this "blocks" (while handling events) till the callback |
367 | # calls send |
443 | # calls send |
368 | $result_ready->recv; |
444 | $result_ready->recv; |
369 | |
445 | |
|
|
446 | Example: wait for a timer, but take advantage of the fact that |
|
|
447 | condition variables are also code references. |
|
|
448 | |
|
|
449 | my $done = AnyEvent->condvar; |
|
|
450 | my $delay = AnyEvent->timer (after => 5, cb => $done); |
|
|
451 | $done->recv; |
|
|
452 | |
370 | =head3 METHODS FOR PRODUCERS |
453 | =head3 METHODS FOR PRODUCERS |
371 | |
454 | |
372 | These methods should only be used by the producing side, i.e. the |
455 | These methods should only be used by the producing side, i.e. the |
373 | code/module that eventually sends the signal. Note that it is also |
456 | code/module that eventually sends the signal. Note that it is also |
374 | the producer side which creates the condvar in most cases, but it isn't |
457 | the producer side which creates the condvar in most cases, but it isn't |
… | |
… | |
385 | If a callback has been set on the condition variable, it is called |
468 | If a callback has been set on the condition variable, it is called |
386 | immediately from within send. |
469 | immediately from within send. |
387 | |
470 | |
388 | Any arguments passed to the C<send> call will be returned by all |
471 | Any arguments passed to the C<send> call will be returned by all |
389 | future C<< ->recv >> calls. |
472 | future C<< ->recv >> calls. |
|
|
473 | |
|
|
474 | Condition variables are overloaded so one can call them directly |
|
|
475 | (as a code reference). Calling them directly is the same as calling |
|
|
476 | C<send>. Note, however, that many C-based event loops do not handle |
|
|
477 | overloading, so as tempting as it may be, passing a condition variable |
|
|
478 | instead of a callback does not work. Both the pure perl and EV loops |
|
|
479 | support overloading, however, as well as all functions that use perl to |
|
|
480 | invoke a callback (as in L<AnyEvent::Socket> and L<AnyEvent::DNS> for |
|
|
481 | example). |
390 | |
482 | |
391 | =item $cv->croak ($error) |
483 | =item $cv->croak ($error) |
392 | |
484 | |
393 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
485 | Similar to send, but causes all call's to C<< ->recv >> to invoke |
394 | C<Carp::croak> with the given error message/object/scalar. |
486 | C<Carp::croak> with the given error message/object/scalar. |
… | |
… | |
443 | doesn't execute once). |
535 | doesn't execute once). |
444 | |
536 | |
445 | This is the general pattern when you "fan out" into multiple subrequests: |
537 | This is the general pattern when you "fan out" into multiple subrequests: |
446 | use an outer C<begin>/C<end> pair to set the callback and ensure C<end> |
538 | use an outer C<begin>/C<end> pair to set the callback and ensure C<end> |
447 | is called at least once, and then, for each subrequest you start, call |
539 | is called at least once, and then, for each subrequest you start, call |
448 | C<begin> and for eahc subrequest you finish, call C<end>. |
540 | C<begin> and for each subrequest you finish, call C<end>. |
449 | |
541 | |
450 | =back |
542 | =back |
451 | |
543 | |
452 | =head3 METHODS FOR CONSUMERS |
544 | =head3 METHODS FOR CONSUMERS |
453 | |
545 | |
… | |
… | |
475 | (programs might want to do that to stay interactive), so I<if you are |
567 | (programs might want to do that to stay interactive), so I<if you are |
476 | using this from a module, never require a blocking wait>, but let the |
568 | using this from a module, never require a blocking wait>, but let the |
477 | caller decide whether the call will block or not (for example, by coupling |
569 | caller decide whether the call will block or not (for example, by coupling |
478 | condition variables with some kind of request results and supporting |
570 | condition variables with some kind of request results and supporting |
479 | callbacks so the caller knows that getting the result will not block, |
571 | callbacks so the caller knows that getting the result will not block, |
480 | while still suppporting blocking waits if the caller so desires). |
572 | while still supporting blocking waits if the caller so desires). |
481 | |
573 | |
482 | Another reason I<never> to C<< ->recv >> in a module is that you cannot |
574 | Another reason I<never> to C<< ->recv >> in a module is that you cannot |
483 | sensibly have two C<< ->recv >>'s in parallel, as that would require |
575 | sensibly have two C<< ->recv >>'s in parallel, as that would require |
484 | multiple interpreters or coroutines/threads, none of which C<AnyEvent> |
576 | multiple interpreters or coroutines/threads, none of which C<AnyEvent> |
485 | can supply. |
577 | can supply. |
… | |
… | |
504 | |
596 | |
505 | This is a mutator function that returns the callback set and optionally |
597 | This is a mutator function that returns the callback set and optionally |
506 | replaces it before doing so. |
598 | replaces it before doing so. |
507 | |
599 | |
508 | The callback will be called when the condition becomes "true", i.e. when |
600 | The callback will be called when the condition becomes "true", i.e. when |
509 | C<send> or C<croak> are called. Calling C<recv> inside the callback |
601 | C<send> or C<croak> are called, with the only argument being the condition |
510 | or at any later time is guaranteed not to block. |
602 | variable itself. Calling C<recv> inside the callback or at any later time |
|
|
603 | is guaranteed not to block. |
511 | |
604 | |
512 | =back |
605 | =back |
513 | |
606 | |
514 | =head1 GLOBAL VARIABLES AND FUNCTIONS |
607 | =head1 GLOBAL VARIABLES AND FUNCTIONS |
515 | |
608 | |
… | |
… | |
601 | |
694 | |
602 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
695 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
603 | do anything special (it does not need to be event-based) and let AnyEvent |
696 | do anything special (it does not need to be event-based) and let AnyEvent |
604 | decide which implementation to chose if some module relies on it. |
697 | decide which implementation to chose if some module relies on it. |
605 | |
698 | |
606 | If the main program relies on a specific event model. For example, in |
699 | If the main program relies on a specific event model - for example, in |
607 | Gtk2 programs you have to rely on the Glib module. You should load the |
700 | Gtk2 programs you have to rely on the Glib module - you should load the |
608 | event module before loading AnyEvent or any module that uses it: generally |
701 | event module before loading AnyEvent or any module that uses it: generally |
609 | speaking, you should load it as early as possible. The reason is that |
702 | speaking, you should load it as early as possible. The reason is that |
610 | modules might create watchers when they are loaded, and AnyEvent will |
703 | modules might create watchers when they are loaded, and AnyEvent will |
611 | decide on the event model to use as soon as it creates watchers, and it |
704 | decide on the event model to use as soon as it creates watchers, and it |
612 | might chose the wrong one unless you load the correct one yourself. |
705 | might chose the wrong one unless you load the correct one yourself. |
613 | |
706 | |
614 | You can chose to use a rather inefficient pure-perl implementation by |
707 | You can chose to use a pure-perl implementation by loading the |
615 | loading the C<AnyEvent::Impl::Perl> module, which gives you similar |
708 | C<AnyEvent::Impl::Perl> module, which gives you similar behaviour |
616 | behaviour everywhere, but letting AnyEvent chose is generally better. |
709 | everywhere, but letting AnyEvent chose the model is generally better. |
|
|
710 | |
|
|
711 | =head2 MAINLOOP EMULATION |
|
|
712 | |
|
|
713 | Sometimes (often for short test scripts, or even standalone programs who |
|
|
714 | only want to use AnyEvent), you do not want to run a specific event loop. |
|
|
715 | |
|
|
716 | In that case, you can use a condition variable like this: |
|
|
717 | |
|
|
718 | AnyEvent->condvar->recv; |
|
|
719 | |
|
|
720 | This has the effect of entering the event loop and looping forever. |
|
|
721 | |
|
|
722 | Note that usually your program has some exit condition, in which case |
|
|
723 | it is better to use the "traditional" approach of storing a condition |
|
|
724 | variable somewhere, waiting for it, and sending it when the program should |
|
|
725 | exit cleanly. |
|
|
726 | |
617 | |
727 | |
618 | =head1 OTHER MODULES |
728 | =head1 OTHER MODULES |
619 | |
729 | |
620 | The following is a non-exhaustive list of additional modules that use |
730 | The following is a non-exhaustive list of additional modules that use |
621 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
731 | AnyEvent and can therefore be mixed easily with other AnyEvent modules |
… | |
… | |
627 | =item L<AnyEvent::Util> |
737 | =item L<AnyEvent::Util> |
628 | |
738 | |
629 | Contains various utility functions that replace often-used but blocking |
739 | Contains various utility functions that replace often-used but blocking |
630 | functions such as C<inet_aton> by event-/callback-based versions. |
740 | functions such as C<inet_aton> by event-/callback-based versions. |
631 | |
741 | |
632 | =item L<AnyEvent::Handle> |
|
|
633 | |
|
|
634 | Provide read and write buffers and manages watchers for reads and writes. |
|
|
635 | |
|
|
636 | =item L<AnyEvent::Socket> |
742 | =item L<AnyEvent::Socket> |
637 | |
743 | |
638 | Provides various utility functions for (internet protocol) sockets, |
744 | Provides various utility functions for (internet protocol) sockets, |
639 | addresses and name resolution. Also functions to create non-blocking tcp |
745 | addresses and name resolution. Also functions to create non-blocking tcp |
640 | connections or tcp servers, with IPv6 and SRV record support and more. |
746 | connections or tcp servers, with IPv6 and SRV record support and more. |
641 | |
747 | |
|
|
748 | =item L<AnyEvent::Handle> |
|
|
749 | |
|
|
750 | Provide read and write buffers, manages watchers for reads and writes, |
|
|
751 | supports raw and formatted I/O, I/O queued and fully transparent and |
|
|
752 | non-blocking SSL/TLS. |
|
|
753 | |
|
|
754 | =item L<AnyEvent::DNS> |
|
|
755 | |
|
|
756 | Provides rich asynchronous DNS resolver capabilities. |
|
|
757 | |
|
|
758 | =item L<AnyEvent::HTTP> |
|
|
759 | |
|
|
760 | A simple-to-use HTTP library that is capable of making a lot of concurrent |
|
|
761 | HTTP requests. |
|
|
762 | |
642 | =item L<AnyEvent::HTTPD> |
763 | =item L<AnyEvent::HTTPD> |
643 | |
764 | |
644 | Provides a simple web application server framework. |
765 | Provides a simple web application server framework. |
645 | |
766 | |
646 | =item L<AnyEvent::DNS> |
|
|
647 | |
|
|
648 | Provides rich asynchronous DNS resolver capabilities. |
|
|
649 | |
|
|
650 | =item L<AnyEvent::FastPing> |
767 | =item L<AnyEvent::FastPing> |
651 | |
768 | |
652 | The fastest ping in the west. |
769 | The fastest ping in the west. |
|
|
770 | |
|
|
771 | =item L<AnyEvent::DBI> |
|
|
772 | |
|
|
773 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
774 | |
|
|
775 | =item L<AnyEvent::AIO> |
|
|
776 | |
|
|
777 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
778 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
779 | together. |
|
|
780 | |
|
|
781 | =item L<AnyEvent::BDB> |
|
|
782 | |
|
|
783 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
784 | L<BDB> and AnyEvent together. |
|
|
785 | |
|
|
786 | =item L<AnyEvent::GPSD> |
|
|
787 | |
|
|
788 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
789 | |
|
|
790 | =item L<AnyEvent::IGS> |
|
|
791 | |
|
|
792 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
793 | L<App::IGS>). |
653 | |
794 | |
654 | =item L<Net::IRC3> |
795 | =item L<Net::IRC3> |
655 | |
796 | |
656 | AnyEvent based IRC client module family. |
797 | AnyEvent based IRC client module family. |
657 | |
798 | |
… | |
… | |
670 | |
811 | |
671 | =item L<Coro> |
812 | =item L<Coro> |
672 | |
813 | |
673 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
814 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
674 | |
815 | |
675 | =item L<AnyEvent::AIO>, L<IO::AIO> |
|
|
676 | |
|
|
677 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
678 | programmer. AnyEvent::AIO transparently fuses IO::AIO and AnyEvent |
|
|
679 | together. |
|
|
680 | |
|
|
681 | =item L<AnyEvent::BDB>, L<BDB> |
|
|
682 | |
|
|
683 | Truly asynchronous Berkeley DB access. AnyEvent::AIO transparently fuses |
|
|
684 | IO::AIO and AnyEvent together. |
|
|
685 | |
|
|
686 | =item L<IO::Lambda> |
816 | =item L<IO::Lambda> |
687 | |
817 | |
688 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
818 | The lambda approach to I/O - don't ask, look there. Can use AnyEvent. |
689 | |
819 | |
690 | =back |
820 | =back |
… | |
… | |
696 | no warnings; |
826 | no warnings; |
697 | use strict; |
827 | use strict; |
698 | |
828 | |
699 | use Carp; |
829 | use Carp; |
700 | |
830 | |
701 | our $VERSION = '3.6'; |
831 | our $VERSION = 4.2; |
702 | our $MODEL; |
832 | our $MODEL; |
703 | |
833 | |
704 | our $AUTOLOAD; |
834 | our $AUTOLOAD; |
705 | our @ISA; |
835 | our @ISA; |
706 | |
836 | |
|
|
837 | our @REGISTRY; |
|
|
838 | |
|
|
839 | our $WIN32; |
|
|
840 | |
|
|
841 | BEGIN { |
|
|
842 | my $win32 = ! ! ($^O =~ /mswin32/i); |
|
|
843 | eval "sub WIN32(){ $win32 }"; |
|
|
844 | } |
|
|
845 | |
707 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
846 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
708 | |
847 | |
709 | our @REGISTRY; |
848 | our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred |
710 | |
|
|
711 | our %PROTOCOL; # (ipv4|ipv6) => (1|2) |
|
|
712 | |
849 | |
713 | { |
850 | { |
714 | my $idx; |
851 | my $idx; |
715 | $PROTOCOL{$_} = ++$idx |
852 | $PROTOCOL{$_} = ++$idx |
|
|
853 | for reverse split /\s*,\s*/, |
716 | for split /\s*,\s*/, $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
854 | $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; |
717 | } |
855 | } |
718 | |
856 | |
719 | my @models = ( |
857 | my @models = ( |
720 | [EV:: => AnyEvent::Impl::EV::], |
858 | [EV:: => AnyEvent::Impl::EV::], |
721 | [Event:: => AnyEvent::Impl::Event::], |
859 | [Event:: => AnyEvent::Impl::Event::], |
722 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
723 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
724 | [Prima:: => AnyEvent::Impl::POE::], |
|
|
725 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
860 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
726 | # everything below here will not be autoprobed as the pureperl backend should work everywhere |
861 | # everything below here will not be autoprobed |
727 | [Glib:: => AnyEvent::Impl::Glib::], |
862 | # as the pureperl backend should work everywhere |
|
|
863 | # and is usually faster |
|
|
864 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
|
|
865 | [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers |
728 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
866 | [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy |
729 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
867 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
730 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
868 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
|
|
869 | [Wx:: => AnyEvent::Impl::POE::], |
|
|
870 | [Prima:: => AnyEvent::Impl::POE::], |
731 | ); |
871 | ); |
732 | |
872 | |
733 | our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); |
873 | our %method = map +($_ => 1), qw(io timer time now signal child condvar one_event DESTROY); |
734 | |
874 | |
735 | our @post_detect; |
875 | our @post_detect; |
736 | |
876 | |
737 | sub post_detect(&) { |
877 | sub post_detect(&) { |
738 | my ($cb) = @_; |
878 | my ($cb) = @_; |
… | |
… | |
755 | } |
895 | } |
756 | |
896 | |
757 | sub detect() { |
897 | sub detect() { |
758 | unless ($MODEL) { |
898 | unless ($MODEL) { |
759 | no strict 'refs'; |
899 | no strict 'refs'; |
|
|
900 | local $SIG{__DIE__}; |
760 | |
901 | |
761 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
902 | if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { |
762 | my $model = "AnyEvent::Impl::$1"; |
903 | my $model = "AnyEvent::Impl::$1"; |
763 | if (eval "require $model") { |
904 | if (eval "require $model") { |
764 | $MODEL = $model; |
905 | $MODEL = $model; |
… | |
… | |
798 | $MODEL |
939 | $MODEL |
799 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
940 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
800 | } |
941 | } |
801 | } |
942 | } |
802 | |
943 | |
|
|
944 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
945 | |
803 | unshift @ISA, $MODEL; |
946 | unshift @ISA, $MODEL; |
804 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
947 | |
|
|
948 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
805 | |
949 | |
806 | (shift @post_detect)->() while @post_detect; |
950 | (shift @post_detect)->() while @post_detect; |
807 | } |
951 | } |
808 | |
952 | |
809 | $MODEL |
953 | $MODEL |
… | |
… | |
820 | my $class = shift; |
964 | my $class = shift; |
821 | $class->$func (@_); |
965 | $class->$func (@_); |
822 | } |
966 | } |
823 | |
967 | |
824 | package AnyEvent::Base; |
968 | package AnyEvent::Base; |
|
|
969 | |
|
|
970 | # default implementation for now and time |
|
|
971 | |
|
|
972 | use Time::HiRes (); |
|
|
973 | |
|
|
974 | sub time { Time::HiRes::time } |
|
|
975 | sub now { Time::HiRes::time } |
825 | |
976 | |
826 | # default implementation for ->condvar |
977 | # default implementation for ->condvar |
827 | |
978 | |
828 | sub condvar { |
979 | sub condvar { |
829 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
980 | bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, AnyEvent::CondVar:: |
… | |
… | |
850 | sub AnyEvent::Base::Signal::DESTROY { |
1001 | sub AnyEvent::Base::Signal::DESTROY { |
851 | my ($signal, $cb) = @{$_[0]}; |
1002 | my ($signal, $cb) = @{$_[0]}; |
852 | |
1003 | |
853 | delete $SIG_CB{$signal}{$cb}; |
1004 | delete $SIG_CB{$signal}{$cb}; |
854 | |
1005 | |
855 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
1006 | delete $SIG{$signal} unless keys %{ $SIG_CB{$signal} }; |
856 | } |
1007 | } |
857 | |
1008 | |
858 | # default implementation for ->child |
1009 | # default implementation for ->child |
859 | |
1010 | |
860 | our %PID_CB; |
1011 | our %PID_CB; |
… | |
… | |
887 | or Carp::croak "required option 'pid' is missing"; |
1038 | or Carp::croak "required option 'pid' is missing"; |
888 | |
1039 | |
889 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1040 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
890 | |
1041 | |
891 | unless ($WNOHANG) { |
1042 | unless ($WNOHANG) { |
892 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
1043 | $WNOHANG = eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
893 | } |
1044 | } |
894 | |
1045 | |
895 | unless ($CHLD_W) { |
1046 | unless ($CHLD_W) { |
896 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
1047 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
897 | # child could be a zombie already, so make at least one round |
1048 | # child could be a zombie already, so make at least one round |
… | |
… | |
913 | package AnyEvent::CondVar; |
1064 | package AnyEvent::CondVar; |
914 | |
1065 | |
915 | our @ISA = AnyEvent::CondVar::Base::; |
1066 | our @ISA = AnyEvent::CondVar::Base::; |
916 | |
1067 | |
917 | package AnyEvent::CondVar::Base; |
1068 | package AnyEvent::CondVar::Base; |
|
|
1069 | |
|
|
1070 | use overload |
|
|
1071 | '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
|
|
1072 | fallback => 1; |
918 | |
1073 | |
919 | sub _send { |
1074 | sub _send { |
920 | # nop |
1075 | # nop |
921 | } |
1076 | } |
922 | |
1077 | |
… | |
… | |
1026 | C<PERL_ANYEVENT_MODEL>. |
1181 | C<PERL_ANYEVENT_MODEL>. |
1027 | |
1182 | |
1028 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1183 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1029 | model it chooses. |
1184 | model it chooses. |
1030 | |
1185 | |
|
|
1186 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1187 | |
|
|
1188 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1189 | argument checking is very costly. Setting this variable to a true value |
|
|
1190 | will cause AnyEvent to thoroughly check the arguments passed to most |
|
|
1191 | method calls and croaks if it finds any problems. In other words, enables |
|
|
1192 | "strict" mode. Unlike C<use strict> it is definitely recommended ot keep |
|
|
1193 | it off in production. |
|
|
1194 | |
1031 | =item C<PERL_ANYEVENT_MODEL> |
1195 | =item C<PERL_ANYEVENT_MODEL> |
1032 | |
1196 | |
1033 | This can be used to specify the event model to be used by AnyEvent, before |
1197 | This can be used to specify the event model to be used by AnyEvent, before |
1034 | autodetection and -probing kicks in. It must be a string consisting |
1198 | auto detection and -probing kicks in. It must be a string consisting |
1035 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
1199 | entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended |
1036 | and the resulting module name is loaded and if the load was successful, |
1200 | and the resulting module name is loaded and if the load was successful, |
1037 | used as event model. If it fails to load AnyEvent will proceed with |
1201 | used as event model. If it fails to load AnyEvent will proceed with |
1038 | autodetection and -probing. |
1202 | auto detection and -probing. |
1039 | |
1203 | |
1040 | This functionality might change in future versions. |
1204 | This functionality might change in future versions. |
1041 | |
1205 | |
1042 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1206 | For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you |
1043 | could start your program like this: |
1207 | could start your program like this: |
1044 | |
1208 | |
1045 | PERL_ANYEVENT_MODEL=Perl perl ... |
1209 | PERL_ANYEVENT_MODEL=Perl perl ... |
1046 | |
1210 | |
1047 | =item C<PERL_ANYEVENT_PROTOCOLS> |
1211 | =item C<PERL_ANYEVENT_PROTOCOLS> |
1048 | |
1212 | |
1049 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
1213 | Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences |
1050 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
1214 | for IPv4 or IPv6. The default is unspecified (and might change, or be the result |
1051 | of autoprobing). |
1215 | of auto probing). |
1052 | |
1216 | |
1053 | Must be set to a comma-separated list of protocols or address families, |
1217 | Must be set to a comma-separated list of protocols or address families, |
1054 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
1218 | current supported: C<ipv4> and C<ipv6>. Only protocols mentioned will be |
1055 | used, and preference will be given to protocols mentioned earlier in the |
1219 | used, and preference will be given to protocols mentioned earlier in the |
1056 | list. |
1220 | list. |
1057 | |
1221 | |
|
|
1222 | This variable can effectively be used for denial-of-service attacks |
|
|
1223 | against local programs (e.g. when setuid), although the impact is likely |
|
|
1224 | small, as the program has to handle connection errors already- |
|
|
1225 | |
1058 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
1226 | Examples: C<PERL_ANYEVENT_PROTOCOLS=ipv4,ipv6> - prefer IPv4 over IPv6, |
1059 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
1227 | but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> |
1060 | - only support IPv4, never try to resolve or contact IPv6 |
1228 | - only support IPv4, never try to resolve or contact IPv6 |
1061 | addressses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
1229 | addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or |
1062 | IPv6, but prefer IPv6 over IPv4. |
1230 | IPv6, but prefer IPv6 over IPv4. |
|
|
1231 | |
|
|
1232 | =item C<PERL_ANYEVENT_EDNS0> |
|
|
1233 | |
|
|
1234 | Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension |
|
|
1235 | for DNS. This extension is generally useful to reduce DNS traffic, but |
|
|
1236 | some (broken) firewalls drop such DNS packets, which is why it is off by |
|
|
1237 | default. |
|
|
1238 | |
|
|
1239 | Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce |
|
|
1240 | EDNS0 in its DNS requests. |
|
|
1241 | |
|
|
1242 | =item C<PERL_ANYEVENT_MAX_FORKS> |
|
|
1243 | |
|
|
1244 | The maximum number of child processes that C<AnyEvent::Util::fork_call> |
|
|
1245 | will create in parallel. |
1063 | |
1246 | |
1064 | =back |
1247 | =back |
1065 | |
1248 | |
1066 | =head1 EXAMPLE PROGRAM |
1249 | =head1 EXAMPLE PROGRAM |
1067 | |
1250 | |
… | |
… | |
1153 | syswrite $txn->{fh}, $txn->{request} |
1336 | syswrite $txn->{fh}, $txn->{request} |
1154 | or die "connection or write error"; |
1337 | or die "connection or write error"; |
1155 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
1338 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
1156 | |
1339 | |
1157 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
1340 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
1158 | result and signals any possible waiters that the request ahs finished: |
1341 | result and signals any possible waiters that the request has finished: |
1159 | |
1342 | |
1160 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1343 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
1161 | |
1344 | |
1162 | if (end-of-file or data complete) { |
1345 | if (end-of-file or data complete) { |
1163 | $txn->{result} = $txn->{buf}; |
1346 | $txn->{result} = $txn->{buf}; |
… | |
… | |
1171 | |
1354 | |
1172 | $txn->{finished}->recv; |
1355 | $txn->{finished}->recv; |
1173 | return $txn->{result}; |
1356 | return $txn->{result}; |
1174 | |
1357 | |
1175 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1358 | The actual code goes further and collects all errors (C<die>s, exceptions) |
1176 | that occured during request processing. The C<result> method detects |
1359 | that occurred during request processing. The C<result> method detects |
1177 | whether an exception as thrown (it is stored inside the $txn object) |
1360 | whether an exception as thrown (it is stored inside the $txn object) |
1178 | and just throws the exception, which means connection errors and other |
1361 | and just throws the exception, which means connection errors and other |
1179 | problems get reported tot he code that tries to use the result, not in a |
1362 | problems get reported tot he code that tries to use the result, not in a |
1180 | random callback. |
1363 | random callback. |
1181 | |
1364 | |
… | |
… | |
1227 | of various event loops I prepared some benchmarks. |
1410 | of various event loops I prepared some benchmarks. |
1228 | |
1411 | |
1229 | =head2 BENCHMARKING ANYEVENT OVERHEAD |
1412 | =head2 BENCHMARKING ANYEVENT OVERHEAD |
1230 | |
1413 | |
1231 | Here is a benchmark of various supported event models used natively and |
1414 | Here is a benchmark of various supported event models used natively and |
1232 | through anyevent. The benchmark creates a lot of timers (with a zero |
1415 | through AnyEvent. The benchmark creates a lot of timers (with a zero |
1233 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
1416 | timeout) and I/O watchers (watching STDOUT, a pty, to become writable, |
1234 | which it is), lets them fire exactly once and destroys them again. |
1417 | which it is), lets them fire exactly once and destroys them again. |
1235 | |
1418 | |
1236 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
1419 | Source code for this benchmark is found as F<eg/bench> in the AnyEvent |
1237 | distribution. |
1420 | distribution. |
… | |
… | |
1360 | |
1543 | |
1361 | =back |
1544 | =back |
1362 | |
1545 | |
1363 | =head2 BENCHMARKING THE LARGE SERVER CASE |
1546 | =head2 BENCHMARKING THE LARGE SERVER CASE |
1364 | |
1547 | |
1365 | This benchmark atcually benchmarks the event loop itself. It works by |
1548 | This benchmark actually benchmarks the event loop itself. It works by |
1366 | creating a number of "servers": each server consists of a socketpair, a |
1549 | creating a number of "servers": each server consists of a socket pair, a |
1367 | timeout watcher that gets reset on activity (but never fires), and an I/O |
1550 | timeout watcher that gets reset on activity (but never fires), and an I/O |
1368 | watcher waiting for input on one side of the socket. Each time the socket |
1551 | watcher waiting for input on one side of the socket. Each time the socket |
1369 | watcher reads a byte it will write that byte to a random other "server". |
1552 | watcher reads a byte it will write that byte to a random other "server". |
1370 | |
1553 | |
1371 | The effect is that there will be a lot of I/O watchers, only part of which |
1554 | The effect is that there will be a lot of I/O watchers, only part of which |
1372 | are active at any one point (so there is a constant number of active |
1555 | are active at any one point (so there is a constant number of active |
1373 | fds for each loop iterstaion, but which fds these are is random). The |
1556 | fds for each loop iteration, but which fds these are is random). The |
1374 | timeout is reset each time something is read because that reflects how |
1557 | timeout is reset each time something is read because that reflects how |
1375 | most timeouts work (and puts extra pressure on the event loops). |
1558 | most timeouts work (and puts extra pressure on the event loops). |
1376 | |
1559 | |
1377 | In this benchmark, we use 10000 socketpairs (20000 sockets), of which 100 |
1560 | In this benchmark, we use 10000 socket pairs (20000 sockets), of which 100 |
1378 | (1%) are active. This mirrors the activity of large servers with many |
1561 | (1%) are active. This mirrors the activity of large servers with many |
1379 | connections, most of which are idle at any one point in time. |
1562 | connections, most of which are idle at any one point in time. |
1380 | |
1563 | |
1381 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
1564 | Source code for this benchmark is found as F<eg/bench2> in the AnyEvent |
1382 | distribution. |
1565 | distribution. |
… | |
… | |
1384 | =head3 Explanation of the columns |
1567 | =head3 Explanation of the columns |
1385 | |
1568 | |
1386 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
1569 | I<sockets> is the number of sockets, and twice the number of "servers" (as |
1387 | each server has a read and write socket end). |
1570 | each server has a read and write socket end). |
1388 | |
1571 | |
1389 | I<create> is the time it takes to create a socketpair (which is |
1572 | I<create> is the time it takes to create a socket pair (which is |
1390 | nontrivial) and two watchers: an I/O watcher and a timeout watcher. |
1573 | nontrivial) and two watchers: an I/O watcher and a timeout watcher. |
1391 | |
1574 | |
1392 | I<request>, the most important value, is the time it takes to handle a |
1575 | I<request>, the most important value, is the time it takes to handle a |
1393 | single "request", that is, reading the token from the pipe and forwarding |
1576 | single "request", that is, reading the token from the pipe and forwarding |
1394 | it to another server. This includes deleting the old timeout and creating |
1577 | it to another server. This includes deleting the old timeout and creating |
… | |
… | |
1467 | speed most when you have lots of watchers, not when you only have a few of |
1650 | speed most when you have lots of watchers, not when you only have a few of |
1468 | them). |
1651 | them). |
1469 | |
1652 | |
1470 | EV is again fastest. |
1653 | EV is again fastest. |
1471 | |
1654 | |
1472 | Perl again comes second. It is noticably faster than the C-based event |
1655 | Perl again comes second. It is noticeably faster than the C-based event |
1473 | loops Event and Glib, although the difference is too small to really |
1656 | loops Event and Glib, although the difference is too small to really |
1474 | matter. |
1657 | matter. |
1475 | |
1658 | |
1476 | POE also performs much better in this case, but is is still far behind the |
1659 | POE also performs much better in this case, but is is still far behind the |
1477 | others. |
1660 | others. |
… | |
… | |
1506 | specified in the variable. |
1689 | specified in the variable. |
1507 | |
1690 | |
1508 | You can make AnyEvent completely ignore this variable by deleting it |
1691 | You can make AnyEvent completely ignore this variable by deleting it |
1509 | before the first watcher gets created, e.g. with a C<BEGIN> block: |
1692 | before the first watcher gets created, e.g. with a C<BEGIN> block: |
1510 | |
1693 | |
1511 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1694 | BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } |
1512 | |
1695 | |
1513 | use AnyEvent; |
1696 | use AnyEvent; |
1514 | |
1697 | |
1515 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1698 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1516 | be used to probe what backend is used and gain other information (which is |
1699 | be used to probe what backend is used and gain other information (which is |
1517 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1700 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and |
|
|
1701 | $ENV{PERL_ANYEGENT_STRICT}. |
|
|
1702 | |
|
|
1703 | |
|
|
1704 | =head1 BUGS |
|
|
1705 | |
|
|
1706 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |
|
|
1707 | to work around. If you suffer from memleaks, first upgrade to Perl 5.10 |
|
|
1708 | and check wether the leaks still show up. (Perl 5.10.0 has other annoying |
|
|
1709 | mamleaks, such as leaking on C<map> and C<grep> but it is usually not as |
|
|
1710 | pronounced). |
1518 | |
1711 | |
1519 | |
1712 | |
1520 | =head1 SEE ALSO |
1713 | =head1 SEE ALSO |
1521 | |
1714 | |
1522 | Utility functions: L<AnyEvent::Util>. |
1715 | Utility functions: L<AnyEvent::Util>. |
… | |
… | |
1539 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1732 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. |
1540 | |
1733 | |
1541 | |
1734 | |
1542 | =head1 AUTHOR |
1735 | =head1 AUTHOR |
1543 | |
1736 | |
1544 | Marc Lehmann <schmorp@schmorp.de> |
1737 | Marc Lehmann <schmorp@schmorp.de> |
1545 | http://home.schmorp.de/ |
1738 | http://home.schmorp.de/ |
1546 | |
1739 | |
1547 | =cut |
1740 | =cut |
1548 | |
1741 | |
1549 | 1 |
1742 | 1 |
1550 | |
1743 | |