… | |
… | |
4 | |
4 | |
5 | Event, Coro, Glib, Tk - various supported event loops |
5 | Event, Coro, Glib, Tk - 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->timer (fh => ..., poll => "[rw]+", cb => sub { |
11 | my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { |
12 | my ($poll_got) = @_; |
12 | my ($poll_got) = @_; |
13 | ... |
13 | ... |
14 | }); |
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 | |
15 | my $w = AnyEvent->io (after => $seconds, cb => sub { |
28 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
16 | ... |
29 | ... |
17 | }); |
30 | }); |
18 | |
31 | |
19 | # watchers get canceled whenever $w is destroyed |
32 | * io and time watchers get canceled whenever $w is destroyed, so keep a copy |
20 | # only one watcher per $fh and $poll type is allowed |
33 | |
21 | # (i.e. on a socket you cna have one r + one w or one rw |
34 | * timers can only be used once and must be recreated for repeated |
22 | # watcher, not any more. |
35 | operation (limitation by Glib and Tk). |
23 | # timers can only be used once |
|
|
24 | |
36 | |
25 | my $w = AnyEvent->condvar; # kind of main loop replacement |
37 | my $w = AnyEvent->condvar; # kind of main loop replacement |
26 | # can only be used once |
|
|
27 | $w->wait; # enters main loop till $condvar gets ->send |
38 | $w->wait; # enters main loop till $condvar gets ->broadcast |
28 | $w->broadcast; # wake up waiting and future wait's |
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. |
29 | |
45 | |
30 | =head1 DESCRIPTION |
46 | =head1 DESCRIPTION |
31 | |
47 | |
32 | L<AnyEvent> provides an identical interface to multiple event loops. This |
48 | L<AnyEvent> provides an identical interface to multiple event loops. This |
33 | allows module authors to utilizy an event loop without forcing module |
49 | allows module authors to utilizy an event loop without forcing module |
… | |
… | |
52 | |
68 | |
53 | no warnings; |
69 | no warnings; |
54 | use strict 'vars'; |
70 | use strict 'vars'; |
55 | use Carp; |
71 | use Carp; |
56 | |
72 | |
57 | our $VERSION = 0.2; |
73 | our $VERSION = '1.02'; |
58 | our $MODEL; |
74 | our $MODEL; |
59 | |
75 | |
60 | our $AUTOLOAD; |
76 | our $AUTOLOAD; |
61 | our @ISA; |
77 | our @ISA; |
62 | |
78 | |
|
|
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
|
|
80 | |
|
|
81 | our @REGISTRY; |
|
|
82 | |
63 | my @models = ( |
83 | my @models = ( |
64 | [Coro => Coro::Event::], |
84 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
65 | [Event => Event::], |
85 | [Event:: => AnyEvent::Impl::Event::], |
66 | [Glib => Glib::], |
86 | [Glib:: => AnyEvent::Impl::Glib::], |
67 | [Tk => Tk::], |
87 | [Tk:: => AnyEvent::Impl::Tk::], |
68 | ); |
88 | ); |
69 | |
89 | |
70 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
90 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
71 | |
91 | |
72 | sub AUTOLOAD { |
92 | sub AUTOLOAD { |
… | |
… | |
75 | $method{$AUTOLOAD} |
95 | $method{$AUTOLOAD} |
76 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
96 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
77 | |
97 | |
78 | unless ($MODEL) { |
98 | unless ($MODEL) { |
79 | # check for already loaded models |
99 | # check for already loaded models |
80 | for (@models) { |
100 | for (@REGISTRY, @models) { |
81 | my ($model, $package) = @$_; |
101 | my ($package, $model) = @$_; |
82 | if (scalar keys %{ *{"$package\::"} }) { |
102 | if (${"$package\::VERSION"} > 0) { |
83 | eval "require AnyEvent::Impl::$model" |
103 | if (eval "require $model") { |
|
|
104 | $MODEL = $model; |
|
|
105 | warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; |
84 | or die; |
106 | last; |
85 | |
107 | } |
86 | last if $MODEL; |
|
|
87 | } |
108 | } |
88 | } |
109 | } |
89 | |
110 | |
90 | unless ($MODEL) { |
111 | unless ($MODEL) { |
91 | # try to load a model |
112 | # try to load a model |
92 | |
113 | |
93 | for (@models) { |
114 | for (@REGISTRY, @models) { |
94 | my ($model, $package) = @$_; |
115 | my ($package, $model) = @$_; |
95 | eval "require AnyEvent::Impl::$model" |
116 | if (eval "require $model") { |
|
|
117 | $MODEL = $model; |
|
|
118 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
96 | or die; |
119 | last; |
97 | |
120 | } |
98 | last if $MODEL; |
|
|
99 | } |
121 | } |
100 | |
122 | |
101 | $MODEL |
123 | $MODEL |
102 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
124 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
103 | } |
125 | } |
… | |
… | |
108 | my $class = shift; |
130 | my $class = shift; |
109 | $class->$AUTOLOAD (@_); |
131 | $class->$AUTOLOAD (@_); |
110 | } |
132 | } |
111 | |
133 | |
112 | =back |
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 watch 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::> module |
|
|
150 | when it finds the C<urxvt> module is loaded. When AnyEvent is loaded and |
|
|
151 | requested to find a suitable event model, it will first check for the |
|
|
152 | urxvt module. |
|
|
153 | |
|
|
154 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) uses |
|
|
155 | the above line exactly. An interface isn't included in AnyEvent |
|
|
156 | because it doesn't make sense outside the embedded interpreter inside |
|
|
157 | I<rxvt-unicode>, and it is updated and maintained as part of the |
|
|
158 | I<rxvt-unicode> distribution. |
|
|
159 | |
|
|
160 | =head1 ENVIRONMENT VARIABLES |
|
|
161 | |
|
|
162 | The following environment variables are used by this module: |
|
|
163 | |
|
|
164 | C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
|
|
165 | model gets used. |
113 | |
166 | |
114 | =head1 EXAMPLE |
167 | =head1 EXAMPLE |
115 | |
168 | |
116 | The following program uses an io watcher to read data from stdin, a timer |
169 | The following program uses an io watcher to read data from stdin, a timer |
117 | to display a message once per second, and a condvar to exit the program |
170 | to display a message once per second, and a condvar to exit the program |
… | |
… | |
139 | |
192 | |
140 | new_timer; # create first timer |
193 | new_timer; # create first timer |
141 | |
194 | |
142 | $cv->wait; # wait until user enters /^q/i |
195 | $cv->wait; # wait until user enters /^q/i |
143 | |
196 | |
|
|
197 | =head1 REAL-WORLD EXAMPLE |
|
|
198 | |
|
|
199 | Consider the L<Net::FCP> module. It features (among others) the following |
|
|
200 | API calls, which are to freenet what HTTP GET requests are to http: |
|
|
201 | |
|
|
202 | my $data = $fcp->client_get ($url); # blocks |
|
|
203 | |
|
|
204 | my $transaction = $fcp->txn_client_get ($url); # does not block |
|
|
205 | $transaction->cb ( sub { ... } ); # set optional result callback |
|
|
206 | my $data = $transaction->result; # possibly blocks |
|
|
207 | |
|
|
208 | The C<client_get> method works like C<LWP::Simple::get>: it requests the |
|
|
209 | given URL and waits till the data has arrived. It is defined to be: |
|
|
210 | |
|
|
211 | sub client_get { $_[0]->txn_client_get ($_[1])->result } |
|
|
212 | |
|
|
213 | And in fact is automatically generated. This is the blocking API of |
|
|
214 | L<Net::FCP>, and it works as simple as in any other, similar, module. |
|
|
215 | |
|
|
216 | More complicated is C<txn_client_get>: It only creates a transaction |
|
|
217 | (completion, result, ...) object and initiates the transaction. |
|
|
218 | |
|
|
219 | my $txn = bless { }, Net::FCP::Txn::; |
|
|
220 | |
|
|
221 | It also creates a condition variable that is used to signal the completion |
|
|
222 | of the request: |
|
|
223 | |
|
|
224 | $txn->{finished} = AnyAvent->condvar; |
|
|
225 | |
|
|
226 | It then creates a socket in non-blocking mode. |
|
|
227 | |
|
|
228 | socket $txn->{fh}, ...; |
|
|
229 | fcntl $txn->{fh}, F_SETFL, O_NONBLOCK; |
|
|
230 | connect $txn->{fh}, ... |
|
|
231 | and !$!{EWOULDBLOCK} |
|
|
232 | and !$!{EINPROGRESS} |
|
|
233 | and Carp::croak "unable to connect: $!\n"; |
|
|
234 | |
|
|
235 | Then it creates a write-watcher which gets called whenever an error occurs |
|
|
236 | or the connection succeeds: |
|
|
237 | |
|
|
238 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); |
|
|
239 | |
|
|
240 | And returns this transaction object. The C<fh_ready_w> callback gets |
|
|
241 | called as soon as the event loop detects that the socket is ready for |
|
|
242 | writing. |
|
|
243 | |
|
|
244 | The C<fh_ready_w> method makes the socket blocking again, writes the |
|
|
245 | request data and replaces the watcher by a read watcher (waiting for reply |
|
|
246 | data). The actual code is more complicated, but that doesn't matter for |
|
|
247 | this example: |
|
|
248 | |
|
|
249 | fcntl $txn->{fh}, F_SETFL, 0; |
|
|
250 | syswrite $txn->{fh}, $txn->{request} |
|
|
251 | or die "connection or write error"; |
|
|
252 | $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'r', cb => sub { $txn->fh_ready_r }); |
|
|
253 | |
|
|
254 | Again, C<fh_ready_r> waits till all data has arrived, and then stores the |
|
|
255 | result and signals any possible waiters that the request ahs finished: |
|
|
256 | |
|
|
257 | sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; |
|
|
258 | |
|
|
259 | if (end-of-file or data complete) { |
|
|
260 | $txn->{result} = $txn->{buf}; |
|
|
261 | $txn->{finished}->broadcast; |
|
|
262 | $txb->{cb}->($txn) of $txn->{cb}; # also call callback |
|
|
263 | } |
|
|
264 | |
|
|
265 | The C<result> method, finally, just waits for the finished signal (if the |
|
|
266 | request was already finished, it doesn't wait, of course, and returns the |
|
|
267 | data: |
|
|
268 | |
|
|
269 | $txn->{finished}->wait; |
|
|
270 | return $txn->{result}; |
|
|
271 | |
|
|
272 | The actual code goes further and collects all errors (C<die>s, exceptions) |
|
|
273 | that occured during request processing. The C<result> method detects |
|
|
274 | wether an exception as thrown (it is stored inside the $txn object) |
|
|
275 | and just throws the exception, which means connection errors and other |
|
|
276 | problems get reported tot he code that tries to use the result, not in a |
|
|
277 | random callback. |
|
|
278 | |
|
|
279 | All of this enables the following usage styles: |
|
|
280 | |
|
|
281 | 1. Blocking: |
|
|
282 | |
|
|
283 | my $data = $fcp->client_get ($url); |
|
|
284 | |
|
|
285 | 2. Blocking, but parallelizing: |
|
|
286 | |
|
|
287 | my @datas = map $_->result, |
|
|
288 | map $fcp->txn_client_get ($_), |
|
|
289 | @urls; |
|
|
290 | |
|
|
291 | Both blocking examples work without the module user having to know |
|
|
292 | anything about events. |
|
|
293 | |
|
|
294 | 3a. Event-based in a main program, using any support Event module: |
|
|
295 | |
|
|
296 | use Event; |
|
|
297 | |
|
|
298 | $fcp->txn_client_get ($url)->cb (sub { |
|
|
299 | my $txn = shift; |
|
|
300 | my $data = $txn->result; |
|
|
301 | ... |
|
|
302 | }); |
|
|
303 | |
|
|
304 | Event::loop; |
|
|
305 | |
|
|
306 | 3b. The module user could use AnyEvent, too: |
|
|
307 | |
|
|
308 | use AnyEvent; |
|
|
309 | |
|
|
310 | my $quit = AnyEvent->condvar; |
|
|
311 | |
|
|
312 | $fcp->txn_client_get ($url)->cb (sub { |
|
|
313 | ... |
|
|
314 | $quit->broadcast; |
|
|
315 | }); |
|
|
316 | |
|
|
317 | $quit->wait; |
|
|
318 | |
144 | =head1 SEE ALSO |
319 | =head1 SEE ALSO |
145 | |
320 | |
146 | L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>, |
321 | Event modules: L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>. |
147 | L<AnyEvent::Impl::Coro>, |
322 | |
148 | L<AnyEvent::Impl::Event>, |
323 | Implementations: L<AnyEvent::Impl::Coro>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>. |
149 | L<AnyEvent::Impl::Glib>, |
324 | |
150 | L<AnyEvent::Impl::Tk>. |
325 | Nontrivial usage example: L<Net::FCP>. |
151 | |
326 | |
152 | =head1 |
327 | =head1 |
153 | |
328 | |
154 | =cut |
329 | =cut |
155 | |
330 | |