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

Comparing cvsroot/Coro-Multicore/Multicore.pm (file contents):
Revision 1.10 by root, Mon Jul 27 14:32:33 2015 UTC vs.
Revision 1.11 by root, Wed Jul 29 18:36:33 2015 UTC

7 # when you DO control the main event loop, e.g. in the main program 7 # when you DO control the main event loop, e.g. in the main program
8 8
9 use Coro::Multicore; # enable by default 9 use Coro::Multicore; # enable by default
10 10
11 Coro::Multicore::scoped_disable; 11 Coro::Multicore::scoped_disable;
12 AE::cv->recv; # or EV::loop, AnyEvent::Loop::run, Event::loop, ... 12 AE::cv->recv; # or EV::run, AnyEvent::Loop::run, Event::loop, ...
13 13
14 # when you DO NOT control the event loop, e.g. in a module on CPAN 14 # when you DO NOT control the event loop, e.g. in a module on CPAN
15 # do nothing (see HOW TO USE IT) or something like this: 15 # do nothing (see HOW TO USE IT) or something like this:
16 16
17 use Coro::Multicore (); # disable by default 17 use Coro::Multicore (); # disable by default
96 use Coro; 96 use Coro;
97 use Coro::Multicore; 97 use Coro::Multicore;
98 98
99 async { 99 async {
100 Coro::Multicore::scoped_disable; 100 Coro::Multicore::scoped_disable;
101 EV::loop; 101 EV::run;
102 }; 102 };
103 103
104 # do something else 104 # do something else
105 105
106 # example 2, run event loop as main program 106 # example 2, run event loop as main program
111 111
112 Coro::Multicore::scoped_disable; 112 Coro::Multicore::scoped_disable;
113 113
114 ... initialisation 114 ... initialisation
115 115
116 EV::loop; 116 EV::run;
117 117
118The latter form is usually better and more idiomatic - the main thread is 118The latter form is usually better and more idiomatic - the main thread is
119the best place to run the event loop. 119the best place to run the event loop.
120
121Often you want to do some initialisation before running the event
122loop. The most efficient way to do that is to put your intialisation code
123(and main program) into its own thread and run the event loop in your main
124program:
125
126 use AnyEvent::Loop;
127 use Coro::Multicore; # enable by default
128
129 async {
130 load_data;
131 do_other_init;
132 bind_socket;
133 ...
134 };
135
136 Coro::Multicore::scoped_disable;
137 AnyEvent::Loop::run;
138
139This has the effect of running the event loop first, so the initialisation
140code can block if it wants to.
141
142If this is too cumbersome but you still want to make sure you can
143call blocking functions before entering the event loop, you can keep
144C<Coro::Multicore> disabled till you cna run the event loop:
145
146 use AnyEvent::Loop;
147 use Coro::Multicore (); # disable by default
148
149 load_data;
150 do_other_init;
151 bind_socket;
152 ...
153
154 Coro::Multicore::scoped_disable; # disable for event loop
155 Coro::Multicore::enable 1; # enable for the rest of the program
156 AnyEvent::Loop::run;
120 157
121=head2 USE IT IN A MODULE 158=head2 USE IT IN A MODULE
122 159
123When you I<do not> control the event loop, for example, because you want 160When you I<do not> control the event loop, for example, because you want
124to use this from a module you published on CPAN, then the previous method 161to use this from a module you published on CPAN, then the previous method

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines