… | |
… | |
18 | |
18 | |
19 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
19 | my $w = AnyEvent->condvar; # stores wether a condition was flagged |
20 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
20 | $w->wait; # enters "main loop" till $condvar gets ->broadcast |
21 | $w->broadcast; # wake up current and all future wait's |
21 | $w->broadcast; # wake up current and all future wait's |
22 | |
22 | |
23 | =head1 WHY YOU SHOULD USE THIS MODULE |
23 | =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) |
24 | |
24 | |
25 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
25 | Glib, POE, IO::Async, Event... CPAN offers event models by the dozen |
26 | nowadays. So what is different about AnyEvent? |
26 | nowadays. So what is different about AnyEvent? |
27 | |
27 | |
28 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
28 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
… | |
… | |
38 | programming (waiting for I/O or timer events) without subscribing to a |
38 | programming (waiting for I/O or timer events) without subscribing to a |
39 | religion, a way of living, and most importantly: without forcing your |
39 | religion, a way of living, and most importantly: without forcing your |
40 | module users into the same thing by forcing them to use the same event |
40 | module users into the same thing by forcing them to use the same event |
41 | model you use. |
41 | model you use. |
42 | |
42 | |
43 | For modules like POE or IO::Async (the latter of which is actually |
43 | For modules like POE or IO::Async (which is actually doing all I/O |
44 | named confusingly, as it does neither do I/O nor does it do anything |
|
|
45 | asynchronously...), using them in your module is like joining a |
44 | I<synchronously>...), using them in your module is like joining a |
46 | cult: After you joined, you are dependent on them and you cannot use |
45 | cult: After you joined, you are dependent on them and you cannot use |
47 | anything else, as it is simply incompatible to everything that isn't |
46 | anything else, as it is simply incompatible to everything that isn't |
48 | itself. |
47 | itself. |
49 | |
48 | |
50 | AnyEvent + POE works fine. AnyEvent + Glib works fine. AnyEvent + Tk |
49 | AnyEvent + POE works fine. AnyEvent + Glib works fine. AnyEvent + Tk |
… | |
… | |
58 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
57 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
59 | modules, you get an enourmous amount of code and strict rules you have |
58 | modules, you get an enourmous amount of code and strict rules you have |
60 | to follow. AnyEvent, on the other hand, is lean and to the point by only |
59 | to follow. AnyEvent, on the other hand, is lean and to the point by only |
61 | offering the functionality that is useful, in as thin as a wrapper as |
60 | offering the functionality that is useful, in as thin as a wrapper as |
62 | technically possible. |
61 | technically possible. |
|
|
62 | |
|
|
63 | Of course, if you want lots of policy (this can arguably be somewhat |
|
|
64 | useful) and you want to force your users to use the one and only event |
|
|
65 | model, you should I<not> use this module. |
63 | |
66 | |
64 | |
67 | |
65 | =head1 DESCRIPTION |
68 | =head1 DESCRIPTION |
66 | |
69 | |
67 | L<AnyEvent> provides an identical interface to multiple event loops. This |
70 | L<AnyEvent> provides an identical interface to multiple event loops. This |
… | |
… | |
176 | =item $cv->wait |
179 | =item $cv->wait |
177 | |
180 | |
178 | Wait (blocking if necessary) until the C<< ->broadcast >> method has been |
181 | Wait (blocking if necessary) until the C<< ->broadcast >> method has been |
179 | called on c<$cv>, while servicing other watchers normally. |
182 | called on c<$cv>, while servicing other watchers normally. |
180 | |
183 | |
181 | Not all event models support a blocking wait - some die in that case, so |
|
|
182 | if you are using this from a module, never require a blocking wait, but |
|
|
183 | let the caller decide wether the call will block or not (for example, |
|
|
184 | by coupling condition variables with some kind of request results and |
|
|
185 | supporting callbacks so the caller knows that getting the result will not |
|
|
186 | block, while still suppporting blockign waits if the caller so desires). |
|
|
187 | |
|
|
188 | You can only wait once on a condition - additional calls will return |
184 | You can only wait once on a condition - additional calls will return |
189 | immediately. |
185 | immediately. |
|
|
186 | |
|
|
187 | Not all event models support a blocking wait - some die in that case |
|
|
188 | (programs might want to do that so they stay interactive), so I<if you |
|
|
189 | are using this from a module, never require a blocking wait>, but let the |
|
|
190 | caller decide wether the call will block or not (for example, by coupling |
|
|
191 | condition variables with some kind of request results and supporting |
|
|
192 | callbacks so the caller knows that getting the result will not block, |
|
|
193 | while still suppporting blocking waits if the caller so desires). |
|
|
194 | |
|
|
195 | Another reason I<never> to C<< ->wait >> in a module is that you cannot |
|
|
196 | sensibly have two C<< ->wait >>'s in parallel, as that would require |
|
|
197 | multiple interpreters or coroutines/threads, none of which C<AnyEvent> |
|
|
198 | can supply (the coroutine-aware backends C<Coro::EV> and C<Coro::Event> |
|
|
199 | explicitly support concurrent C<< ->wait >>'s from different coroutines, |
|
|
200 | however). |
190 | |
201 | |
191 | =item $cv->broadcast |
202 | =item $cv->broadcast |
192 | |
203 | |
193 | Flag the condition as ready - a running C<< ->wait >> and all further |
204 | Flag the condition as ready - a running C<< ->wait >> and all further |
194 | calls to C<wait> will return after this method has been called. If nobody |
205 | calls to C<wait> will return after this method has been called. If nobody |
… | |
… | |
249 | |
260 | |
250 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
261 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
251 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
262 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
252 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
263 | AnyEvent::Impl::CoroEvent based on Coro::Event, second best choice. |
253 | AnyEvent::Impl::Event based on Event, also second best choice :) |
264 | AnyEvent::Impl::Event based on Event, also second best choice :) |
254 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
265 | AnyEvent::Impl::Glib based on Glib, third-best choice. |
255 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
266 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
256 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
267 | AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. |
257 | |
268 | |
258 | =item AnyEvent::detect |
269 | =item AnyEvent::detect |
259 | |
270 | |
260 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
271 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
261 | necessary. You should only call this function right before you would have |
272 | necessary. You should only call this function right before you would have |
… | |
… | |
631 | |
642 | |
632 | 1. Blocking: |
643 | 1. Blocking: |
633 | |
644 | |
634 | my $data = $fcp->client_get ($url); |
645 | my $data = $fcp->client_get ($url); |
635 | |
646 | |
636 | 2. Blocking, but parallelizing: |
647 | 2. Blocking, but running in parallel: |
637 | |
648 | |
638 | my @datas = map $_->result, |
649 | my @datas = map $_->result, |
639 | map $fcp->txn_client_get ($_), |
650 | map $fcp->txn_client_get ($_), |
640 | @urls; |
651 | @urls; |
641 | |
652 | |
642 | Both blocking examples work without the module user having to know |
653 | Both blocking examples work without the module user having to know |
643 | anything about events. |
654 | anything about events. |
644 | |
655 | |
645 | 3a. Event-based in a main program, using any support Event module: |
656 | 3a. Event-based in a main program, using any supported event module: |
646 | |
657 | |
647 | use Event; |
658 | use EV; |
648 | |
659 | |
649 | $fcp->txn_client_get ($url)->cb (sub { |
660 | $fcp->txn_client_get ($url)->cb (sub { |
650 | my $txn = shift; |
661 | my $txn = shift; |
651 | my $data = $txn->result; |
662 | my $data = $txn->result; |
652 | ... |
663 | ... |
653 | }); |
664 | }); |
654 | |
665 | |
655 | Event::loop; |
666 | EV::loop; |
656 | |
667 | |
657 | 3b. The module user could use AnyEvent, too: |
668 | 3b. The module user could use AnyEvent, too: |
658 | |
669 | |
659 | use AnyEvent; |
670 | use AnyEvent; |
660 | |
671 | |
… | |
… | |
667 | |
678 | |
668 | $quit->wait; |
679 | $quit->wait; |
669 | |
680 | |
670 | =head1 SEE ALSO |
681 | =head1 SEE ALSO |
671 | |
682 | |
672 | Event modules: L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>. |
683 | Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, |
|
|
684 | L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>. |
673 | |
685 | |
674 | Implementations: L<AnyEvent::Impl::Coro>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>. |
686 | Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>, |
|
|
687 | L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, |
|
|
688 | L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>. |
675 | |
689 | |
676 | Nontrivial usage example: L<Net::FCP>. |
690 | Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. |
677 | |
691 | |
678 | =head1 |
692 | =head1 |
679 | |
693 | |
680 | =cut |
694 | =cut |
681 | |
695 | |