1 |
root |
1.1 |
=head1 NAME |
2 |
|
|
|
3 |
root |
1.2 |
AnyEvent - provide framework for multiple event loops |
4 |
|
|
|
5 |
root |
1.14 |
Event, Coro, Glib, Tk, Perl - various supported event loops |
6 |
root |
1.1 |
|
7 |
|
|
=head1 SYNOPSIS |
8 |
|
|
|
9 |
root |
1.7 |
use AnyEvent; |
10 |
root |
1.2 |
|
11 |
root |
1.14 |
my $w = AnyEvent->io (fh => $fh, poll => "r|w", cb => sub { |
12 |
root |
1.2 |
... |
13 |
|
|
}); |
14 |
root |
1.5 |
|
15 |
|
|
my $w = AnyEvent->timer (after => $seconds, cb => sub { |
16 |
root |
1.2 |
... |
17 |
|
|
}); |
18 |
|
|
|
19 |
root |
1.14 |
my $w = AnyEvent->condvar; # stores wether a condition was flagged |
20 |
|
|
$w->wait; # enters "main loop" till $condvar gets ->broadcast |
21 |
root |
1.5 |
$w->broadcast; # wake up current and all future wait's |
22 |
|
|
|
23 |
root |
1.1 |
=head1 DESCRIPTION |
24 |
|
|
|
25 |
root |
1.2 |
L<AnyEvent> provides an identical interface to multiple event loops. This |
26 |
root |
1.13 |
allows module authors to utilise an event loop without forcing module |
27 |
root |
1.2 |
users to use the same event loop (as only a single event loop can coexist |
28 |
|
|
peacefully at any one time). |
29 |
|
|
|
30 |
|
|
The interface itself is vaguely similar but not identical to the Event |
31 |
|
|
module. |
32 |
|
|
|
33 |
|
|
On the first call of any method, the module tries to detect the currently |
34 |
|
|
loaded event loop by probing wether any of the following modules is |
35 |
|
|
loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
36 |
|
|
used. If none is found, the module tries to load these modules in the |
37 |
|
|
order given. The first one that could be successfully loaded will be |
38 |
root |
1.14 |
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 |
root |
1.36 |
a watcher waiting for "r"eadable or "w"ritable events. C<cb> the callback |
76 |
root |
1.14 |
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 |
root |
1.19 |
=head2 TIME WATCHERS |
95 |
root |
1.14 |
|
96 |
root |
1.19 |
You can create a time watcher by calling the C<< AnyEvent->timer >> |
97 |
root |
1.14 |
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 |
root |
1.37 |
undef $w; |
115 |
root |
1.14 |
|
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: |
125 |
root |
1.2 |
|
126 |
root |
1.1 |
=over 4 |
127 |
|
|
|
128 |
root |
1.14 |
=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 |
root |
1.19 |
=head2 SIGNAL WATCHERS |
163 |
|
|
|
164 |
|
|
You can listen for signals using a signal watcher, C<signal> is the signal |
165 |
root |
1.20 |
I<name> without any C<SIG> prefix. Multiple signals events can be clumped |
166 |
root |
1.22 |
together into one callback invocation, and callback invocation might or |
167 |
root |
1.20 |
might not be asynchronous. |
168 |
root |
1.19 |
|
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 |
root |
1.20 |
=head2 CHILD PROCESS WATCHERS |
177 |
|
|
|
178 |
|
|
You can also listen for the status of a child process specified by the |
179 |
root |
1.31 |
C<pid> argument (or any child if the pid argument is 0). The watcher will |
180 |
|
|
trigger as often as status change for the child are received. This works |
181 |
root |
1.32 |
by installing a signal handler for C<SIGCHLD>. The callback will be called with |
182 |
|
|
the pid and exit status (as returned by waitpid). |
183 |
root |
1.20 |
|
184 |
|
|
Example: wait for pid 1333 |
185 |
|
|
|
186 |
|
|
my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
187 |
|
|
|
188 |
root |
1.16 |
=head1 GLOBALS |
189 |
|
|
|
190 |
|
|
=over 4 |
191 |
|
|
|
192 |
|
|
=item $AnyEvent::MODEL |
193 |
|
|
|
194 |
|
|
Contains C<undef> until the first watcher is being created. Then it |
195 |
|
|
contains the event model that is being used, which is the name of the |
196 |
|
|
Perl class implementing the model. This class is usually one of the |
197 |
|
|
C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
198 |
|
|
AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
199 |
|
|
|
200 |
|
|
The known classes so far are: |
201 |
|
|
|
202 |
root |
1.33 |
AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
203 |
|
|
AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
204 |
root |
1.29 |
AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
205 |
|
|
AnyEvent::Impl::Event based on Event, also second best choice :) |
206 |
root |
1.16 |
AnyEvent::Impl::Glib based on Glib, second-best choice. |
207 |
|
|
AnyEvent::Impl::Tk based on Tk, very bad choice. |
208 |
|
|
AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
209 |
|
|
|
210 |
root |
1.19 |
=item AnyEvent::detect |
211 |
|
|
|
212 |
|
|
Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
213 |
|
|
necessary. You should only call this function right before you would have |
214 |
|
|
created an AnyEvent watcher anyway, that is, very late at runtime. |
215 |
|
|
|
216 |
root |
1.16 |
=back |
217 |
|
|
|
218 |
root |
1.14 |
=head1 WHAT TO DO IN A MODULE |
219 |
|
|
|
220 |
|
|
As a module author, you should "use AnyEvent" and call AnyEvent methods |
221 |
|
|
freely, but you should not load a specific event module or rely on it. |
222 |
|
|
|
223 |
|
|
Be careful when you create watchers in the module body - Anyevent will |
224 |
|
|
decide which event module to use as soon as the first method is called, so |
225 |
|
|
by calling AnyEvent in your module body you force the user of your module |
226 |
|
|
to load the event module first. |
227 |
|
|
|
228 |
|
|
=head1 WHAT TO DO IN THE MAIN PROGRAM |
229 |
|
|
|
230 |
|
|
There will always be a single main program - the only place that should |
231 |
|
|
dictate which event model to use. |
232 |
|
|
|
233 |
|
|
If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
234 |
|
|
do anything special and let AnyEvent decide which implementation to chose. |
235 |
|
|
|
236 |
|
|
If the main program relies on a specific event model (for example, in Gtk2 |
237 |
|
|
programs you have to rely on either Glib or Glib::Event), you should load |
238 |
|
|
it before loading AnyEvent or any module that uses it, generally, as early |
239 |
|
|
as possible. The reason is that modules might create watchers when they |
240 |
|
|
are loaded, and AnyEvent will decide on the event model to use as soon as |
241 |
|
|
it creates watchers, and it might chose the wrong one unless you load the |
242 |
|
|
correct one yourself. |
243 |
|
|
|
244 |
|
|
You can chose to use a rather inefficient pure-perl implementation by |
245 |
|
|
loading the C<AnyEvent::Impl::Perl> module, but letting AnyEvent chose is |
246 |
|
|
generally better. |
247 |
|
|
|
248 |
root |
1.1 |
=cut |
249 |
|
|
|
250 |
|
|
package AnyEvent; |
251 |
|
|
|
252 |
root |
1.2 |
no warnings; |
253 |
root |
1.19 |
use strict; |
254 |
root |
1.24 |
|
255 |
root |
1.1 |
use Carp; |
256 |
|
|
|
257 |
root |
1.39 |
our $VERSION = '2.9'; |
258 |
root |
1.2 |
our $MODEL; |
259 |
root |
1.1 |
|
260 |
root |
1.2 |
our $AUTOLOAD; |
261 |
|
|
our @ISA; |
262 |
root |
1.1 |
|
263 |
root |
1.7 |
our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
264 |
|
|
|
265 |
root |
1.8 |
our @REGISTRY; |
266 |
|
|
|
267 |
root |
1.1 |
my @models = ( |
268 |
root |
1.33 |
[Coro::EV:: => AnyEvent::Impl::CoroEV::], |
269 |
|
|
[EV:: => AnyEvent::Impl::EV::], |
270 |
root |
1.18 |
[Coro::Event:: => AnyEvent::Impl::Coro::], |
271 |
|
|
[Event:: => AnyEvent::Impl::Event::], |
272 |
|
|
[Glib:: => AnyEvent::Impl::Glib::], |
273 |
|
|
[Tk:: => AnyEvent::Impl::Tk::], |
274 |
|
|
[AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
275 |
root |
1.1 |
); |
276 |
|
|
|
277 |
root |
1.19 |
our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
278 |
root |
1.3 |
|
279 |
root |
1.19 |
sub detect() { |
280 |
|
|
unless ($MODEL) { |
281 |
|
|
no strict 'refs'; |
282 |
root |
1.1 |
|
283 |
root |
1.2 |
# check for already loaded models |
284 |
root |
1.8 |
for (@REGISTRY, @models) { |
285 |
|
|
my ($package, $model) = @$_; |
286 |
root |
1.7 |
if (${"$package\::VERSION"} > 0) { |
287 |
root |
1.8 |
if (eval "require $model") { |
288 |
|
|
$MODEL = $model; |
289 |
|
|
warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
290 |
|
|
last; |
291 |
|
|
} |
292 |
root |
1.2 |
} |
293 |
root |
1.1 |
} |
294 |
|
|
|
295 |
root |
1.2 |
unless ($MODEL) { |
296 |
|
|
# try to load a model |
297 |
|
|
|
298 |
root |
1.8 |
for (@REGISTRY, @models) { |
299 |
|
|
my ($package, $model) = @$_; |
300 |
root |
1.21 |
if (eval "require $package" |
301 |
|
|
and ${"$package\::VERSION"} > 0 |
302 |
|
|
and eval "require $model") { |
303 |
root |
1.8 |
$MODEL = $model; |
304 |
|
|
warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
305 |
|
|
last; |
306 |
|
|
} |
307 |
root |
1.2 |
} |
308 |
|
|
|
309 |
|
|
$MODEL |
310 |
root |
1.35 |
or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk."; |
311 |
root |
1.1 |
} |
312 |
root |
1.19 |
|
313 |
|
|
unshift @ISA, $MODEL; |
314 |
|
|
push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
315 |
root |
1.1 |
} |
316 |
|
|
|
317 |
root |
1.19 |
$MODEL |
318 |
|
|
} |
319 |
|
|
|
320 |
|
|
sub AUTOLOAD { |
321 |
|
|
(my $func = $AUTOLOAD) =~ s/.*://; |
322 |
|
|
|
323 |
|
|
$method{$func} |
324 |
|
|
or croak "$func: not a valid method for AnyEvent objects"; |
325 |
|
|
|
326 |
|
|
detect unless $MODEL; |
327 |
root |
1.2 |
|
328 |
|
|
my $class = shift; |
329 |
root |
1.18 |
$class->$func (@_); |
330 |
root |
1.1 |
} |
331 |
|
|
|
332 |
root |
1.19 |
package AnyEvent::Base; |
333 |
|
|
|
334 |
root |
1.20 |
# default implementation for ->condvar, ->wait, ->broadcast |
335 |
|
|
|
336 |
|
|
sub condvar { |
337 |
|
|
bless \my $flag, "AnyEvent::Base::CondVar" |
338 |
|
|
} |
339 |
|
|
|
340 |
|
|
sub AnyEvent::Base::CondVar::broadcast { |
341 |
|
|
${$_[0]}++; |
342 |
|
|
} |
343 |
|
|
|
344 |
|
|
sub AnyEvent::Base::CondVar::wait { |
345 |
|
|
AnyEvent->one_event while !${$_[0]}; |
346 |
|
|
} |
347 |
|
|
|
348 |
|
|
# default implementation for ->signal |
349 |
root |
1.19 |
|
350 |
|
|
our %SIG_CB; |
351 |
|
|
|
352 |
|
|
sub signal { |
353 |
|
|
my (undef, %arg) = @_; |
354 |
|
|
|
355 |
|
|
my $signal = uc $arg{signal} |
356 |
|
|
or Carp::croak "required option 'signal' is missing"; |
357 |
|
|
|
358 |
root |
1.31 |
$SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
359 |
root |
1.19 |
$SIG{$signal} ||= sub { |
360 |
root |
1.20 |
$_->() for values %{ $SIG_CB{$signal} || {} }; |
361 |
root |
1.19 |
}; |
362 |
|
|
|
363 |
root |
1.20 |
bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
364 |
root |
1.19 |
} |
365 |
|
|
|
366 |
|
|
sub AnyEvent::Base::Signal::DESTROY { |
367 |
|
|
my ($signal, $cb) = @{$_[0]}; |
368 |
|
|
|
369 |
|
|
delete $SIG_CB{$signal}{$cb}; |
370 |
|
|
|
371 |
|
|
$SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
372 |
|
|
} |
373 |
|
|
|
374 |
root |
1.20 |
# default implementation for ->child |
375 |
|
|
|
376 |
|
|
our %PID_CB; |
377 |
|
|
our $CHLD_W; |
378 |
root |
1.37 |
our $CHLD_DELAY_W; |
379 |
root |
1.20 |
our $PID_IDLE; |
380 |
|
|
our $WNOHANG; |
381 |
|
|
|
382 |
|
|
sub _child_wait { |
383 |
root |
1.38 |
while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
384 |
root |
1.32 |
$_->($pid, $?) for (values %{ $PID_CB{$pid} || {} }), |
385 |
|
|
(values %{ $PID_CB{0} || {} }); |
386 |
root |
1.20 |
} |
387 |
|
|
|
388 |
|
|
undef $PID_IDLE; |
389 |
|
|
} |
390 |
|
|
|
391 |
root |
1.37 |
sub _sigchld { |
392 |
|
|
# make sure we deliver these changes "synchronous" with the event loop. |
393 |
|
|
$CHLD_DELAY_W ||= AnyEvent->timer (after => 0, cb => sub { |
394 |
|
|
undef $CHLD_DELAY_W; |
395 |
|
|
&_child_wait; |
396 |
|
|
}); |
397 |
|
|
} |
398 |
|
|
|
399 |
root |
1.20 |
sub child { |
400 |
|
|
my (undef, %arg) = @_; |
401 |
|
|
|
402 |
root |
1.31 |
defined (my $pid = $arg{pid} + 0) |
403 |
root |
1.20 |
or Carp::croak "required option 'pid' is missing"; |
404 |
|
|
|
405 |
|
|
$PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
406 |
|
|
|
407 |
|
|
unless ($WNOHANG) { |
408 |
|
|
$WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
409 |
|
|
} |
410 |
|
|
|
411 |
root |
1.23 |
unless ($CHLD_W) { |
412 |
root |
1.37 |
$CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_sigchld); |
413 |
|
|
# child could be a zombie already, so make at least one round |
414 |
|
|
&_sigchld; |
415 |
root |
1.23 |
} |
416 |
root |
1.20 |
|
417 |
|
|
bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
sub AnyEvent::Base::Child::DESTROY { |
421 |
|
|
my ($pid, $cb) = @{$_[0]}; |
422 |
|
|
|
423 |
|
|
delete $PID_CB{$pid}{$cb}; |
424 |
|
|
delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
425 |
|
|
|
426 |
|
|
undef $CHLD_W unless keys %PID_CB; |
427 |
|
|
} |
428 |
|
|
|
429 |
root |
1.8 |
=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
430 |
|
|
|
431 |
|
|
If you need to support another event library which isn't directly |
432 |
|
|
supported by AnyEvent, you can supply your own interface to it by |
433 |
root |
1.11 |
pushing, before the first watcher gets created, the package name of |
434 |
root |
1.8 |
the event module and the package name of the interface to use onto |
435 |
|
|
C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
436 |
|
|
AnyEvent. |
437 |
|
|
|
438 |
|
|
Example: |
439 |
|
|
|
440 |
|
|
push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
441 |
|
|
|
442 |
root |
1.12 |
This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
443 |
|
|
package/class when it finds the C<urxvt> package/module is loaded. When |
444 |
|
|
AnyEvent is loaded and asked to find a suitable event model, it will |
445 |
|
|
first check for the presence of urxvt. |
446 |
|
|
|
447 |
root |
1.19 |
The class should provide implementations for all watcher types (see |
448 |
root |
1.12 |
L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
449 |
|
|
(Source code) and so on for actual examples, use C<perldoc -m |
450 |
|
|
AnyEvent::Impl::Glib> to see the sources). |
451 |
root |
1.8 |
|
452 |
root |
1.12 |
The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
453 |
|
|
uses the above line as-is. An interface isn't included in AnyEvent |
454 |
root |
1.8 |
because it doesn't make sense outside the embedded interpreter inside |
455 |
|
|
I<rxvt-unicode>, and it is updated and maintained as part of the |
456 |
|
|
I<rxvt-unicode> distribution. |
457 |
|
|
|
458 |
root |
1.12 |
I<rxvt-unicode> also cheats a bit by not providing blocking access to |
459 |
|
|
condition variables: code blocking while waiting for a condition will |
460 |
|
|
C<die>. This still works with most modules/usages, and blocking calls must |
461 |
root |
1.25 |
not be in an interactive application, so it makes sense. |
462 |
root |
1.12 |
|
463 |
root |
1.7 |
=head1 ENVIRONMENT VARIABLES |
464 |
|
|
|
465 |
|
|
The following environment variables are used by this module: |
466 |
|
|
|
467 |
|
|
C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
468 |
|
|
model gets used. |
469 |
|
|
|
470 |
root |
1.2 |
=head1 EXAMPLE |
471 |
|
|
|
472 |
|
|
The following program uses an io watcher to read data from stdin, a timer |
473 |
|
|
to display a message once per second, and a condvar to exit the program |
474 |
|
|
when the user enters quit: |
475 |
|
|
|
476 |
|
|
use AnyEvent; |
477 |
|
|
|
478 |
|
|
my $cv = AnyEvent->condvar; |
479 |
|
|
|
480 |
|
|
my $io_watcher = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
481 |
|
|
warn "io event <$_[0]>\n"; # will always output <r> |
482 |
|
|
chomp (my $input = <STDIN>); # read a line |
483 |
|
|
warn "read: $input\n"; # output what has been read |
484 |
|
|
$cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
485 |
|
|
}); |
486 |
|
|
|
487 |
|
|
my $time_watcher; # can only be used once |
488 |
|
|
|
489 |
|
|
sub new_timer { |
490 |
|
|
$timer = AnyEvent->timer (after => 1, cb => sub { |
491 |
|
|
warn "timeout\n"; # print 'timeout' about every second |
492 |
|
|
&new_timer; # and restart the time |
493 |
|
|
}); |
494 |
|
|
} |
495 |
|
|
|
496 |
|
|
new_timer; # create first timer |
497 |
|
|
|
498 |
|
|
$cv->wait; # wait until user enters /^q/i |
499 |
|
|
|
500 |
root |
1.5 |
=head1 REAL-WORLD EXAMPLE |
501 |
|
|
|
502 |
|
|
Consider the L<Net::FCP> module. It features (among others) the following |
503 |
|
|
API calls, which are to freenet what HTTP GET requests are to http: |
504 |
|
|
|
505 |
|
|
my $data = $fcp->client_get ($url); # blocks |
506 |
|
|
|
507 |
|
|
my $transaction = $fcp->txn_client_get ($url); # does not block |
508 |
|
|
$transaction->cb ( sub { ... } ); # set optional result callback |
509 |
|
|
my $data = $transaction->result; # possibly blocks |
510 |
|
|
|
511 |
|
|
The C<client_get> method works like C<LWP::Simple::get>: it requests the |
512 |
|
|
given URL and waits till the data has arrived. It is defined to be: |
513 |
|
|
|
514 |
|
|
sub client_get { $_[0]->txn_client_get ($_[1])->result } |
515 |
|
|
|
516 |
|
|
And in fact is automatically generated. This is the blocking API of |
517 |
|
|
L<Net::FCP>, and it works as simple as in any other, similar, module. |
518 |
|
|
|
519 |
|
|
More complicated is C<txn_client_get>: It only creates a transaction |
520 |
|
|
(completion, result, ...) object and initiates the transaction. |
521 |
|
|
|
522 |
|
|
my $txn = bless { }, Net::FCP::Txn::; |
523 |
|
|
|
524 |
|
|
It also creates a condition variable that is used to signal the completion |
525 |
|
|
of the request: |
526 |
|
|
|
527 |
|
|
$txn->{finished} = AnyAvent->condvar; |
528 |
|
|
|
529 |
|
|
It then creates a socket in non-blocking mode. |
530 |
|
|
|
531 |
|
|
socket $txn->{fh}, ...; |
532 |
|
|
fcntl $txn->{fh}, F_SETFL, O_NONBLOCK; |
533 |
|
|
connect $txn->{fh}, ... |
534 |
|
|
and !$!{EWOULDBLOCK} |
535 |
|
|
and !$!{EINPROGRESS} |
536 |
|
|
and Carp::croak "unable to connect: $!\n"; |
537 |
|
|
|
538 |
root |
1.6 |
Then it creates a write-watcher which gets called whenever an error occurs |
539 |
root |
1.5 |
or the connection succeeds: |
540 |
|
|
|
541 |
|
|
$txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
542 |
|
|
|
543 |
|
|
And returns this transaction object. The C<fh_ready_w> callback gets |
544 |
|
|
called as soon as the event loop detects that the socket is ready for |
545 |
|
|
writing. |
546 |
|
|
|
547 |
|
|
The C<fh_ready_w> method makes the socket blocking again, writes the |
548 |
|
|
request data and replaces the watcher by a read watcher (waiting for reply |
549 |
|
|
data). The actual code is more complicated, but that doesn't matter for |
550 |
|
|
this example: |
551 |
|
|
|
552 |
|
|
fcntl $txn->{fh}, F_SETFL, 0; |
553 |
|
|
syswrite $txn->{fh}, $txn->{request} |
554 |
|
|
or die "connection or write error"; |
555 |
|
|
$txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
556 |
|
|
|
557 |
|
|
Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
558 |
|
|
result and signals any possible waiters that the request ahs finished: |
559 |
|
|
|
560 |
|
|
sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
561 |
|
|
|
562 |
|
|
if (end-of-file or data complete) { |
563 |
|
|
$txn->{result} = $txn->{buf}; |
564 |
|
|
$txn->{finished}->broadcast; |
565 |
root |
1.6 |
$txb->{cb}->($txn) of $txn->{cb}; # also call callback |
566 |
root |
1.5 |
} |
567 |
|
|
|
568 |
|
|
The C<result> method, finally, just waits for the finished signal (if the |
569 |
|
|
request was already finished, it doesn't wait, of course, and returns the |
570 |
|
|
data: |
571 |
|
|
|
572 |
|
|
$txn->{finished}->wait; |
573 |
root |
1.6 |
return $txn->{result}; |
574 |
root |
1.5 |
|
575 |
|
|
The actual code goes further and collects all errors (C<die>s, exceptions) |
576 |
|
|
that occured during request processing. The C<result> method detects |
577 |
|
|
wether an exception as thrown (it is stored inside the $txn object) |
578 |
|
|
and just throws the exception, which means connection errors and other |
579 |
|
|
problems get reported tot he code that tries to use the result, not in a |
580 |
|
|
random callback. |
581 |
|
|
|
582 |
|
|
All of this enables the following usage styles: |
583 |
|
|
|
584 |
|
|
1. Blocking: |
585 |
|
|
|
586 |
|
|
my $data = $fcp->client_get ($url); |
587 |
|
|
|
588 |
|
|
2. Blocking, but parallelizing: |
589 |
|
|
|
590 |
|
|
my @datas = map $_->result, |
591 |
|
|
map $fcp->txn_client_get ($_), |
592 |
|
|
@urls; |
593 |
|
|
|
594 |
|
|
Both blocking examples work without the module user having to know |
595 |
|
|
anything about events. |
596 |
|
|
|
597 |
|
|
3a. Event-based in a main program, using any support Event module: |
598 |
|
|
|
599 |
|
|
use Event; |
600 |
|
|
|
601 |
|
|
$fcp->txn_client_get ($url)->cb (sub { |
602 |
|
|
my $txn = shift; |
603 |
|
|
my $data = $txn->result; |
604 |
|
|
... |
605 |
|
|
}); |
606 |
|
|
|
607 |
|
|
Event::loop; |
608 |
|
|
|
609 |
|
|
3b. The module user could use AnyEvent, too: |
610 |
|
|
|
611 |
|
|
use AnyEvent; |
612 |
|
|
|
613 |
|
|
my $quit = AnyEvent->condvar; |
614 |
|
|
|
615 |
|
|
$fcp->txn_client_get ($url)->cb (sub { |
616 |
|
|
... |
617 |
|
|
$quit->broadcast; |
618 |
|
|
}); |
619 |
|
|
|
620 |
|
|
$quit->wait; |
621 |
|
|
|
622 |
root |
1.2 |
=head1 SEE ALSO |
623 |
|
|
|
624 |
root |
1.5 |
Event modules: L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>. |
625 |
|
|
|
626 |
|
|
Implementations: L<AnyEvent::Impl::Coro>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>. |
627 |
|
|
|
628 |
|
|
Nontrivial usage example: L<Net::FCP>. |
629 |
root |
1.2 |
|
630 |
|
|
=head1 |
631 |
|
|
|
632 |
|
|
=cut |
633 |
|
|
|
634 |
|
|
1 |
635 |
root |
1.1 |
|