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 | Event, Coro, Glib, Tk - various supported event loops |
5 | Event, Coro, Glib, Tk, Perl - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { |
11 | my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
12 | my ($poll_got) = @_; |
|
|
13 | ... |
12 | ... |
14 | }); |
13 | }); |
15 | |
|
|
16 | - only one io watcher per $fh and $poll type is allowed |
|
|
17 | (i.e. on a socket you can have one r + one w or one rw |
|
|
18 | watcher, not any more. |
|
|
19 | |
|
|
20 | - AnyEvent will keep filehandles alive, so as long as the watcher exists, |
|
|
21 | the filehandle exists. |
|
|
22 | |
14 | |
23 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
24 | ... |
16 | ... |
25 | }); |
17 | }); |
26 | |
18 | |
27 | - io and time watchers get canceled whenever $w is destroyed, so keep a copy |
19 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
28 | |
|
|
29 | - timers can only be used once and must be recreated for repeated operation |
|
|
30 | |
|
|
31 | my $w = AnyEvent->condvar; # kind of main loop replacement |
|
|
32 | $w->wait; # enters main loop till $condvar gets ->broadcast |
20 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
33 | $w->broadcast; # wake up current and all future wait's |
21 | $w->broadcast; # wake up current and all future wait's |
34 | |
22 | |
35 | - condvars are used to give blocking behaviour when neccessary. Create |
|
|
36 | a condvar for any "request" or "event" your module might create, C<< |
|
|
37 | ->broadcast >> it when the event happens and provide a function that calls |
|
|
38 | C<< ->wait >> for it. See the examples below. |
|
|
39 | |
|
|
40 | =head1 DESCRIPTION |
23 | =head1 DESCRIPTION |
41 | |
24 | |
42 | L<AnyEvent> provides an identical interface to multiple event loops. This |
25 | L<AnyEvent> provides an identical interface to multiple event loops. This |
43 | allows module authors to utilizy an event loop without forcing module |
26 | allows module authors to utilise an event loop without forcing module |
44 | users to use the same event loop (as only a single event loop can coexist |
27 | users to use the same event loop (as only a single event loop can coexist |
45 | peacefully at any one time). |
28 | peacefully at any one time). |
46 | |
29 | |
47 | The interface itself is vaguely similar but not identical to the Event |
30 | The interface itself is vaguely similar but not identical to the Event |
48 | module. |
31 | module. |
… | |
… | |
50 | On the first call of any method, the module tries to detect the currently |
33 | On the first call of any method, the module tries to detect the currently |
51 | loaded event loop by probing wether any of the following modules is |
34 | loaded event loop by probing wether any of the following modules is |
52 | loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
35 | loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
53 | used. If none is found, the module tries to load these modules in the |
36 | used. If none is found, the module tries to load these modules in the |
54 | order given. The first one that could be successfully loaded will be |
37 | order given. The first one that could be successfully loaded will be |
55 | used. If still none could be found, it will issue an error. |
38 | used. If still none could be found, AnyEvent will fall back to a pure-perl |
|
|
39 | event loop, which is also not very efficient. |
|
|
40 | |
|
|
41 | Because AnyEvent first checks for modules that are already loaded, loading |
|
|
42 | an Event model explicitly before first using AnyEvent will likely make |
|
|
43 | that model the default. For example: |
|
|
44 | |
|
|
45 | use Tk; |
|
|
46 | use AnyEvent; |
|
|
47 | |
|
|
48 | # .. AnyEvent will likely default to Tk |
|
|
49 | |
|
|
50 | The pure-perl implementation of AnyEvent is called |
|
|
51 | C<AnyEvent::Impl::Perl>. Like other event modules you can load it |
|
|
52 | explicitly. |
|
|
53 | |
|
|
54 | =head1 WATCHERS |
|
|
55 | |
|
|
56 | AnyEvent has the central concept of a I<watcher>, which is an object that |
|
|
57 | stores relevant data for each kind of event you are waiting for, such as |
|
|
58 | the callback to call, the filehandle to watch, etc. |
|
|
59 | |
|
|
60 | These watchers are normal Perl objects with normal Perl lifetime. After |
|
|
61 | creating a watcher it will immediately "watch" for events and invoke |
|
|
62 | the callback. To disable the watcher you have to destroy it (e.g. by |
|
|
63 | setting the variable that stores it to C<undef> or otherwise deleting all |
|
|
64 | references to it). |
|
|
65 | |
|
|
66 | All watchers are created by calling a method on the C<AnyEvent> class. |
|
|
67 | |
|
|
68 | =head2 IO WATCHERS |
|
|
69 | |
|
|
70 | You can create I/O watcher by calling the C<< AnyEvent->io >> method with |
|
|
71 | the following mandatory arguments: |
|
|
72 | |
|
|
73 | C<fh> the Perl I<filehandle> (not filedescriptor) to watch for |
|
|
74 | events. C<poll> must be a string that is either C<r> or C<w>, that creates |
|
|
75 | a watcher waiting for "r"eadable or "w"ritable events. C<cb> teh callback |
|
|
76 | to invoke everytime the filehandle becomes ready. |
|
|
77 | |
|
|
78 | Only one io watcher per C<fh> and C<poll> combination is allowed (i.e. on |
|
|
79 | a socket you can have one r + one w, not any more (limitation comes from |
|
|
80 | Tk - if you are sure you are not using Tk this limitation is gone). |
|
|
81 | |
|
|
82 | Filehandles will be kept alive, so as long as the watcher exists, the |
|
|
83 | filehandle exists, too. |
|
|
84 | |
|
|
85 | Example: |
|
|
86 | |
|
|
87 | # wait for readability of STDIN, then read a line and disable the watcher |
|
|
88 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
|
|
89 | chomp (my $input = <STDIN>); |
|
|
90 | warn "read: $input\n"; |
|
|
91 | undef $w; |
|
|
92 | }); |
|
|
93 | |
|
|
94 | =head2 TIME WATCHERS |
|
|
95 | |
|
|
96 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
|
|
97 | method with the following mandatory arguments: |
|
|
98 | |
|
|
99 | C<after> after how many seconds (fractions are supported) should the timer |
|
|
100 | activate. C<cb> the callback to invoke. |
|
|
101 | |
|
|
102 | The timer callback will be invoked at most once: if you want a repeating |
|
|
103 | timer you have to create a new watcher (this is a limitation by both Tk |
|
|
104 | and Glib). |
|
|
105 | |
|
|
106 | Example: |
|
|
107 | |
|
|
108 | # fire an event after 7.7 seconds |
|
|
109 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
|
|
110 | warn "timeout\n"; |
|
|
111 | }); |
|
|
112 | |
|
|
113 | # to cancel the timer: |
|
|
114 | undef $w |
|
|
115 | |
|
|
116 | =head2 CONDITION WATCHERS |
|
|
117 | |
|
|
118 | Condition watchers can be created by calling the C<< AnyEvent->condvar >> |
|
|
119 | method without any arguments. |
|
|
120 | |
|
|
121 | A condition watcher watches for a condition - precisely that the C<< |
|
|
122 | ->broadcast >> method has been called. |
|
|
123 | |
|
|
124 | The watcher has only two methods: |
56 | |
125 | |
57 | =over 4 |
126 | =over 4 |
58 | |
127 | |
|
|
128 | =item $cv->wait |
|
|
129 | |
|
|
130 | Wait (blocking if necessary) until the C<< ->broadcast >> method has been |
|
|
131 | called on c<$cv>, while servicing other watchers normally. |
|
|
132 | |
|
|
133 | Not all event models support a blocking wait - some die in that case, so |
|
|
134 | if you are using this from a module, never require a blocking wait, but |
|
|
135 | let the caller decide wether the call will block or not (for example, |
|
|
136 | by coupling condition variables with some kind of request results and |
|
|
137 | supporting callbacks so the caller knows that getting the result will not |
|
|
138 | block, while still suppporting blockign waits if the caller so desires). |
|
|
139 | |
|
|
140 | You can only wait once on a condition - additional calls will return |
|
|
141 | immediately. |
|
|
142 | |
|
|
143 | =item $cv->broadcast |
|
|
144 | |
|
|
145 | Flag the condition as ready - a running C<< ->wait >> and all further |
|
|
146 | calls to C<wait> will return after this method has been called. If nobody |
|
|
147 | is waiting the broadcast will be remembered.. |
|
|
148 | |
|
|
149 | Example: |
|
|
150 | |
|
|
151 | # wait till the result is ready |
|
|
152 | my $result_ready = AnyEvent->condvar; |
|
|
153 | |
|
|
154 | # do something such as adding a timer |
|
|
155 | # or socket watcher the calls $result_ready->broadcast |
|
|
156 | # when the "result" is ready. |
|
|
157 | |
|
|
158 | $result_ready->wait; |
|
|
159 | |
|
|
160 | =back |
|
|
161 | |
|
|
162 | =head2 SIGNAL WATCHERS |
|
|
163 | |
|
|
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
|
|
165 | I<name> without any C<SIG> prefix. Multiple signals events can be clumped |
|
|
166 | together into one callback invocation, and callback invocation might or |
|
|
167 | might not be asynchronous. |
|
|
168 | |
|
|
169 | These watchers might use C<%SIG>, so programs overwriting those signals |
|
|
170 | directly will likely not work correctly. |
|
|
171 | |
|
|
172 | Example: exit on SIGINT |
|
|
173 | |
|
|
174 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
175 | |
|
|
176 | =head2 CHILD PROCESS WATCHERS |
|
|
177 | |
|
|
178 | You can also listen for the status of a child process specified by the |
|
|
179 | C<pid> argument. The watcher will only trigger once. This works by |
|
|
180 | installing a signal handler for C<SIGCHLD>. |
|
|
181 | |
|
|
182 | Example: wait for pid 1333 |
|
|
183 | |
|
|
184 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
185 | |
|
|
186 | =head1 GLOBALS |
|
|
187 | |
|
|
188 | =over 4 |
|
|
189 | |
|
|
190 | =item $AnyEvent::MODEL |
|
|
191 | |
|
|
192 | Contains C<undef> until the first watcher is being created. Then it |
|
|
193 | contains the event model that is being used, which is the name of the |
|
|
194 | Perl class implementing the model. This class is usually one of the |
|
|
195 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
|
|
196 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
|
|
197 | |
|
|
198 | The known classes so far are: |
|
|
199 | |
|
|
200 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
|
|
201 | AnyEvent::Impl::Event based on Event, also best choice :) |
|
|
202 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
|
|
203 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
|
|
204 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
205 | |
|
|
206 | =item AnyEvent::detect |
|
|
207 | |
|
|
208 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
|
|
209 | necessary. You should only call this function right before you would have |
|
|
210 | created an AnyEvent watcher anyway, that is, very late at runtime. |
|
|
211 | |
|
|
212 | =back |
|
|
213 | |
|
|
214 | =head1 WHAT TO DO IN A MODULE |
|
|
215 | |
|
|
216 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
|
|
217 | freely, but you should not load a specific event module or rely on it. |
|
|
218 | |
|
|
219 | Be careful when you create watchers in the module body - Anyevent will |
|
|
220 | decide which event module to use as soon as the first method is called, so |
|
|
221 | by calling AnyEvent in your module body you force the user of your module |
|
|
222 | to load the event module first. |
|
|
223 | |
|
|
224 | =head1 WHAT TO DO IN THE MAIN PROGRAM |
|
|
225 | |
|
|
226 | There will always be a single main program - the only place that should |
|
|
227 | dictate which event model to use. |
|
|
228 | |
|
|
229 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
|
|
230 | do anything special and let AnyEvent decide which implementation to chose. |
|
|
231 | |
|
|
232 | If the main program relies on a specific event model (for example, in Gtk2 |
|
|
233 | programs you have to rely on either Glib or Glib::Event), you should load |
|
|
234 | it before loading AnyEvent or any module that uses it, generally, as early |
|
|
235 | as possible. The reason is that modules might create watchers when they |
|
|
236 | are loaded, and AnyEvent will decide on the event model to use as soon as |
|
|
237 | it creates watchers, and it might chose the wrong one unless you load the |
|
|
238 | correct one yourself. |
|
|
239 | |
|
|
240 | You can chose to use a rather inefficient pure-perl implementation by |
|
|
241 | loading the C<AnyEvent::Impl::Perl> module, but letting AnyEvent chose is |
|
|
242 | generally better. |
|
|
243 | |
59 | =cut |
244 | =cut |
60 | |
245 | |
61 | package AnyEvent; |
246 | package AnyEvent; |
62 | |
247 | |
63 | no warnings; |
248 | no warnings; |
64 | use strict 'vars'; |
249 | use strict; |
65 | use Carp; |
250 | use Carp; |
66 | |
251 | |
67 | our $VERSION = 0.3; |
252 | our $VERSION = '2.52'; |
68 | our $MODEL; |
253 | our $MODEL; |
69 | |
254 | |
70 | our $AUTOLOAD; |
255 | our $AUTOLOAD; |
71 | our @ISA; |
256 | our @ISA; |
72 | |
257 | |
|
|
258 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
|
|
259 | |
|
|
260 | our @REGISTRY; |
|
|
261 | |
73 | my @models = ( |
262 | my @models = ( |
74 | [Coro => Coro::Event::], |
263 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
75 | [Event => Event::], |
264 | [Event:: => AnyEvent::Impl::Event::], |
76 | [Glib => Glib::], |
265 | [Glib:: => AnyEvent::Impl::Glib::], |
77 | [Tk => Tk::], |
266 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
267 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
78 | ); |
268 | ); |
79 | |
269 | |
80 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
270 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
81 | |
271 | |
82 | sub AUTOLOAD { |
272 | sub detect() { |
83 | $AUTOLOAD =~ s/.*://; |
|
|
84 | |
|
|
85 | $method{$AUTOLOAD} |
|
|
86 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
|
|
87 | |
|
|
88 | unless ($MODEL) { |
273 | unless ($MODEL) { |
|
|
274 | no strict 'refs'; |
|
|
275 | |
89 | # check for already loaded models |
276 | # check for already loaded models |
90 | for (@models) { |
277 | for (@REGISTRY, @models) { |
91 | my ($model, $package) = @$_; |
278 | my ($package, $model) = @$_; |
92 | if (scalar keys %{ *{"$package\::"} }) { |
279 | if (${"$package\::VERSION"} > 0) { |
93 | eval "require AnyEvent::Impl::$model"; |
280 | if (eval "require $model") { |
94 | last if $MODEL; |
281 | $MODEL = $model; |
|
|
282 | warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
|
|
283 | last; |
|
|
284 | } |
95 | } |
285 | } |
96 | } |
286 | } |
97 | |
287 | |
98 | unless ($MODEL) { |
288 | unless ($MODEL) { |
99 | # try to load a model |
289 | # try to load a model |
100 | |
290 | |
101 | for (@models) { |
291 | for (@REGISTRY, @models) { |
102 | my ($model, $package) = @$_; |
292 | my ($package, $model) = @$_; |
103 | eval "require AnyEvent::Impl::$model"; |
293 | if (eval "require $package" |
104 | last if $MODEL; |
294 | and ${"$package\::VERSION"} > 0 |
|
|
295 | and eval "require $model") { |
|
|
296 | $MODEL = $model; |
|
|
297 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
|
|
298 | last; |
|
|
299 | } |
105 | } |
300 | } |
106 | |
301 | |
107 | $MODEL |
302 | $MODEL |
108 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
303 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; |
109 | } |
304 | } |
|
|
305 | |
|
|
306 | unshift @ISA, $MODEL; |
|
|
307 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
110 | } |
308 | } |
111 | |
309 | |
112 | @ISA = $MODEL; |
310 | $MODEL |
|
|
311 | } |
|
|
312 | |
|
|
313 | sub AUTOLOAD { |
|
|
314 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
315 | |
|
|
316 | $method{$func} |
|
|
317 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
318 | |
|
|
319 | detect unless $MODEL; |
113 | |
320 | |
114 | my $class = shift; |
321 | my $class = shift; |
115 | $class->$AUTOLOAD (@_); |
322 | $class->$func (@_); |
116 | } |
323 | } |
117 | |
324 | |
118 | =back |
325 | package AnyEvent::Base; |
|
|
326 | |
|
|
327 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
328 | |
|
|
329 | sub condvar { |
|
|
330 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
331 | } |
|
|
332 | |
|
|
333 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
334 | ${$_[0]}++; |
|
|
335 | } |
|
|
336 | |
|
|
337 | sub AnyEvent::Base::CondVar::wait { |
|
|
338 | AnyEvent->one_event while !${$_[0]}; |
|
|
339 | } |
|
|
340 | |
|
|
341 | # default implementation for ->signal |
|
|
342 | |
|
|
343 | our %SIG_CB; |
|
|
344 | |
|
|
345 | sub signal { |
|
|
346 | my (undef, %arg) = @_; |
|
|
347 | |
|
|
348 | my $signal = uc $arg{signal} |
|
|
349 | or Carp::croak "required option 'signal' is missing"; |
|
|
350 | |
|
|
351 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
352 | $SIG{$signal} ||= sub { |
|
|
353 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
|
|
354 | }; |
|
|
355 | |
|
|
356 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
|
|
357 | } |
|
|
358 | |
|
|
359 | sub AnyEvent::Base::Signal::DESTROY { |
|
|
360 | my ($signal, $cb) = @{$_[0]}; |
|
|
361 | |
|
|
362 | delete $SIG_CB{$signal}{$cb}; |
|
|
363 | |
|
|
364 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
365 | } |
|
|
366 | |
|
|
367 | # default implementation for ->child |
|
|
368 | |
|
|
369 | our %PID_CB; |
|
|
370 | our $CHLD_W; |
|
|
371 | our $PID_IDLE; |
|
|
372 | our $WNOHANG; |
|
|
373 | |
|
|
374 | sub _child_wait { |
|
|
375 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
|
|
376 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
|
|
377 | } |
|
|
378 | |
|
|
379 | undef $PID_IDLE; |
|
|
380 | } |
|
|
381 | |
|
|
382 | sub child { |
|
|
383 | my (undef, %arg) = @_; |
|
|
384 | |
|
|
385 | my $pid = uc $arg{pid} |
|
|
386 | or Carp::croak "required option 'pid' is missing"; |
|
|
387 | |
|
|
388 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
|
|
389 | |
|
|
390 | unless ($WNOHANG) { |
|
|
391 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
|
|
392 | } |
|
|
393 | |
|
|
394 | unless ($CHLD_W) { |
|
|
395 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
|
|
396 | # child could be a zombie already |
|
|
397 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
|
|
398 | } |
|
|
399 | |
|
|
400 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
|
|
401 | } |
|
|
402 | |
|
|
403 | sub AnyEvent::Base::Child::DESTROY { |
|
|
404 | my ($pid, $cb) = @{$_[0]}; |
|
|
405 | |
|
|
406 | delete $PID_CB{$pid}{$cb}; |
|
|
407 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
|
|
408 | |
|
|
409 | undef $CHLD_W unless keys %PID_CB; |
|
|
410 | } |
|
|
411 | |
|
|
412 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
|
|
413 | |
|
|
414 | If you need to support another event library which isn't directly |
|
|
415 | supported by AnyEvent, you can supply your own interface to it by |
|
|
416 | pushing, before the first watcher gets created, the package name of |
|
|
417 | the event module and the package name of the interface to use onto |
|
|
418 | C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
|
|
419 | AnyEvent. |
|
|
420 | |
|
|
421 | Example: |
|
|
422 | |
|
|
423 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
|
|
424 | |
|
|
425 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
|
|
426 | package/class when it finds the C<urxvt> package/module is loaded. When |
|
|
427 | AnyEvent is loaded and asked to find a suitable event model, it will |
|
|
428 | first check for the presence of urxvt. |
|
|
429 | |
|
|
430 | The class should provide implementations for all watcher types (see |
|
|
431 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
|
|
432 | (Source code) and so on for actual examples, use C<perldoc -m |
|
|
433 | AnyEvent::Impl::Glib> to see the sources). |
|
|
434 | |
|
|
435 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
|
|
436 | uses the above line as-is. An interface isn't included in AnyEvent |
|
|
437 | because it doesn't make sense outside the embedded interpreter inside |
|
|
438 | I<rxvt-unicode>, and it is updated and maintained as part of the |
|
|
439 | I<rxvt-unicode> distribution. |
|
|
440 | |
|
|
441 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
|
|
442 | condition variables: code blocking while waiting for a condition will |
|
|
443 | C<die>. This still works with most modules/usages, and blocking calls must |
|
|
444 | not be in an interactive appliation, so it makes sense. |
|
|
445 | |
|
|
446 | =head1 ENVIRONMENT VARIABLES |
|
|
447 | |
|
|
448 | The following environment variables are used by this module: |
|
|
449 | |
|
|
450 | C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
|
|
451 | model gets used. |
119 | |
452 | |
120 | =head1 EXAMPLE |
453 | =head1 EXAMPLE |
121 | |
454 | |
122 | The following program uses an io watcher to read data from stdin, a timer |
455 | The following program uses an io watcher to read data from stdin, a timer |
123 | to display a message once per second, and a condvar to exit the program |
456 | to display a message once per second, and a condvar to exit the program |
… | |
… | |
183 | connect $txn->{fh}, ... |
516 | connect $txn->{fh}, ... |
184 | and !$!{EWOULDBLOCK} |
517 | and !$!{EWOULDBLOCK} |
185 | and !$!{EINPROGRESS} |
518 | and !$!{EINPROGRESS} |
186 | and Carp::croak "unable to connect: $!\n"; |
519 | and Carp::croak "unable to connect: $!\n"; |
187 | |
520 | |
188 | Then it creates a write-watcher which gets called wehnever an error occurs |
521 | Then it creates a write-watcher which gets called whenever an error occurs |
189 | or the connection succeeds: |
522 | or the connection succeeds: |
190 | |
523 | |
191 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
524 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
192 | |
525 | |
193 | And returns this transaction object. The C<fh_ready_w> callback gets |
526 | And returns this transaction object. The C<fh_ready_w> callback gets |
… | |
… | |
210 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
543 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
211 | |
544 | |
212 | if (end-of-file or data complete) { |
545 | if (end-of-file or data complete) { |
213 | $txn->{result} = $txn->{buf}; |
546 | $txn->{result} = $txn->{buf}; |
214 | $txn->{finished}->broadcast; |
547 | $txn->{finished}->broadcast; |
|
|
548 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
215 | } |
549 | } |
216 | |
550 | |
217 | The C<result> method, finally, just waits for the finished signal (if the |
551 | The C<result> method, finally, just waits for the finished signal (if the |
218 | request was already finished, it doesn't wait, of course, and returns the |
552 | request was already finished, it doesn't wait, of course, and returns the |
219 | data: |
553 | data: |
220 | |
554 | |
221 | $txn->{finished}->wait; |
555 | $txn->{finished}->wait; |
222 | return $txn->{buf}; |
556 | return $txn->{result}; |
223 | |
557 | |
224 | The actual code goes further and collects all errors (C<die>s, exceptions) |
558 | The actual code goes further and collects all errors (C<die>s, exceptions) |
225 | that occured during request processing. The C<result> method detects |
559 | that occured during request processing. The C<result> method detects |
226 | wether an exception as thrown (it is stored inside the $txn object) |
560 | wether an exception as thrown (it is stored inside the $txn object) |
227 | and just throws the exception, which means connection errors and other |
561 | and just throws the exception, which means connection errors and other |