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.76 by root, Fri Nov 11 20:22:08 2011 UTC vs.
Revision 1.77 by root, Thu Dec 22 01:33:48 2011 UTC

6 6
7 use Coro; 7 use Coro;
8 use AnyEvent; 8 use AnyEvent;
9 # using both Coro and AnyEvent will automatically load Coro::AnyEvent 9 # using both Coro and AnyEvent will automatically load Coro::AnyEvent
10 10
11 # or load it manually for it's utility functions: 11 # or load it manually for its utility functions:
12 use Coro::AnyEvent; 12 use Coro::AnyEvent;
13 13
14 Coro::AnyEvent::sleep 5; # block current thread for 5s 14 Coro::AnyEvent::sleep 5; # block current thread for 5s
15 Coro::AnyEvent::poll; # poll for new events once 15 Coro::AnyEvent::poll; # poll for new events once
16 Coro::AnyEvent::idle; # block until process no longer busy 16 Coro::AnyEvent::idle; # block until process no longer busy
20 or die "fh didn't become readable within 60 seconds\n"; 20 or die "fh didn't become readable within 60 seconds\n";
21 21
22=head1 DESCRIPTION 22=head1 DESCRIPTION
23 23
24When one naively starts to use threads in Perl, one will quickly run 24When one naively starts to use threads in Perl, one will quickly run
25into the problem that threads that block on a syscall (sleeping, 25into the problem that threads which block on a syscall (sleeping,
26reading from a socket etc.) will block all threads. 26reading from a socket etc.) will block all threads.
27 27
28If one then uses an event loop, the problem is that the event loop has 28If one then uses an event loop, the problem is that the event loop has
29no knowledge of threads and will not run them before it polls for new 29no knowledge of threads and will not run them before it polls for new
30events, again blocking the whole process. 30events, again blocking the whole process.
49=head1 USAGE 49=head1 USAGE
50 50
51=head2 RUN AN EVENT LOOP - OR NOT? 51=head2 RUN AN EVENT LOOP - OR NOT?
52 52
53For performance reasons, it is recommended that the main program or 53For performance reasons, it is recommended that the main program or
54something else runs the event loop of the event model you use, i.e. 54something else run the event loop of the event model you use, i.e.
55 55
56 use Gtk2; # <- the event model 56 use Gtk2; # <- the event model
57 use AnyEvent; 57 use AnyEvent;
58 use Coro: 58 use Coro:
59 59
77 use Coro::AnyEvent; 77 use Coro::AnyEvent;
78 Coro::AnyEvent::sleep 1; 78 Coro::AnyEvent::sleep 1;
79 print "ping...\n"; 79 print "ping...\n";
80 } 80 }
81 81
82You can also do nothing, in which case Coro:AnyEvent will invoke the event 82You can also do nothing, in which case Coro::AnyEvent will invoke the event
83loop as needed, which is less efficient, but sometimes very convenient. 83loop as needed, which is less efficient, but sometimes very convenient.
84 84
85What you I<MUST NOT DO EVER> is to block inside an event loop 85What you I<MUST NOT EVER DO> is to block inside an event loop
86callback. The reason is that most event loops are not reentrant and this 86callback. The reason is that most event loops are not reentrant and
87can cause a deadlock at best and corrupt memory at worst. 87this can cause a deadlock at best and corrupt memory at worst.
88 88
89Coro will try to catch you when you block in the event loop 89Coro will try to catch you when you block in the event loop
90("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and 90("FATAL:$Coro::IDLE blocked itself"), but this is just best effort and
91only works when you do not run your own event loop. 91only works when you do not run your own event loop.
92 92
103 { 103 {
104 my $int_w = AnyEvent->signal (signal => "INT", cb => Coro::rouse_cb); 104 my $int_w = AnyEvent->signal (signal => "INT", cb => Coro::rouse_cb);
105 Coro::rouse_wait; 105 Coro::rouse_wait;
106 } 106 }
107 107
108=head2 EVENT MODULES OTHER THEN ANYEVENT 108=head2 EVENT MODULES OTHER THAN ANYEVENT
109 109
110Keep in mind that, as shipped, Coro and Coro::AnyEvent only work with 110Keep in mind that, as shipped, Coro and Coro::AnyEvent only work with
111AnyEvent, and only when AnyEvent is actually used (i.e. initialised), so 111AnyEvent, and only when AnyEvent is actually used (i.e. initialised), so
112this will not work: 112this will not work:
113 113
425that implements it, basically), you must not call into the event 425that implements it, basically), you must not call into the event
426loop recursively from any coroutine. This is not usually a difficult 426loop recursively from any coroutine. This is not usually a difficult
427restriction to live with, just use condvars, C<unblock_sub> or other means 427restriction to live with, just use condvars, C<unblock_sub> or other means
428of inter-coroutine-communications. 428of inter-coroutine-communications.
429 429
430If you use a module that supports AnyEvent (or uses the same event loop 430If you use a module that supports AnyEvent (or uses the same event
431as AnyEvent, making the compatible), and it offers callbacks of any kind, 431loop as AnyEvent, making it implicitly compatible), and it offers
432then you must not block in them, either (or use e.g. C<unblock_sub>), see 432callbacks of any kind, then you must not block in them, either (or use
433the description of C<unblock_sub> in the L<Coro> module. 433e.g. C<unblock_sub>), see the description of C<unblock_sub> in the
434L<Coro> module.
434 435
435This also means that you should load the module as early as possible, 436This also means that you should load the module as early as possible,
436as only condvars created after this module has been loaded will work 437as only condvars created after this module has been loaded will work
437correctly. 438correctly.
438 439

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines