-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForkManager.pm
453 lines (326 loc) · 9.56 KB
/
ForkManager.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package AnyEvent::ForkManager;
use 5.008_001;
use strict;
use warnings;
our $VERSION = '0.04';
use AnyEvent;
use Scalar::Util qw/weaken/;
use POSIX qw/WNOHANG/;
use Time::HiRes ();
use Class::Accessor::Lite 0.04 (
ro => [
qw/max_workers manager_pid/,
],
rw => [
qw/on_start on_finish on_error on_enqueue on_dequeue on_working_max/,
qw/process_queue running_worker process_cb wait_async/,
],
);
sub default_max_workers { 10 }
sub new {
my $class = shift;
my $arg = (@_ == 1) ? +shift : +{ @_ };
$arg->{max_workers} ||= $class->default_max_workers;
bless(+{
%$arg,
manager_pid => $$,
} => $class)->init;
}
sub init {
my $self = shift;
$self->process_queue([]);
$self->running_worker(+{});
$self->process_cb(+{});
return $self;
}
sub is_child { shift->manager_pid != $$ }
sub is_working_max {
my $self = shift;
$self->num_workers >= $self->max_workers;
}
sub num_workers {
my $self = shift;
return scalar keys %{ $self->running_worker };
}
sub num_queues {
my $self = shift;
return scalar @{ $self->process_queue };
}
sub start {
my $self = shift;
my $arg = (@_ == 1) ? +shift : +{ @_ };
die "\$fork_manager->start() should be called within the manager process\n"
if $self->is_child;
if ($self->is_working_max) {## child working max
$self->_run_cb('on_working_max' => @{ $arg->{args} });
$self->enqueue($arg);
return;
}
else {## create child process
my $pid = fork;
if (not(defined $pid)) {
$self->_run_cb('on_error' => @{ $arg->{args} });
return;
}
elsif ($pid) {
# parent
$self->_run_cb('on_start' => $pid, @{ $arg->{args} });
$self->process_cb->{$pid} = $self->_create_callback(@{ $arg->{args} });
$self->running_worker->{$pid} = AnyEvent->child(
pid => $pid,
cb => $self->process_cb->{$pid},
);
# delete worker watcher if already finished child process.
delete $self->running_worker->{$pid} unless exists $self->process_cb->{$pid};
return $pid;
}
else {
# child
$arg->{cb}->($self, @{ $arg->{args} });
$self->finish;
}
}
}
sub _create_callback {
my($self, @args) = @_;
weaken($self);
return sub {
my ($pid, $status) = @_;
delete $self->running_worker->{$pid};
delete $self->process_cb->{$pid};
$self->_run_cb('on_finish' => $pid, $status, @args);
if ($self->num_queues) {
## dequeue
$self->dequeue;
}
};
}
sub finish {
my ($self, $exit_code) = @_;
die "\$fork_manager->finish() shouln't be called within the manager process\n"
unless $self->is_child;
exit($exit_code || 0);
}
sub enqueue {
my($self, $arg) = @_;
$self->_run_cb('on_enqueue' => @{ $arg->{args} });
push @{ $self->process_queue } => $arg;
}
sub dequeue {
my $self = shift;
until ($self->is_working_max) {
last unless @{ $self->process_queue };
# dequeue
if (my $arg = shift @{ $self->process_queue }) {
$self->_run_cb('on_dequeue' => @{ $arg->{args} });
$self->start($arg);
}
}
}
sub signal_all_children {
my ($self, $sig) = @_;
foreach my $pid (sort keys %{ $self->running_worker }) {
kill $sig, $pid;
}
}
sub wait_all_children {
my $self = shift;
my $arg = (@_ == 1) ? +shift : +{ @_ };
my $cb = $arg->{cb};
if ($arg->{blocking}) {
$self->_wait_all_children_with_blocking;
$self->$cb;
}
else {
die 'cannot call.' if $self->wait_async;
my $super = $self->on_finish;
weaken($self);
$self->on_finish(
sub {
$super->(@_);
if ($self->num_workers == 0 and $self->num_queues == 0) {
$self->$cb;
$self->on_finish($super);
$self->wait_async(0);
}
}
);
$self->wait_async(1);
}
}
sub _run_cb {
my $self = shift;
my $name = shift;
my $cb = $self->$name();
if ($cb) {
$self->$cb(@_);
}
}
our $WAIT_INTERVAL = 0.1 * 1000 * 1000;
sub _wait_all_children_with_blocking {
my $self = shift;
until ($self->num_workers == 0 and $self->num_queues == 0) {
my($pid, $status) = _wait_with_status(-1, WNOHANG);
if ($pid and exists $self->running_worker->{$pid}) {
$self->process_cb->{$pid}->($pid, $status);
}
}
continue {
# retry interval
Time::HiRes::usleep( $WAIT_INTERVAL );
}
}
# function
sub _wait_with_status {## blocking
my($waitpid, $option) = @_;
use vmsish 'status';
local $?;
my $pid = waitpid($waitpid, $option);
return ($pid, $?);
}
1;
__END__
=head1 NAME
AnyEvent::ForkManager - A simple parallel processing fork manager with AnyEvent
=head1 VERSION
This document describes AnyEvent::ForkManager version 0.04.
=head1 SYNOPSIS
use AnyEvent;
use AnyEvent::ForkManager;
use List::Util qw/shuffle/;
my $MAX_WORKERS = 10;
my $pm = AnyEvent::ForkManager->new(max_workers => $MAX_WORKERS);
$pm->on_start(sub {
my($pm, $pid, $sec) = @_;
printf "start sleep %2d sec.\n", $sec;
});
$pm->on_finish(sub {
my($pm, $pid, $status, $sec) = @_;
printf "end sleep %2d sec.\n", $sec;
});
my @sleep_time = shuffle(1 .. 20);
foreach my $sec (@sleep_time) {
$pm->start(
cb => sub {
my($pm, $sec) = @_;
sleep $sec;
},
args => [$sec]
);
}
my $cv = AnyEvent->condvar;
# wait with non-blocking
$pm->wait_all_children(
cb => sub {
my($pm) = @_;
print "end task!\n";
$cv->send;
},
);
$cv->recv;
=head1 DESCRIPTION
C<AnyEvent::ForkManager> is much like L<Parallel::ForkManager>,
but supports non-blocking interface with L<AnyEvent>.
L<Parallel::ForkManager> is useful but,
it is difficult to use in conjunction with L<AnyEvent>.
Because L<Parallel::ForkManager>'s some methods are blocking the event loop of the L<AnyEvent>.
You can accomplish the same goals without adversely affecting the L<Parallel::ForkManager> to L<AnyEvent::ForkManager> with L<AnyEvent>.
Because L<AnyEvent::ForkManager>'s methods are non-blocking the event loop of the L<AnyEvent>.
=head1 INTERFACE
=head2 Methods
=head3 C<< new >>
This is constructor.
=over 4
=item max_workers
max parallel forking count. (default: 10)
=item on_start
started child process callback.
=item on_finish
finished child process callback.
=item on_error
fork error callback.
=item on_enqueue
If push to start up child process queue, this callback is called.
=item on_dequeue
If shift from start up child process queue, this callback is called.
=item on_working_max
If request to start up child process and process count equal max process count, this callback is called.
=back
=head4 Example
my $pm = AnyEvent::ForkManager->new(
max_workers => 2, ## default 10
on_finish => sub { ## optional
my($pid, $status, @anyargs) = @_;
## this callback call when finished child process.(like AnyEvent->child)
},
on_error => sub { ## optional
my($pm, @anyargs) = @_;
## this callback call when fork failed.
},
);
=head3 C<< start >>
start child process.
=over 4
=item args
arguments passed to the callback function of the child process.
=item cb
run on child process callback.
=back
=head4 Example
$pm->start(
cb => sub { ## optional
my($pm, $job_id) = @_;
## this callback call in child process.
},
args => [$job_id],## this arguments passed to the callback function
);
=head3 C<< wait_all_children >>
You can call this method to wait for all the processes which have been forked.
This can wait with blocking or wait with non-blocking in event loop of AnyEvent.
B<feature to wait with blocking is ALPHA quality till the version hits v1.0.0. Things might be broken.>
=over 4
=item blocking
If this parameter is true, blocking wait enable. (default: false)
B<feature to wait with blocking is ALPHA quality till the version hits v1.0.0. Things might be broken.>
=item cb
finished all the processes callback.
=back
=head4 Example
$pm->wait_all_children(
cb => sub { ## optional
my($pm) = @_;
## this callback call when finished all child process.
},
);
=head3 C<< signal_all_children >>
Sends signal to all worker processes. Only usable from manager process.
=head3 C<< on_error >>
As a new method's argument.
=head3 C<< on_start >>
As a new method's argument.
=head3 C<< on_finish >>
As a new method's argument.
=head3 C<< on_enqueue >>
As a new method's argument.
=head3 C<< on_dequeue >>
As a new method's argument.
=head3 C<< on_working_max >>
As a new method's argument.
=head1 DEPENDENCIES
Perl 5.8.1 or later.
=head1 BUGS
All complex software has bugs lurking in it, and this module is no
exception. If you find a bug please either email me, or add the bug
to cpan-RT.
=head1 SEE ALSO
L<AnyEvent>
L<AnyEvent::Util>
L<Parallel::ForkManager>
L<Parallel::Prefork>
=head1 AUTHOR
Kenta Sato E<lt>karupa@cpan.orgE<gt>
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2012, Kenta Sato. All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut