ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/AnyEvent.pm
(Generate patch)

Comparing Coro/Coro/AnyEvent.pm (file contents):
Revision 1.1 by root, Fri May 9 22:04:37 2008 UTC vs.
Revision 1.2 by root, Fri May 9 23:16:50 2008 UTC

5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Coro; 7 use Coro;
8 use Coro::AnyEvent; 8 use Coro::AnyEvent;
9 9
10 # use coro within an Anyevent environment 10 # use coro within an AnyEvent environment
11
12=head1 INTRODUCTION
13
14When one naively starts to use coroutines in Perl, one will quickly run
15into the problem that coroutines that block on a syscall (sleeping,
16reading from a socket etc.) will block all coroutines.
17
18If one then uses an event loop, the problem is that the event loop has
19no knowledge of coroutines and will not run them before it polls for new
20events, again blocking the whole process.
21
22This module integrates coroutines into any event loop supported by
23AnyEvent, combining event-based programming with coroutine-based
24programming in a natural way.
25
26All you have to do is C<use Coro::AnyEvent> and then you can run a
27coroutines freely.
11 28
12=head1 DESCRIPTION 29=head1 DESCRIPTION
13 30
14TODO: 31This module autodetects the event loop used (by relying on L<AnyEvent>)
32and will either automatically defer to the high-performance L<Coro::EV> or
33L<Coro::Event> modules, or will use a generic integration into any event
34loop supported by L<AnyEvent>.
15 35
16This module does two things: First, it offers some utility functions that 36Unfortunately, few event loops (basically only L<EV> and L<Event>) support
17might be useful for coroutines, and secondly, it integrates Coro into the 37this kind of integration well, and therefore AnyEvent cannot offer the
18EV main loop: 38required functionality.
19 39
20Before the process blocks (in EV::loop) to wait for events, this module 40Here is what this module does when it has to work with other event loops:
21will schedule and run all ready (= runnable) coroutines of the same or
22higher priority. After that, it will cede once to a coroutine of lower
23priority, then continue in the event loop.
24 41
25That means that coroutines with the same or higher pripority as the 42Each time a coroutine is put into the ready queue (and there are no other
26coroutine running the main loop will inhibit event processing, while 43coroutines in the ready queue), a timer with an C<after> value of C<0> is
27coroutines of lower priority will get the CPU, but cannot completeley 44registered with AnyEvent.
28inhibit event processing. 45
46This creates something similar to an I<idle> watcher, i.e. a watcher
47that keeps the event loop from blocking but still polls for new
48events. (Unfortunately, some badly designed event loops (e.g. Event::Lib)
49don't support a timeout of C<0> and will always block for a bit).
50
51The callback for that timer will C<cede> to other coroutines of the same
52or higher priority for as long as such coroutines exists. This has the
53effect of running all coroutines that have work to do will all coroutines
54block to wait for external events.
55
56If no coroutines of equal or higher priority are ready, it will cede
57to any coroutine, but only once. This has the effect of running
58lower-priority coroutines as well, but it will not keep higher priority
59coroutines from receiving new events.
60
61The priority used is simply the priority of the coroutine that runs the
62event loop, usually the main program, and the priority is usually C<0>.
63
64As C<unblock_sub> cannot be used, you must not call into the event loop
65recursively (e.g. you must not use AnyEvent condvars in a blocking
66way). This restriction will be lifted in a later version of AnyEvent and
67Coro.
68
69In addition to hooking into C<ready>, this module will also provide a
70C<$Coro::idle> handler that runs the event loop. It is best not to rely on
71this.
29 72
30=cut 73=cut
31 74
32package Coro::AnyEvent; 75package Coro::AnyEvent;
33 76

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines