1 |
=head1 NAME |
2 |
|
3 |
AnyEvent - provide framework for multiple event loops |
4 |
|
5 |
Event, Coro, Glib, Tk - various supported event loops |
6 |
|
7 |
=head1 SYNOPSIS |
8 |
|
9 |
use AnyEvent; |
10 |
|
11 |
my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { |
12 |
my ($poll_got) = @_; |
13 |
... |
14 |
}); |
15 |
|
16 |
* only one io watcher per $fh and $poll type is allowed (i.e. on a socket |
17 |
you can have one r + one w or one rw watcher, not any more (limitation by |
18 |
Tk). |
19 |
|
20 |
* the C<$poll_got> passed to the handler needs to be checked by looking |
21 |
for single characters (e.g. with a regex), as it can contain more event |
22 |
types than were requested (e.g. a 'w' watcher might generate 'rw' events, |
23 |
limitation by Glib). |
24 |
|
25 |
* AnyEvent will keep filehandles alive, so as long as the watcher exists, |
26 |
the filehandle exists. |
27 |
|
28 |
my $w = AnyEvent->timer (after => $seconds, cb => sub { |
29 |
... |
30 |
}); |
31 |
|
32 |
* io and time watchers get canceled whenever $w is destroyed, so keep a copy |
33 |
|
34 |
* timers can only be used once and must be recreated for repeated |
35 |
operation (limitation by Glib and Tk). |
36 |
|
37 |
my $w = AnyEvent->condvar; # kind of main loop replacement |
38 |
$w->wait; # enters main loop till $condvar gets ->broadcast |
39 |
$w->broadcast; # wake up current and all future wait's |
40 |
|
41 |
* condvars are used to give blocking behaviour when neccessary. Create |
42 |
a condvar for any "request" or "event" your module might create, C<< |
43 |
->broadcast >> it when the event happens and provide a function that calls |
44 |
C<< ->wait >> for it. See the examples below. |
45 |
|
46 |
=head1 DESCRIPTION |
47 |
|
48 |
L<AnyEvent> provides an identical interface to multiple event loops. This |
49 |
allows module authors to utilise an event loop without forcing module |
50 |
users to use the same event loop (as only a single event loop can coexist |
51 |
peacefully at any one time). |
52 |
|
53 |
The interface itself is vaguely similar but not identical to the Event |
54 |
module. |
55 |
|
56 |
On the first call of any method, the module tries to detect the currently |
57 |
loaded event loop by probing wether any of the following modules is |
58 |
loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
59 |
used. If none is found, the module tries to load these modules in the |
60 |
order given. The first one that could be successfully loaded will be |
61 |
used. If still none could be found, it will issue an error. |
62 |
|
63 |
=over 4 |
64 |
|
65 |
=cut |
66 |
|
67 |
package AnyEvent; |
68 |
|
69 |
no warnings; |
70 |
use strict 'vars'; |
71 |
use Carp; |
72 |
|
73 |
our $VERSION = '1.02'; |
74 |
our $MODEL; |
75 |
|
76 |
our $AUTOLOAD; |
77 |
our @ISA; |
78 |
|
79 |
our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
80 |
|
81 |
our @REGISTRY; |
82 |
|
83 |
my @models = ( |
84 |
[Coro::Event:: => AnyEvent::Impl::Coro::], |
85 |
[Event:: => AnyEvent::Impl::Event::], |
86 |
[Glib:: => AnyEvent::Impl::Glib::], |
87 |
[Tk:: => AnyEvent::Impl::Tk::], |
88 |
); |
89 |
|
90 |
our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
91 |
|
92 |
sub AUTOLOAD { |
93 |
$AUTOLOAD =~ s/.*://; |
94 |
|
95 |
$method{$AUTOLOAD} |
96 |
or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
97 |
|
98 |
unless ($MODEL) { |
99 |
# check for already loaded models |
100 |
for (@REGISTRY, @models) { |
101 |
my ($package, $model) = @$_; |
102 |
if (${"$package\::VERSION"} > 0) { |
103 |
if (eval "require $model") { |
104 |
$MODEL = $model; |
105 |
warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
106 |
last; |
107 |
} |
108 |
} |
109 |
} |
110 |
|
111 |
unless ($MODEL) { |
112 |
# try to load a model |
113 |
|
114 |
for (@REGISTRY, @models) { |
115 |
my ($package, $model) = @$_; |
116 |
if (eval "require $model") { |
117 |
$MODEL = $model; |
118 |
warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
119 |
last; |
120 |
} |
121 |
} |
122 |
|
123 |
$MODEL |
124 |
or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
125 |
} |
126 |
} |
127 |
|
128 |
@ISA = $MODEL; |
129 |
|
130 |
my $class = shift; |
131 |
$class->$AUTOLOAD (@_); |
132 |
} |
133 |
|
134 |
=back |
135 |
|
136 |
=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
137 |
|
138 |
If you need to support another event library which isn't directly |
139 |
supported by AnyEvent, you can supply your own interface to it by |
140 |
pushing, before the first watcher gets created, the package name of |
141 |
the event module and the package name of the interface to use onto |
142 |
C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
143 |
AnyEvent. |
144 |
|
145 |
Example: |
146 |
|
147 |
push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
148 |
|
149 |
This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
150 |
package/class when it finds the C<urxvt> package/module is loaded. When |
151 |
AnyEvent is loaded and asked to find a suitable event model, it will |
152 |
first check for the presence of urxvt. |
153 |
|
154 |
The class should prove implementations for all watcher types (see |
155 |
L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
156 |
(Source code) and so on for actual examples, use C<perldoc -m |
157 |
AnyEvent::Impl::Glib> to see the sources). |
158 |
|
159 |
The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
160 |
uses the above line as-is. An interface isn't included in AnyEvent |
161 |
because it doesn't make sense outside the embedded interpreter inside |
162 |
I<rxvt-unicode>, and it is updated and maintained as part of the |
163 |
I<rxvt-unicode> distribution. |
164 |
|
165 |
I<rxvt-unicode> also cheats a bit by not providing blocking access to |
166 |
condition variables: code blocking while waiting for a condition will |
167 |
C<die>. This still works with most modules/usages, and blocking calls must |
168 |
not be in an interactive appliation, so it makes sense. |
169 |
|
170 |
=head1 ENVIRONMENT VARIABLES |
171 |
|
172 |
The following environment variables are used by this module: |
173 |
|
174 |
C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
175 |
model gets used. |
176 |
|
177 |
=head1 EXAMPLE |
178 |
|
179 |
The following program uses an io watcher to read data from stdin, a timer |
180 |
to display a message once per second, and a condvar to exit the program |
181 |
when the user enters quit: |
182 |
|
183 |
use AnyEvent; |
184 |
|
185 |
my $cv = AnyEvent->condvar; |
186 |
|
187 |
my $io_watcher = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
188 |
warn "io event <$_[0]>\n"; # will always output <r> |
189 |
chomp (my $input = <STDIN>); # read a line |
190 |
warn "read: $input\n"; # output what has been read |
191 |
$cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
192 |
}); |
193 |
|
194 |
my $time_watcher; # can only be used once |
195 |
|
196 |
sub new_timer { |
197 |
$timer = AnyEvent->timer (after => 1, cb => sub { |
198 |
warn "timeout\n"; # print 'timeout' about every second |
199 |
&new_timer; # and restart the time |
200 |
}); |
201 |
} |
202 |
|
203 |
new_timer; # create first timer |
204 |
|
205 |
$cv->wait; # wait until user enters /^q/i |
206 |
|
207 |
=head1 REAL-WORLD EXAMPLE |
208 |
|
209 |
Consider the L<Net::FCP> module. It features (among others) the following |
210 |
API calls, which are to freenet what HTTP GET requests are to http: |
211 |
|
212 |
my $data = $fcp->client_get ($url); # blocks |
213 |
|
214 |
my $transaction = $fcp->txn_client_get ($url); # does not block |
215 |
$transaction->cb ( sub { ... } ); # set optional result callback |
216 |
my $data = $transaction->result; # possibly blocks |
217 |
|
218 |
The C<client_get> method works like C<LWP::Simple::get>: it requests the |
219 |
given URL and waits till the data has arrived. It is defined to be: |
220 |
|
221 |
sub client_get { $_[0]->txn_client_get ($_[1])->result } |
222 |
|
223 |
And in fact is automatically generated. This is the blocking API of |
224 |
L<Net::FCP>, and it works as simple as in any other, similar, module. |
225 |
|
226 |
More complicated is C<txn_client_get>: It only creates a transaction |
227 |
(completion, result, ...) object and initiates the transaction. |
228 |
|
229 |
my $txn = bless { }, Net::FCP::Txn::; |
230 |
|
231 |
It also creates a condition variable that is used to signal the completion |
232 |
of the request: |
233 |
|
234 |
$txn->{finished} = AnyAvent->condvar; |
235 |
|
236 |
It then creates a socket in non-blocking mode. |
237 |
|
238 |
socket $txn->{fh}, ...; |
239 |
fcntl $txn->{fh}, F_SETFL, O_NONBLOCK; |
240 |
connect $txn->{fh}, ... |
241 |
and !$!{EWOULDBLOCK} |
242 |
and !$!{EINPROGRESS} |
243 |
and Carp::croak "unable to connect: $!\n"; |
244 |
|
245 |
Then it creates a write-watcher which gets called whenever an error occurs |
246 |
or the connection succeeds: |
247 |
|
248 |
$txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
249 |
|
250 |
And returns this transaction object. The C<fh_ready_w> callback gets |
251 |
called as soon as the event loop detects that the socket is ready for |
252 |
writing. |
253 |
|
254 |
The C<fh_ready_w> method makes the socket blocking again, writes the |
255 |
request data and replaces the watcher by a read watcher (waiting for reply |
256 |
data). The actual code is more complicated, but that doesn't matter for |
257 |
this example: |
258 |
|
259 |
fcntl $txn->{fh}, F_SETFL, 0; |
260 |
syswrite $txn->{fh}, $txn->{request} |
261 |
or die "connection or write error"; |
262 |
$txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
263 |
|
264 |
Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
265 |
result and signals any possible waiters that the request ahs finished: |
266 |
|
267 |
sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
268 |
|
269 |
if (end-of-file or data complete) { |
270 |
$txn->{result} = $txn->{buf}; |
271 |
$txn->{finished}->broadcast; |
272 |
$txb->{cb}->($txn) of $txn->{cb}; # also call callback |
273 |
} |
274 |
|
275 |
The C<result> method, finally, just waits for the finished signal (if the |
276 |
request was already finished, it doesn't wait, of course, and returns the |
277 |
data: |
278 |
|
279 |
$txn->{finished}->wait; |
280 |
return $txn->{result}; |
281 |
|
282 |
The actual code goes further and collects all errors (C<die>s, exceptions) |
283 |
that occured during request processing. The C<result> method detects |
284 |
wether an exception as thrown (it is stored inside the $txn object) |
285 |
and just throws the exception, which means connection errors and other |
286 |
problems get reported tot he code that tries to use the result, not in a |
287 |
random callback. |
288 |
|
289 |
All of this enables the following usage styles: |
290 |
|
291 |
1. Blocking: |
292 |
|
293 |
my $data = $fcp->client_get ($url); |
294 |
|
295 |
2. Blocking, but parallelizing: |
296 |
|
297 |
my @datas = map $_->result, |
298 |
map $fcp->txn_client_get ($_), |
299 |
@urls; |
300 |
|
301 |
Both blocking examples work without the module user having to know |
302 |
anything about events. |
303 |
|
304 |
3a. Event-based in a main program, using any support Event module: |
305 |
|
306 |
use Event; |
307 |
|
308 |
$fcp->txn_client_get ($url)->cb (sub { |
309 |
my $txn = shift; |
310 |
my $data = $txn->result; |
311 |
... |
312 |
}); |
313 |
|
314 |
Event::loop; |
315 |
|
316 |
3b. The module user could use AnyEvent, too: |
317 |
|
318 |
use AnyEvent; |
319 |
|
320 |
my $quit = AnyEvent->condvar; |
321 |
|
322 |
$fcp->txn_client_get ($url)->cb (sub { |
323 |
... |
324 |
$quit->broadcast; |
325 |
}); |
326 |
|
327 |
$quit->wait; |
328 |
|
329 |
=head1 SEE ALSO |
330 |
|
331 |
Event modules: L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>. |
332 |
|
333 |
Implementations: L<AnyEvent::Impl::Coro>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>. |
334 |
|
335 |
Nontrivial usage example: L<Net::FCP>. |
336 |
|
337 |
=head1 |
338 |
|
339 |
=cut |
340 |
|
341 |
1 |
342 |
|