… | |
… | |
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 | |
24 | When one naively starts to use threads in Perl, one will quickly run |
24 | When one naively starts to use threads in Perl, one will quickly run |
25 | into the problem that threads that block on a syscall (sleeping, |
25 | into the problem that threads which block on a syscall (sleeping, |
26 | reading from a socket etc.) will block all threads. |
26 | reading from a socket etc.) will block all threads. |
27 | |
27 | |
28 | If one then uses an event loop, the problem is that the event loop has |
28 | If one then uses an event loop, the problem is that the event loop has |
29 | no knowledge of threads and will not run them before it polls for new |
29 | no knowledge of threads and will not run them before it polls for new |
30 | events, again blocking the whole process. |
30 | events, 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 | |
53 | For performance reasons, it is recommended that the main program or |
53 | For performance reasons, it is recommended that the main program or |
54 | something else runs the event loop of the event model you use, i.e. |
54 | something 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 | |
82 | You can also do nothing, in which case Coro:AnyEvent will invoke the event |
82 | You can also do nothing, in which case Coro::AnyEvent will invoke the event |
83 | loop as needed, which is less efficient, but sometimes very convenient. |
83 | loop as needed, which is less efficient, but sometimes very convenient. |
84 | |
84 | |
85 | What you I<MUST NOT DO EVER> is to block inside an event loop |
85 | What you I<MUST NOT EVER DO> is to block inside an event loop |
86 | callback. The reason is that most event loops are not reentrant and this |
86 | callback. The reason is that most event loops are not reentrant and |
87 | can cause a deadlock at best and corrupt memory at worst. |
87 | this can cause a deadlock at best and corrupt memory at worst. |
88 | |
88 | |
89 | Coro will try to catch you when you block in the event loop |
89 | Coro 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 |
91 | only works when you do not run your own event loop. |
91 | only 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 | |
110 | Keep in mind that, as shipped, Coro and Coro::AnyEvent only work with |
110 | Keep in mind that, as shipped, Coro and Coro::AnyEvent only work with |
111 | AnyEvent, and only when AnyEvent is actually used (i.e. initialised), so |
111 | AnyEvent, and only when AnyEvent is actually used (i.e. initialised), so |
112 | this will not work: |
112 | this will not work: |
113 | |
113 | |
… | |
… | |
425 | that implements it, basically), you must not call into the event |
425 | that implements it, basically), you must not call into the event |
426 | loop recursively from any coroutine. This is not usually a difficult |
426 | loop recursively from any coroutine. This is not usually a difficult |
427 | restriction to live with, just use condvars, C<unblock_sub> or other means |
427 | restriction to live with, just use condvars, C<unblock_sub> or other means |
428 | of inter-coroutine-communications. |
428 | of inter-coroutine-communications. |
429 | |
429 | |
430 | If you use a module that supports AnyEvent (or uses the same event loop |
430 | If you use a module that supports AnyEvent (or uses the same event |
431 | as AnyEvent, making the compatible), and it offers callbacks of any kind, |
431 | loop as AnyEvent, making it implicitly compatible), and it offers |
432 | then you must not block in them, either (or use e.g. C<unblock_sub>), see |
432 | callbacks of any kind, then you must not block in them, either (or use |
433 | the description of C<unblock_sub> in the L<Coro> module. |
433 | e.g. C<unblock_sub>), see the description of C<unblock_sub> in the |
|
|
434 | L<Coro> module. |
434 | |
435 | |
435 | This also means that you should load the module as early as possible, |
436 | This also means that you should load the module as early as possible, |
436 | as only condvars created after this module has been loaded will work |
437 | as only condvars created after this module has been loaded will work |
437 | correctly. |
438 | correctly. |
438 | |
439 | |