… | |
… | |
6 | |
6 | |
7 | use AnyEvent; |
7 | use AnyEvent; |
8 | use AnyEvent::Fork::Pool; |
8 | use AnyEvent::Fork::Pool; |
9 | # use AnyEvent::Fork is not needed |
9 | # use AnyEvent::Fork is not needed |
10 | |
10 | |
11 | # all parameters with default values |
11 | # all possible parameters shown, with default values |
12 | my $pool = AnyEvent::Fork |
12 | my $pool = AnyEvent::Fork |
13 | ->new |
13 | ->new |
14 | ->require ("MyWorker") |
14 | ->require ("MyWorker") |
15 | ->AnyEvent::Fork::Pool::run ( |
15 | ->AnyEvent::Fork::Pool::run ( |
16 | "MyWorker::run", # the worker function |
16 | "MyWorker::run", # the worker function |
17 | |
17 | |
18 | # pool management |
18 | # pool management |
19 | max => 4, # absolute maximum # of processes |
19 | max => 4, # absolute maximum # of processes |
20 | idle => 2, # minimum # of idle processes |
20 | idle => 0, # minimum # of idle processes |
21 | load => 2, # queue at most this number of jobs per process |
21 | load => 2, # queue at most this number of jobs per process |
22 | start => 0.1, # wait this many seconds before starting a new process |
22 | start => 0.1, # wait this many seconds before starting a new process |
23 | stop => 1, # wait this many seconds before stopping an idle process |
23 | stop => 10, # wait this many seconds before stopping an idle process |
24 | on_destroy => (my $finish = AE::cv), # called when object is destroyed |
24 | on_destroy => (my $finish = AE::cv), # called when object is destroyed |
25 | |
25 | |
26 | # parameters passed to AnyEvent::Fork::RPC |
26 | # parameters passed to AnyEvent::Fork::RPC |
27 | async => 0, |
27 | async => 0, |
28 | on_error => sub { die "FATAL: $_[0]\n" }, |
28 | on_error => sub { die "FATAL: $_[0]\n" }, |
… | |
… | |
41 | |
41 | |
42 | $finish->recv; |
42 | $finish->recv; |
43 | |
43 | |
44 | =head1 DESCRIPTION |
44 | =head1 DESCRIPTION |
45 | |
45 | |
46 | This module uses processes created via L<AnyEvent::Fork> and the RPC |
46 | This module uses processes created via L<AnyEvent::Fork> (or |
47 | protocol implement in L<AnyEvent::Fork::RPC> to create a load-balanced |
47 | L<AnyEvent::Fork::Remote>) and the RPC protocol implement in |
48 | pool of processes that handles jobs. |
48 | L<AnyEvent::Fork::RPC> to create a load-balanced pool of processes that |
|
|
49 | handles jobs. |
49 | |
50 | |
50 | Understanding of L<AnyEvent::Fork> is helpful but not critical to be able |
51 | Understanding of L<AnyEvent::Fork> is helpful but not critical to be able |
51 | to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC> |
52 | to use this module, but a thorough understanding of L<AnyEvent::Fork::RPC> |
52 | is, as it defines the actual API that needs to be implemented in the |
53 | is, as it defines the actual API that needs to be implemented in the |
53 | children. |
54 | worker processes. |
54 | |
|
|
55 | =head1 EXAMPLES |
|
|
56 | |
55 | |
57 | =head1 PARENT USAGE |
56 | =head1 PARENT USAGE |
|
|
57 | |
|
|
58 | To create a pool, you first have to create a L<AnyEvent::Fork> object - |
|
|
59 | this object becomes your template process. Whenever a new worker process |
|
|
60 | is needed, it is forked from this template process. Then you need to |
|
|
61 | "hand off" this template process to the C<AnyEvent::Fork::Pool> module by |
|
|
62 | calling its run method on it: |
|
|
63 | |
|
|
64 | my $template = AnyEvent::Fork |
|
|
65 | ->new |
|
|
66 | ->require ("SomeModule", "MyWorkerModule"); |
|
|
67 | |
|
|
68 | my $pool = $template->AnyEvent::Fork::Pool::run ("MyWorkerModule::myfunction"); |
|
|
69 | |
|
|
70 | The pool "object" is not a regular Perl object, but a code reference that |
|
|
71 | you can call and that works roughly like calling the worker function |
|
|
72 | directly, except that it returns nothing but instead you need to specify a |
|
|
73 | callback to be invoked once results are in: |
|
|
74 | |
|
|
75 | $pool->(1, 2, 3, sub { warn "myfunction(1,2,3) returned @_" }); |
58 | |
76 | |
59 | =over 4 |
77 | =over 4 |
60 | |
78 | |
61 | =cut |
79 | =cut |
62 | |
80 | |
… | |
… | |
68 | |
86 | |
69 | use Guard (); |
87 | use Guard (); |
70 | use Array::Heap (); |
88 | use Array::Heap (); |
71 | |
89 | |
72 | use AnyEvent; |
90 | use AnyEvent; |
|
|
91 | # explicit version on next line, as some cpan-testers test with the 0.1 version, |
|
|
92 | # ignoring dependencies, and this line will at least give a clear indication of that. |
73 | use AnyEvent::Fork; # we don't actually depend on it, this is for convenience |
93 | use AnyEvent::Fork 0.6; # we don't actually depend on it, this is for convenience |
74 | use AnyEvent::Fork::RPC; |
94 | use AnyEvent::Fork::RPC; |
75 | |
95 | |
76 | # these are used for the first and last argument of events |
96 | # these are used for the first and last argument of events |
77 | # in the hope of not colliding. yes, I don't like it either, |
97 | # in the hope of not colliding. yes, I don't like it either, |
78 | # but didn't come up with an obviously better alternative. |
98 | # but didn't come up with an obviously better alternative. |
79 | my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; |
99 | my $magic0 = ':t6Z@HK1N%Dx@_7?=~-7NQgWDdAs6a,jFN=wLO0*jD*1%P'; |
80 | my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; |
100 | my $magic1 = '<~53rexz.U`!]X[A235^"fyEoiTF\T~oH1l/N6+Djep9b~bI9`\1x%B~vWO1q*'; |
81 | |
101 | |
82 | our $VERSION = 0.1; |
102 | our $VERSION = 1.1; |
83 | |
103 | |
84 | =item my $rpc = AnyEvent::Fork::Pool::run $fork, $function, [key => value...] |
104 | =item my $pool = AnyEvent::Fork::Pool::run $fork, $function, [key => value...] |
85 | |
105 | |
86 | The traditional way to call it. But it is way cooler to call it in the |
106 | The traditional way to call the pool creation function. But it is way |
87 | following way: |
107 | cooler to call it in the following way: |
88 | |
108 | |
89 | =item my $rpc = $fork->AnyEvent::Fork::Pool::run ($function, [key => value...]) |
109 | =item my $pool = $fork->AnyEvent::Fork::Pool::run ($function, [key => value...]) |
90 | |
110 | |
91 | Creates a new pool object with the specified C<$function> as function |
111 | Creates a new pool object with the specified C<$function> as function |
92 | (name) to call for each request. The pool uses the C<$fork> object as the |
112 | (name) to call for each request. The pool uses the C<$fork> object as the |
93 | template when creating worker processes. |
113 | template when creating worker processes. |
94 | |
114 | |
… | |
… | |
103 | |
123 | |
104 | =item Pool Management |
124 | =item Pool Management |
105 | |
125 | |
106 | The pool consists of a certain number of worker processes. These options |
126 | The pool consists of a certain number of worker processes. These options |
107 | decide how many of these processes exist and when they are started and |
127 | decide how many of these processes exist and when they are started and |
108 | stopp.ed |
128 | stopped. |
|
|
129 | |
|
|
130 | The worker pool is dynamically resized, according to (perceived :) |
|
|
131 | load. The minimum size is given by the C<idle> parameter and the maximum |
|
|
132 | size is given by the C<max> parameter. A new worker is started every |
|
|
133 | C<start> seconds at most, and an idle worker is stopped at most every |
|
|
134 | C<stop> second. |
|
|
135 | |
|
|
136 | You can specify the amount of jobs sent to a worker concurrently using the |
|
|
137 | C<load> parameter. |
109 | |
138 | |
110 | =over 4 |
139 | =over 4 |
111 | |
140 | |
112 | =item idle => $count (default: 0) |
141 | =item idle => $count (default: 0) |
113 | |
142 | |
114 | The minimum amount of idle processes in the pool - when there are fewer |
143 | The minimum amount of idle processes in the pool - when there are fewer |
115 | than this many idle workers, C<AnyEvent::Fork::Pool> will try to start new |
144 | than this many idle workers, C<AnyEvent::Fork::Pool> will try to start new |
116 | ones, subject to C<max> and C<start>. |
145 | ones, subject to the limits set by C<max> and C<start>. |
117 | |
146 | |
118 | This is also the initial/minimum amount of workers in the pool. The |
147 | This is also the initial amount of workers in the pool. The default of |
119 | default of zero means that the pool starts empty and can shrink back to |
148 | zero means that the pool starts empty and can shrink back to zero workers |
120 | zero workers over time. |
149 | over time. |
121 | |
150 | |
122 | =item max => $count (default: 4) |
151 | =item max => $count (default: 4) |
123 | |
152 | |
124 | The maximum number of processes in the pool, in addition to the template |
153 | The maximum number of processes in the pool, in addition to the template |
125 | process. C<AnyEvent::Fork::Pool> will never create more than this number |
154 | process. C<AnyEvent::Fork::Pool> will never have more than this number of |
126 | of worker processes, although there can be more temporarily when a worker |
155 | worker processes, although there can be more temporarily when a worker is |
127 | is shut down and hasn't exited yet. |
156 | shut down and hasn't exited yet. |
128 | |
157 | |
129 | =item load => $count (default: 2) |
158 | =item load => $count (default: 2) |
130 | |
159 | |
131 | The maximum number of concurrent jobs sent to a single worker |
160 | The maximum number of concurrent jobs sent to a single worker process. |
132 | process. Worker processes that handle this number of jobs already are |
|
|
133 | called "busy". |
|
|
134 | |
161 | |
135 | Jobs that cannot be sent to a worker immediately (because all workers are |
162 | Jobs that cannot be sent to a worker immediately (because all workers are |
136 | busy) will be queued until a worker is available. |
163 | busy) will be queued until a worker is available. |
137 | |
164 | |
|
|
165 | Setting this low improves latency. For example, at C<1>, every job that |
|
|
166 | is sent to a worker is sent to a completely idle worker that doesn't run |
|
|
167 | any other jobs. The downside is that throughput is reduced - a worker that |
|
|
168 | finishes a job needs to wait for a new job from the parent. |
|
|
169 | |
|
|
170 | The default of C<2> is usually a good compromise. |
|
|
171 | |
138 | =item start => $seconds (default: 0.1) |
172 | =item start => $seconds (default: 0.1) |
139 | |
173 | |
140 | When a job is queued and all workers are busy, a timer is started. If the |
174 | When there are fewer than C<idle> workers (or all workers are completely |
141 | timer elapses and there are still jobs that cannot be queued to a worker, |
175 | busy), then a timer is started. If the timer elapses and there are still |
142 | a new worker is started. |
176 | jobs that cannot be queued to a worker, a new worker is started. |
143 | |
177 | |
144 | This configurs the time that all workers must be busy before a new worker |
178 | This sets the minimum time that all workers must be busy before a new |
145 | is started. Or, put differently, the minimum delay betwene starting new |
179 | worker is started. Or, put differently, the minimum delay between starting |
146 | workers. |
180 | new workers. |
147 | |
181 | |
148 | The delay is zero by default, which means new workers will be started |
182 | The delay is small by default, which means new workers will be started |
149 | without delay. |
183 | relatively quickly. A delay of C<0> is possible, and ensures that the pool |
|
|
184 | will grow as quickly as possible under load. |
150 | |
185 | |
|
|
186 | Non-zero values are useful to avoid "exploding" a pool because a lot of |
|
|
187 | jobs are queued in an instant. |
|
|
188 | |
|
|
189 | Higher values are often useful to improve efficiency at the cost of |
|
|
190 | latency - when fewer processes can do the job over time, starting more and |
|
|
191 | more is not necessarily going to help. |
|
|
192 | |
151 | =item stop => $seconds (default: 1) |
193 | =item stop => $seconds (default: 10) |
152 | |
194 | |
153 | When a worker has no jobs to execute it becomes idle. An idle worker that |
195 | When a worker has no jobs to execute it becomes idle. An idle worker that |
154 | hasn't executed a job within this amount of time will be stopped, unless |
196 | hasn't executed a job within this amount of time will be stopped, unless |
155 | the other parameters say otherwise. |
197 | the other parameters say otherwise. |
156 | |
198 | |
|
|
199 | Setting this to a very high value means that workers stay around longer, |
|
|
200 | even when they have nothing to do, which can be good as they don't have to |
|
|
201 | be started on the netx load spike again. |
|
|
202 | |
|
|
203 | Setting this to a lower value can be useful to avoid memory or simply |
|
|
204 | process table wastage. |
|
|
205 | |
|
|
206 | Usually, setting this to a time longer than the time between load spikes |
|
|
207 | is best - if you expect a lot of requests every minute and little work |
|
|
208 | in between, setting this to longer than a minute avoids having to stop |
|
|
209 | and start workers. On the other hand, you have to ask yourself if letting |
|
|
210 | workers run idle is a good use of your resources. Try to find a good |
|
|
211 | balance between resource usage of your workers and the time to start new |
|
|
212 | workers - the processes created by L<AnyEvent::Fork> itself is fats at |
|
|
213 | creating workers while not using much memory for them, so most of the |
|
|
214 | overhead is likely from your own code. |
|
|
215 | |
157 | =item on_destroy => $callback->() (default: none) |
216 | =item on_destroy => $callback->() (default: none) |
158 | |
217 | |
159 | When a pool object goes out of scope, it will still handle all outstanding |
218 | When a pool object goes out of scope, the outstanding requests are still |
160 | jobs. After that, it will destroy all workers (and also the template |
219 | handled till completion. Only after handling all jobs will the workers |
161 | process if it isn't referenced otherwise). |
220 | be destroyed (and also the template process if it isn't referenced |
|
|
221 | otherwise). |
|
|
222 | |
|
|
223 | To find out when a pool I<really> has finished its work, you can set this |
|
|
224 | callback, which will be called when the pool has been destroyed. |
162 | |
225 | |
163 | =back |
226 | =back |
164 | |
227 | |
165 | =item Template Process |
228 | =item AnyEvent::Fork::RPC Parameters |
166 | |
229 | |
167 | The worker processes are all forked from a single template |
230 | These parameters are all passed more or less directly to |
168 | process. Ideally, all modules and all cdoe used by the worker, as well as |
231 | L<AnyEvent::Fork::RPC>. They are only briefly mentioned here, for |
169 | any shared data structures should be loaded into the template process, to |
232 | their full documentation please refer to the L<AnyEvent::Fork::RPC> |
170 | take advantage of data sharing via fork. |
233 | documentation. Also, the default values mentioned here are only documented |
171 | |
234 | as a best effort - the L<AnyEvent::Fork::RPC> documentation is binding. |
172 | You can create your own template process by creating a L<AnyEvent::Fork> |
|
|
173 | object yourself and passing it as the C<template> parameter, but |
|
|
174 | C<AnyEvent::Fork::Pool> can create one for you, including some standard |
|
|
175 | options. |
|
|
176 | |
235 | |
177 | =over 4 |
236 | =over 4 |
178 | |
237 | |
179 | =item template => $fork (default: C<< AnyEvent::Fork->new >>) |
|
|
180 | |
|
|
181 | The template process to use, if you want to create your own. |
|
|
182 | |
|
|
183 | =item require => \@modules (default: C<[]>) |
|
|
184 | |
|
|
185 | The modules in this list will be laoded into the template process. |
|
|
186 | |
|
|
187 | =item eval => "# perl code to execute in template" (default: none) |
|
|
188 | |
|
|
189 | This is a perl string that is evaluated after creating the template |
|
|
190 | process and after requiring the modules. It can do whatever it wants to |
|
|
191 | configure the process, but it must not do anything that would keep a later |
|
|
192 | fork from working (so must not create event handlers or (real) threads for |
|
|
193 | example). |
|
|
194 | |
|
|
195 | =back |
|
|
196 | |
|
|
197 | =item AnyEvent::Fork::RPC Parameters |
|
|
198 | |
|
|
199 | These parameters are all passed directly to L<AnyEvent::Fork::RPC>. They |
|
|
200 | are only briefly mentioned here, for their full documentation |
|
|
201 | please refer to the L<AnyEvent::Fork::RPC> documentation. Also, the |
|
|
202 | default values mentioned here are only documented as a best effort - |
|
|
203 | L<AnyEvent::Fork::RPC> documentation is binding. |
|
|
204 | |
|
|
205 | =over 4 |
|
|
206 | |
|
|
207 | =item async => $boolean (default: 0) |
238 | =item async => $boolean (default: 0) |
208 | |
239 | |
209 | Whether to sue the synchronous or asynchronous RPC backend. |
240 | Whether to use the synchronous or asynchronous RPC backend. |
210 | |
241 | |
211 | =item on_error => $callback->($message) (default: die with message) |
242 | =item on_error => $callback->($message) (default: die with message) |
212 | |
243 | |
213 | The callback to call on any (fatal) errors. |
244 | The callback to call on any (fatal) errors. |
214 | |
245 | |
… | |
… | |
235 | |
266 | |
236 | my $max = $arg{max} || 4; |
267 | my $max = $arg{max} || 4; |
237 | my $idle = $arg{idle} || 0, |
268 | my $idle = $arg{idle} || 0, |
238 | my $load = $arg{load} || 2, |
269 | my $load = $arg{load} || 2, |
239 | my $start = $arg{start} || 0.1, |
270 | my $start = $arg{start} || 0.1, |
240 | my $stop = $arg{stop} || 1, |
271 | my $stop = $arg{stop} || 10, |
241 | my $on_event = $arg{on_event} || sub { }, |
272 | my $on_event = $arg{on_event} || sub { }, |
242 | my $on_destroy = $arg{on_destroy}; |
273 | my $on_destroy = $arg{on_destroy}; |
243 | |
274 | |
244 | my @rpc = ( |
275 | my @rpc = ( |
245 | async => $arg{async}, |
276 | async => $arg{async}, |
246 | init => $arg{init}, |
277 | init => $arg{init}, |
247 | serialiser => $arg{serialiser}, |
278 | serialiser => delete $arg{serialiser}, |
248 | on_error => $arg{on_error}, |
279 | on_error => $arg{on_error}, |
249 | ); |
280 | ); |
250 | |
281 | |
251 | my (@pool, @queue, $nidle, $start_w, $stop_w, $shutdown); |
282 | my (@pool, @queue, $nidle, $start_w, $stop_w, $shutdown); |
252 | my ($start, $stop, $want_start, $want_stop, $scheduler); |
283 | my ($start_worker, $stop_worker, $want_start, $want_stop, $scheduler); |
253 | |
284 | |
254 | my $destroy_guard = Guard::guard { |
285 | my $destroy_guard = Guard::guard { |
255 | $on_destroy->() |
286 | $on_destroy->() |
256 | if $on_destroy; |
287 | if $on_destroy; |
257 | }; |
288 | }; |
258 | |
289 | |
259 | $template |
290 | $template |
260 | ->require ("AnyEvent::Fork::RPC::" . ($arg{async} ? "Async" : "Sync")) |
291 | ->require ("AnyEvent::Fork::RPC::" . ($arg{async} ? "Async" : "Sync")) |
261 | ->eval (' |
292 | ->eval (' |
262 | my ($magic0, $magic1) = @_; |
293 | my ($magic0, $magic1) = @_; |
263 | sub AnyEvent::Fork::Pool::quit() { |
294 | sub AnyEvent::Fork::Pool::retire() { |
264 | AnyEvent::Fork::RPC::on_event $magic0, "quit", $magic1; |
295 | AnyEvent::Fork::RPC::event $magic0, "quit", $magic1; |
265 | } |
296 | } |
266 | ', $magic0, $magic1) |
297 | ', $magic0, $magic1) |
267 | ->eval (delete $arg{eval}); |
298 | ; |
268 | |
299 | |
269 | $start = sub { |
300 | $start_worker = sub { |
270 | my $proc = [0, 0, undef]; # load, index, rpc |
301 | my $proc = [0, 0, undef]; # load, index, rpc |
271 | |
|
|
272 | warn "start a worker\n";#d# |
|
|
273 | |
302 | |
274 | $proc->[2] = $template |
303 | $proc->[2] = $template |
275 | ->fork |
304 | ->fork |
276 | ->AnyEvent::Fork::RPC::run ($function, |
305 | ->AnyEvent::Fork::RPC::run ($function, |
277 | @rpc, |
306 | @rpc, |
278 | on_event => sub { |
307 | on_event => sub { |
279 | if (@_ == 3 && $_[0] eq $magic0 && $_[2] eq $magic1) { |
308 | if (@_ == 3 && $_[0] eq $magic0 && $_[2] eq $magic1) { |
280 | $destroy_guard if 0; # keep it alive |
309 | $destroy_guard if 0; # keep it alive |
281 | |
310 | |
282 | $_[1] eq "quit" and $stop->($proc); |
311 | $_[1] eq "quit" and $stop_worker->($proc); |
283 | return; |
312 | return; |
284 | } |
313 | } |
285 | |
314 | |
286 | &$on_event; |
315 | &$on_event; |
287 | }, |
316 | }, |
288 | ) |
317 | ) |
289 | ; |
318 | ; |
290 | |
319 | |
291 | ++$nidle; |
320 | ++$nidle; |
292 | Array::Heap::push_heap @pool, $proc; |
321 | Array::Heap::push_heap_idx @pool, $proc; |
293 | |
322 | |
294 | Scalar::Util::weaken $proc; |
323 | Scalar::Util::weaken $proc; |
295 | }; |
324 | }; |
296 | |
325 | |
297 | $stop = sub { |
326 | $stop_worker = sub { |
298 | my $proc = shift; |
327 | my $proc = shift; |
299 | |
328 | |
300 | $proc->[0] |
329 | $proc->[0] |
301 | or --$nidle; |
330 | or --$nidle; |
302 | |
331 | |
303 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
332 | Array::Heap::splice_heap_idx @pool, $proc->[1] |
304 | if defined $proc->[1]; |
333 | if defined $proc->[1]; |
|
|
334 | |
|
|
335 | @$proc = 0; # tell others to leave it be |
305 | }; |
336 | }; |
306 | |
337 | |
307 | $want_start = sub { |
338 | $want_start = sub { |
308 | undef $stop_w; |
339 | undef $stop_w; |
309 | |
340 | |
310 | $start_w ||= AE::timer $start, 0, sub { |
341 | $start_w ||= AE::timer $start, $start, sub { |
311 | undef $start_w; |
342 | if (($nidle < $idle || @queue) && @pool < $max) { |
312 | |
|
|
313 | if (@queue) { |
|
|
314 | $start->(); |
343 | $start_worker->(); |
315 | $scheduler->(); |
344 | $scheduler->(); |
|
|
345 | } else { |
|
|
346 | undef $start_w; |
316 | } |
347 | } |
317 | }; |
348 | }; |
318 | }; |
349 | }; |
319 | |
350 | |
320 | $want_stop = sub { |
351 | $want_stop = sub { |
321 | $stop_w ||= AE::timer $stop, 0, sub { |
352 | $stop_w ||= AE::timer $stop, $stop, sub { |
322 | undef $stop_w; |
|
|
323 | |
|
|
324 | $stop->($pool[0]) |
353 | $stop_worker->($pool[0]) |
325 | if $nidle; |
354 | if $nidle; |
|
|
355 | |
|
|
356 | undef $stop_w |
|
|
357 | if $nidle <= $idle; |
326 | }; |
358 | }; |
327 | }; |
359 | }; |
328 | |
360 | |
329 | $scheduler = sub { |
361 | $scheduler = sub { |
330 | if (@queue) { |
362 | if (@queue) { |
331 | while (@queue) { |
363 | while (@queue) { |
|
|
364 | @pool or $start_worker->(); |
|
|
365 | |
332 | my $proc = $pool[0]; |
366 | my $proc = $pool[0]; |
333 | |
367 | |
334 | if ($proc->[0] < $load) { |
368 | if ($proc->[0] < $load) { |
335 | warn "free $proc $proc->[0]\n";#d# |
|
|
336 | # found free worker |
369 | # found free worker, increase load |
337 | $proc->[0]++ |
370 | unless ($proc->[0]++) { |
|
|
371 | # worker became busy |
338 | or --$nidle >= $idle |
372 | --$nidle |
|
|
373 | or undef $stop_w; |
|
|
374 | |
339 | or $want_start->(); |
375 | $want_start->() |
|
|
376 | if $nidle < $idle && @pool < $max; |
|
|
377 | } |
340 | |
378 | |
341 | Array::Heap::adjust_heap @pool, 0; |
379 | Array::Heap::adjust_heap_idx @pool, 0; |
342 | |
380 | |
343 | my $job = shift @queue; |
381 | my $job = shift @queue; |
344 | my $ocb = pop @$job; |
382 | my $ocb = pop @$job; |
345 | |
383 | |
346 | $proc->[2]->(@$job, sub { |
384 | $proc->[2]->(@$job, sub { |
347 | # reduce queue counter |
385 | # reduce load |
348 | --$pool[$_][0] |
386 | --$proc->[0] # worker still busy? |
349 | or ++$nidle > $idle |
387 | or ++$nidle > $idle # not too many idle processes? |
350 | or $want_stop->(); |
388 | or $want_stop->(); |
351 | |
389 | |
352 | Array::Heap::adjust_heap @pool, $_; |
390 | Array::Heap::adjust_heap_idx @pool, $proc->[1] |
|
|
391 | if defined $proc->[1]; |
|
|
392 | |
|
|
393 | &$ocb; |
353 | |
394 | |
354 | $scheduler->(); |
395 | $scheduler->(); |
355 | |
|
|
356 | &$ocb; |
|
|
357 | }); |
396 | }); |
358 | } else { |
397 | } else { |
359 | warn "busy $proc->[0]\n";#d# |
|
|
360 | # all busy, delay |
|
|
361 | |
|
|
362 | $want_start->(); |
398 | $want_start->() |
|
|
399 | unless @pool >= $max; |
|
|
400 | |
363 | last; |
401 | last; |
364 | } |
402 | } |
365 | } |
403 | } |
366 | } elsif ($shutdown) { |
404 | } elsif ($shutdown) { |
367 | @pool = (); |
405 | @pool = (); |
368 | undef $start_w; |
406 | undef $start_w; |
369 | undef $start; # frees $destroy_guard reference |
407 | undef $start_worker; # frees $destroy_guard reference |
370 | |
408 | |
371 | $stop->($pool[0]) |
409 | $stop_worker->($pool[0]) |
372 | while $nidle; |
410 | while $nidle; |
373 | } |
411 | } |
374 | }; |
412 | }; |
375 | |
413 | |
376 | my $shutdown_guard = Guard::guard { |
414 | my $shutdown_guard = Guard::guard { |
377 | $shutdown = 1; |
415 | $shutdown = 1; |
378 | $scheduler->(); |
416 | $scheduler->(); |
379 | }; |
417 | }; |
380 | |
418 | |
381 | $start->() |
419 | $start_worker->() |
382 | while @pool < $idle; |
420 | while @pool < $idle; |
383 | |
421 | |
384 | sub { |
422 | sub { |
385 | $shutdown_guard if 0; # keep it alive |
423 | $shutdown_guard if 0; # keep it alive |
386 | |
424 | |
387 | $start->() |
425 | $start_worker->() |
388 | unless @pool; |
426 | unless @pool; |
389 | |
427 | |
390 | push @queue, [@_]; |
428 | push @queue, [@_]; |
391 | $scheduler->(); |
429 | $scheduler->(); |
392 | } |
430 | } |
393 | } |
431 | } |
394 | |
432 | |
395 | =item $pool->call (..., $cb->(...)) |
433 | =item $pool->(..., $cb->(...)) |
396 | |
434 | |
397 | Call the RPC function of a worker with the given arguments, and when the |
435 | Call the RPC function of a worker with the given arguments, and when the |
398 | worker is done, call the C<$cb> with the results, like just calling the |
436 | worker is done, call the C<$cb> with the results, just like calling the |
399 | L<AnyEvent::Fork::RPC> object directly. |
437 | RPC object durectly - see the L<AnyEvent::Fork::RPC> documentation for |
|
|
438 | details on the RPC API. |
400 | |
439 | |
401 | If there is no free worker, the call will be queued. |
440 | If there is no free worker, the call will be queued until a worker becomes |
|
|
441 | available. |
402 | |
442 | |
403 | Note that there can be considerable time between calling this method and |
443 | Note that there can be considerable time between calling this method and |
404 | the call actually being executed. During this time, the parameters passed |
444 | the call actually being executed. During this time, the parameters passed |
405 | to this function are effectively read-only - modifying them after the call |
445 | to this function are effectively read-only - modifying them after the call |
406 | and before the callback is invoked causes undefined behaviour. |
446 | and before the callback is invoked causes undefined behaviour. |
407 | |
447 | |
408 | =cut |
448 | =cut |
409 | |
449 | |
|
|
450 | =item $cpus = AnyEvent::Fork::Pool::ncpu [$default_cpus] |
|
|
451 | |
|
|
452 | =item ($cpus, $eus) = AnyEvent::Fork::Pool::ncpu [$default_cpus] |
|
|
453 | |
|
|
454 | Tries to detect the number of CPUs (C<$cpus> often called cpu cores |
|
|
455 | nowadays) and execution units (C<$eus>) which include e.g. extra |
|
|
456 | hyperthreaded units). When C<$cpus> cannot be determined reliably, |
|
|
457 | C<$default_cpus> is returned for both values, or C<1> if it is missing. |
|
|
458 | |
|
|
459 | For normal CPU bound uses, it is wise to have as many worker processes |
|
|
460 | as CPUs in the system (C<$cpus>), if nothing else uses the CPU. Using |
|
|
461 | hyperthreading is usually detrimental to performance, but in those rare |
|
|
462 | cases where that really helps it might be beneficial to use more workers |
|
|
463 | (C<$eus>). |
|
|
464 | |
|
|
465 | Currently, F</proc/cpuinfo> is parsed on GNU/Linux systems for both |
|
|
466 | C<$cpus> and C<$eu>, and on {Free,Net,Open}BSD, F<sysctl -n hw.ncpu> is |
|
|
467 | used for C<$cpus>. |
|
|
468 | |
|
|
469 | Example: create a worker pool with as many workers as cpu cores, or C<2>, |
|
|
470 | if the actual number could not be determined. |
|
|
471 | |
|
|
472 | $fork->AnyEvent::Fork::Pool::run ("myworker::function", |
|
|
473 | max => (scalar AnyEvent::Fork::Pool::ncpu 2), |
|
|
474 | ); |
|
|
475 | |
|
|
476 | =cut |
|
|
477 | |
|
|
478 | BEGIN { |
|
|
479 | if ($^O eq "linux") { |
|
|
480 | *ncpu = sub(;$) { |
|
|
481 | my ($cpus, $eus); |
|
|
482 | |
|
|
483 | if (open my $fh, "<", "/proc/cpuinfo") { |
|
|
484 | my %id; |
|
|
485 | |
|
|
486 | while (<$fh>) { |
|
|
487 | if (/^core id\s*:\s*(\d+)/) { |
|
|
488 | ++$eus; |
|
|
489 | undef $id{$1}; |
|
|
490 | } |
|
|
491 | } |
|
|
492 | |
|
|
493 | $cpus = scalar keys %id; |
|
|
494 | } else { |
|
|
495 | $cpus = $eus = @_ ? shift : 1; |
|
|
496 | } |
|
|
497 | wantarray ? ($cpus, $eus) : $cpus |
|
|
498 | }; |
|
|
499 | } elsif ($^O eq "freebsd" || $^O eq "netbsd" || $^O eq "openbsd") { |
|
|
500 | *ncpu = sub(;$) { |
|
|
501 | my $cpus = qx<sysctl -n hw.ncpu> * 1 |
|
|
502 | || (@_ ? shift : 1); |
|
|
503 | wantarray ? ($cpus, $cpus) : $cpus |
|
|
504 | }; |
|
|
505 | } else { |
|
|
506 | *ncpu = sub(;$) { |
|
|
507 | my $cpus = @_ ? shift : 1; |
|
|
508 | wantarray ? ($cpus, $cpus) : $cpus |
|
|
509 | }; |
|
|
510 | } |
|
|
511 | } |
|
|
512 | |
410 | =back |
513 | =back |
|
|
514 | |
|
|
515 | =head1 CHILD USAGE |
|
|
516 | |
|
|
517 | In addition to the L<AnyEvent::Fork::RPC> API, this module implements one |
|
|
518 | more child-side function: |
|
|
519 | |
|
|
520 | =over 4 |
|
|
521 | |
|
|
522 | =item AnyEvent::Fork::Pool::retire () |
|
|
523 | |
|
|
524 | This function sends an event to the parent process to request retirement: |
|
|
525 | the worker is removed from the pool and no new jobs will be sent to it, |
|
|
526 | but it has to handle the jobs that are already queued. |
|
|
527 | |
|
|
528 | The parentheses are part of the syntax: the function usually isn't defined |
|
|
529 | when you compile your code (because that happens I<before> handing the |
|
|
530 | template process over to C<AnyEvent::Fork::Pool::run>, so you need the |
|
|
531 | empty parentheses to tell Perl that the function is indeed a function. |
|
|
532 | |
|
|
533 | Retiring a worker can be useful to gracefully shut it down when the worker |
|
|
534 | deems this useful. For example, after executing a job, one could check |
|
|
535 | the process size or the number of jobs handled so far, and if either is |
|
|
536 | too high, the worker could ask to get retired, to avoid memory leaks to |
|
|
537 | accumulate. |
|
|
538 | |
|
|
539 | Example: retire a worker after it has handled roughly 100 requests. |
|
|
540 | |
|
|
541 | my $count = 0; |
|
|
542 | |
|
|
543 | sub my::worker { |
|
|
544 | |
|
|
545 | ++$count == 100 |
|
|
546 | and AnyEvent::Fork::Pool::retire (); |
|
|
547 | |
|
|
548 | ... normal code goes here |
|
|
549 | } |
|
|
550 | |
|
|
551 | =back |
|
|
552 | |
|
|
553 | =head1 POOL PARAMETERS RECIPES |
|
|
554 | |
|
|
555 | This section describes some recipes for pool paramaters. These are mostly |
|
|
556 | meant for the synchronous RPC backend, as the asynchronous RPC backend |
|
|
557 | changes the rules considerably, making workers themselves responsible for |
|
|
558 | their scheduling. |
|
|
559 | |
|
|
560 | =over 4 |
|
|
561 | |
|
|
562 | =item low latency - set load = 1 |
|
|
563 | |
|
|
564 | If you need a deterministic low latency, you should set the C<load> |
|
|
565 | parameter to C<1>. This ensures that never more than one job is sent to |
|
|
566 | each worker. This avoids having to wait for a previous job to finish. |
|
|
567 | |
|
|
568 | This makes most sense with the synchronous (default) backend, as the |
|
|
569 | asynchronous backend can handle multiple requests concurrently. |
|
|
570 | |
|
|
571 | =item lowest latency - set load = 1 and idle = max |
|
|
572 | |
|
|
573 | To achieve the lowest latency, you additionally should disable any dynamic |
|
|
574 | resizing of the pool by setting C<idle> to the same value as C<max>. |
|
|
575 | |
|
|
576 | =item high throughput, cpu bound jobs - set load >= 2, max = #cpus |
|
|
577 | |
|
|
578 | To get high throughput with cpu-bound jobs, you should set the maximum |
|
|
579 | pool size to the number of cpus in your system, and C<load> to at least |
|
|
580 | C<2>, to make sure there can be another job waiting for the worker when it |
|
|
581 | has finished one. |
|
|
582 | |
|
|
583 | The value of C<2> for C<load> is the minimum value that I<can> achieve |
|
|
584 | 100% throughput, but if your parent process itself is sometimes busy, you |
|
|
585 | might need higher values. Also there is a limit on the amount of data that |
|
|
586 | can be "in flight" to the worker, so if you send big blobs of data to your |
|
|
587 | worker, C<load> might have much less of an effect. |
|
|
588 | |
|
|
589 | =item high throughput, I/O bound jobs - set load >= 2, max = 1, or very high |
|
|
590 | |
|
|
591 | When your jobs are I/O bound, using more workers usually boils down to |
|
|
592 | higher throughput, depending very much on your actual workload - sometimes |
|
|
593 | having only one worker is best, for example, when you read or write big |
|
|
594 | files at maixmum speed, as a second worker will increase seek times. |
|
|
595 | |
|
|
596 | =back |
|
|
597 | |
|
|
598 | =head1 EXCEPTIONS |
|
|
599 | |
|
|
600 | The same "policy" as with L<AnyEvent::Fork::RPC> applies - exceptins will |
|
|
601 | not be caught, and exceptions in both worker and in callbacks causes |
|
|
602 | undesirable or undefined behaviour. |
411 | |
603 | |
412 | =head1 SEE ALSO |
604 | =head1 SEE ALSO |
413 | |
605 | |
414 | L<AnyEvent::Fork>, to create the processes in the first place. |
606 | L<AnyEvent::Fork>, to create the processes in the first place. |
415 | |
607 | |