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 (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 | |
14 | |
28 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
15 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
29 | ... |
16 | ... |
30 | }); |
17 | }); |
31 | |
18 | |
32 | * 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 |
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 |
20 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
39 | $w->broadcast; # wake up current and all future wait's |
21 | $w->broadcast; # wake up current and all future wait's |
40 | |
22 | |
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 |
23 | =head1 DESCRIPTION |
47 | |
24 | |
48 | L<AnyEvent> provides an identical interface to multiple event loops. This |
25 | L<AnyEvent> provides an identical interface to multiple event loops. This |
49 | allows module authors to utilizy an event loop without forcing module |
26 | 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 |
27 | users to use the same event loop (as only a single event loop can coexist |
51 | peacefully at any one time). |
28 | peacefully at any one time). |
52 | |
29 | |
53 | 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 |
54 | module. |
31 | module. |
… | |
… | |
56 | 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 |
57 | 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 |
58 | 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 |
59 | 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 |
60 | 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 |
61 | 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 TIMER WATCHERS |
|
|
95 | |
|
|
96 | You can create a timer 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: |
62 | |
125 | |
63 | =over 4 |
126 | =over 4 |
|
|
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 | =head1 WHAT TO DO IN A MODULE |
|
|
163 | |
|
|
164 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
|
|
165 | freely, but you should not load a specific event module or rely on it. |
|
|
166 | |
|
|
167 | Be careful when you create watchers in the module body - Anyevent will |
|
|
168 | decide which event module to use as soon as the first method is called, so |
|
|
169 | by calling AnyEvent in your module body you force the user of your module |
|
|
170 | to load the event module first. |
|
|
171 | |
|
|
172 | =head1 WHAT TO DO IN THE MAIN PROGRAM |
|
|
173 | |
|
|
174 | There will always be a single main program - the only place that should |
|
|
175 | dictate which event model to use. |
|
|
176 | |
|
|
177 | If it doesn't care, it can just "use AnyEvent" and use it itself, or not |
|
|
178 | do anything special and let AnyEvent decide which implementation to chose. |
|
|
179 | |
|
|
180 | If the main program relies on a specific event model (for example, in Gtk2 |
|
|
181 | programs you have to rely on either Glib or Glib::Event), you should load |
|
|
182 | it before loading AnyEvent or any module that uses it, generally, as early |
|
|
183 | as possible. The reason is that modules might create watchers when they |
|
|
184 | are loaded, and AnyEvent will decide on the event model to use as soon as |
|
|
185 | it creates watchers, and it might chose the wrong one unless you load the |
|
|
186 | correct one yourself. |
|
|
187 | |
|
|
188 | You can chose to use a rather inefficient pure-perl implementation by |
|
|
189 | loading the C<AnyEvent::Impl::Perl> module, but letting AnyEvent chose is |
|
|
190 | generally better. |
64 | |
191 | |
65 | =cut |
192 | =cut |
66 | |
193 | |
67 | package AnyEvent; |
194 | package AnyEvent; |
68 | |
195 | |
69 | no warnings; |
196 | no warnings; |
70 | use strict 'vars'; |
197 | use strict 'vars'; |
71 | use Carp; |
198 | use Carp; |
72 | |
199 | |
73 | our $VERSION = '1.02'; |
200 | our $VERSION = '2.0'; |
74 | our $MODEL; |
201 | our $MODEL; |
75 | |
202 | |
76 | our $AUTOLOAD; |
203 | our $AUTOLOAD; |
77 | our @ISA; |
204 | our @ISA; |
78 | |
205 | |
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
206 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
80 | |
207 | |
81 | our @REGISTRY; |
208 | our @REGISTRY; |
82 | |
209 | |
83 | my @models = ( |
210 | my @models = ( |
84 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
211 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
85 | [Event:: => AnyEvent::Impl::Event::], |
212 | [Event:: => AnyEvent::Impl::Event::], |
86 | [Glib:: => AnyEvent::Impl::Glib::], |
213 | [Glib:: => AnyEvent::Impl::Glib::], |
87 | [Tk:: => AnyEvent::Impl::Tk::], |
214 | [Tk:: => AnyEvent::Impl::Tk::], |
|
|
215 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
88 | ); |
216 | ); |
89 | |
217 | |
90 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
218 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); |
91 | |
219 | |
92 | sub AUTOLOAD { |
220 | sub AUTOLOAD { |
93 | $AUTOLOAD =~ s/.*://; |
221 | $AUTOLOAD =~ s/.*://; |
94 | |
222 | |
95 | $method{$AUTOLOAD} |
223 | $method{$AUTOLOAD} |
… | |
… | |
129 | |
257 | |
130 | my $class = shift; |
258 | my $class = shift; |
131 | $class->$AUTOLOAD (@_); |
259 | $class->$AUTOLOAD (@_); |
132 | } |
260 | } |
133 | |
261 | |
134 | =back |
|
|
135 | |
|
|
136 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
262 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
137 | |
263 | |
138 | If you need to support another event library which isn't directly |
264 | 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 |
265 | supported by AnyEvent, you can supply your own interface to it by |
140 | pushing, before the first watcher gets created, the package name of |
266 | pushing, before the first watcher gets created, the package name of |
… | |
… | |
144 | |
270 | |
145 | Example: |
271 | Example: |
146 | |
272 | |
147 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
273 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
148 | |
274 | |
149 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> module |
275 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
150 | when it finds the C<urxvt> module is loaded. When AnyEvent is loaded and |
276 | package/class when it finds the C<urxvt> package/module is loaded. When |
151 | requested to find a suitable event model, it will first check for the |
277 | AnyEvent is loaded and asked to find a suitable event model, it will |
152 | urxvt module. |
278 | first check for the presence of urxvt. |
153 | |
279 | |
|
|
280 | The class should prove implementations for all watcher types (see |
|
|
281 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
|
|
282 | (Source code) and so on for actual examples, use C<perldoc -m |
|
|
283 | AnyEvent::Impl::Glib> to see the sources). |
|
|
284 | |
154 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) uses |
285 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
155 | the above line exactly. An interface isn't included in AnyEvent |
286 | uses the above line as-is. An interface isn't included in AnyEvent |
156 | because it doesn't make sense outside the embedded interpreter inside |
287 | 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 |
288 | I<rxvt-unicode>, and it is updated and maintained as part of the |
158 | I<rxvt-unicode> distribution. |
289 | I<rxvt-unicode> distribution. |
|
|
290 | |
|
|
291 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
|
|
292 | condition variables: code blocking while waiting for a condition will |
|
|
293 | C<die>. This still works with most modules/usages, and blocking calls must |
|
|
294 | not be in an interactive appliation, so it makes sense. |
159 | |
295 | |
160 | =head1 ENVIRONMENT VARIABLES |
296 | =head1 ENVIRONMENT VARIABLES |
161 | |
297 | |
162 | The following environment variables are used by this module: |
298 | The following environment variables are used by this module: |
163 | |
299 | |