I
it_says_BALLS_on_your forehead
I am getting erratic behavior with this script...
#!/apps/webstats/bin/perl
use File::Copy;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(10);
$pm->run_on_start(
sub { my ($pid,$ident)=@_;
# print "** $ident started, pid: $pid\n";
}
);
# NOTE: this MUST be assigned BEFORE the $pm->run_on_finish
my @tokens = "log1" .. "log5";
# test with a 'late' file.
my @data = "file01" .. "file10";
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident) = @_;
my ($outlog, $missingfile) = split /\|/, $_[2];
push( @tokens, $outlog );
# only push if the file is missing...HOW TO KNOW
IF IT'S MISSING?? or do a next if (-e $missing
# ...but the next messes up the ForkManager b/c
child process does not go to $pm->finish and so
# tries to spawn an extra process...breaks the
limit
# could put $pm->finish in continue block, search
in ...perl.misc Google Groups: fork ftp
unless (-e "data/$missingfile") {
push( @data, $missingfile );
}
# print "ID: $ident (pid: $pid) had exit code:
$exit_code.\n";
}
);
my $counter = 0;
for (@data) {
$counter++;
if ($counter > 20 ) {
print "\n*******counter was above 20:
$counter**********************\n\n";
last; # maybe this doesn't ->finish...
}
my $outfile = shift(@tokens);
$pm->start("$outfile|$_") and next;
print "$counter: ";
print "reading data/$_: writing to log -$outfile-\n";
my $func_ref = hello($_);
$func_ref->("Simon");
$pm->finish;
}
$pm->wait_all_children;
#--- subs ---
sub hello {
my ($type) = @_;
if ($type eq "file01") {
print "type: $type\n";
return \&func1;
}
else {
print "type: $type\n";
return \&func2;
}
return 0;
}
sub func1 {
my ($noun) = @_;
print "* using func1. you stink, $noun\n";
}
sub func2 {
my ($noun) = @_;
print "* * using func2. yay!!! it's $noun!!!\n";
}
#-----
RESULT 1:
[mymachine] ~/simon/1-perl > tryFork.pl
1: reading data/file01: writing to log -log1-
type: file01
* using func1. you stink, Simon
2: reading data/file02: writing to log -log2-
type: file02
* * using func2. yay!!! it's Simon!!!
3: reading data/file03: writing to log -log3-
type: file03
* * using func2. yay!!! it's Simon!!!
4: reading data/file04: writing to log -log4-
type: file04
* * using func2. yay!!! it's Simon!!!
5: reading data/file05: writing to log -log5-
type: file05
* * using func2. yay!!! it's Simon!!!
6: reading data/file06: writing to log -log1-
type: file06
* * using func2. yay!!! it's Simon!!!
7: reading data/file07: writing to log -log2-
type: file07
* * using func2. yay!!! it's Simon!!!
8: reading data/file08: writing to log -log3-
type: file08
* * using func2. yay!!! it's Simon!!!
9: reading data/file09: writing to log -log4-
type: file09
* * using func2. yay!!! it's Simon!!!
10: reading data/file10: writing to log -log5-
type: file10
* * using func2. yay!!! it's Simon!!!
11: reading data/file07: writing to log -log1-
type: file07
* * using func2. yay!!! it's Simon!!!
12: reading data/file08: writing to log -log2-
type: file08
* * using func2. yay!!! it's Simon!!!
13: reading data/file09: writing to log -log3-
type: file09
* * using func2. yay!!! it's Simon!!!
14: reading data/file10: writing to log -log4-
type: file10
* * using func2. yay!!! it's Simon!!!
15: reading data/file07: writing to log -log5-
type: file07
* * using func2. yay!!! it's Simon!!!
16: reading data/file08: writing to log -log1-
type: file08
* * using func2. yay!!! it's Simon!!!
17: reading data/file09: writing to log -log2-
type: file09
* * using func2. yay!!! it's Simon!!!
18: reading data/file10: writing to log -log3-
type: file10
* * using func2. yay!!! it's Simon!!!
19: reading data/file07: writing to log -log4-
type: file07
* * using func2. yay!!! it's Simon!!!
*******counter was above 20: 21**********************
20: reading data/file08: writing to log -log5-
type: file08
* * using func2. yay!!! it's Simon!!!
RESULT2:
[mymachine] ~/simon/1-perl > tryFork.pl
1: reading data/file01: writing to log -log1-
type: file01
* using func1. you stink, Simon
2: reading data/file02: writing to log -log2-
type: file02
* * using func2. yay!!! it's Simon!!!
3: reading data/file03: writing to log -log3-
type: file03
* * using func2. yay!!! it's Simon!!!
4: reading data/file04: writing to log -log4-
type: file04
* * using func2. yay!!! it's Simon!!!
5: reading data/file05: writing to log -log5-
type: file05
* * using func2. yay!!! it's Simon!!!
6: reading data/file06: writing to log -log1-
type: file06
* * using func2. yay!!! it's Simon!!!
7: reading data/file07: writing to log -log2-
type: file07
* * using func2. yay!!! it's Simon!!!
8: reading data/file08: writing to log -log3-
type: file08
* * using func2. yay!!! it's Simon!!!
9: reading data/file09: writing to log -log4-
type: file09
* * using func2. yay!!! it's Simon!!!
10: reading data/file10: writing to log -log5-
type: file10
* * using func2. yay!!! it's Simon!!!
11: reading data/file07: writing to log -log1-
type: file07
* * using func2. yay!!! it's Simon!!!
12: reading data/file08: writing to log -log2-
type: file08
* * using func2. yay!!! it's Simon!!!
13: reading data/file09: writing to log -log3-
type: file09
* * using func2. yay!!! it's Simon!!!
14: reading data/file10: writing to log -log4-
type: file10
* * using func2. yay!!! it's Simon!!!
15: reading data/file07: writing to log -log5-
type: file07
* * using func2. yay!!! it's Simon!!!
16: reading data/file08: writing to log -log1-
type: file08
* * using func2. yay!!! it's Simon!!!
17: reading data/file09: writing to log -log2-
type: file09
* * using func2. yay!!! it's Simon!!!
....sometimes it stops at 10, sometimes at 15...MOST of the time it gets
all the way to 20. does anyone understand this behavior? do i need to
stick a continue block around the $pm->finish? i understand that if i
do this, the parent will also call finish, but this will be a silent
no-op when called by the parent.
#!/apps/webstats/bin/perl
use File::Copy;
use Parallel::ForkManager;
my $pm = Parallel::ForkManager->new(10);
$pm->run_on_start(
sub { my ($pid,$ident)=@_;
# print "** $ident started, pid: $pid\n";
}
);
# NOTE: this MUST be assigned BEFORE the $pm->run_on_finish
my @tokens = "log1" .. "log5";
# test with a 'late' file.
my @data = "file01" .. "file10";
$pm->run_on_finish(
sub {
my ($pid, $exit_code, $ident) = @_;
my ($outlog, $missingfile) = split /\|/, $_[2];
push( @tokens, $outlog );
# only push if the file is missing...HOW TO KNOW
IF IT'S MISSING?? or do a next if (-e $missing
# ...but the next messes up the ForkManager b/c
child process does not go to $pm->finish and so
# tries to spawn an extra process...breaks the
limit
# could put $pm->finish in continue block, search
in ...perl.misc Google Groups: fork ftp
unless (-e "data/$missingfile") {
push( @data, $missingfile );
}
# print "ID: $ident (pid: $pid) had exit code:
$exit_code.\n";
}
);
my $counter = 0;
for (@data) {
$counter++;
if ($counter > 20 ) {
print "\n*******counter was above 20:
$counter**********************\n\n";
last; # maybe this doesn't ->finish...
}
my $outfile = shift(@tokens);
$pm->start("$outfile|$_") and next;
print "$counter: ";
print "reading data/$_: writing to log -$outfile-\n";
my $func_ref = hello($_);
$func_ref->("Simon");
$pm->finish;
}
$pm->wait_all_children;
#--- subs ---
sub hello {
my ($type) = @_;
if ($type eq "file01") {
print "type: $type\n";
return \&func1;
}
else {
print "type: $type\n";
return \&func2;
}
return 0;
}
sub func1 {
my ($noun) = @_;
print "* using func1. you stink, $noun\n";
}
sub func2 {
my ($noun) = @_;
print "* * using func2. yay!!! it's $noun!!!\n";
}
#-----
RESULT 1:
[mymachine] ~/simon/1-perl > tryFork.pl
1: reading data/file01: writing to log -log1-
type: file01
* using func1. you stink, Simon
2: reading data/file02: writing to log -log2-
type: file02
* * using func2. yay!!! it's Simon!!!
3: reading data/file03: writing to log -log3-
type: file03
* * using func2. yay!!! it's Simon!!!
4: reading data/file04: writing to log -log4-
type: file04
* * using func2. yay!!! it's Simon!!!
5: reading data/file05: writing to log -log5-
type: file05
* * using func2. yay!!! it's Simon!!!
6: reading data/file06: writing to log -log1-
type: file06
* * using func2. yay!!! it's Simon!!!
7: reading data/file07: writing to log -log2-
type: file07
* * using func2. yay!!! it's Simon!!!
8: reading data/file08: writing to log -log3-
type: file08
* * using func2. yay!!! it's Simon!!!
9: reading data/file09: writing to log -log4-
type: file09
* * using func2. yay!!! it's Simon!!!
10: reading data/file10: writing to log -log5-
type: file10
* * using func2. yay!!! it's Simon!!!
11: reading data/file07: writing to log -log1-
type: file07
* * using func2. yay!!! it's Simon!!!
12: reading data/file08: writing to log -log2-
type: file08
* * using func2. yay!!! it's Simon!!!
13: reading data/file09: writing to log -log3-
type: file09
* * using func2. yay!!! it's Simon!!!
14: reading data/file10: writing to log -log4-
type: file10
* * using func2. yay!!! it's Simon!!!
15: reading data/file07: writing to log -log5-
type: file07
* * using func2. yay!!! it's Simon!!!
16: reading data/file08: writing to log -log1-
type: file08
* * using func2. yay!!! it's Simon!!!
17: reading data/file09: writing to log -log2-
type: file09
* * using func2. yay!!! it's Simon!!!
18: reading data/file10: writing to log -log3-
type: file10
* * using func2. yay!!! it's Simon!!!
19: reading data/file07: writing to log -log4-
type: file07
* * using func2. yay!!! it's Simon!!!
*******counter was above 20: 21**********************
20: reading data/file08: writing to log -log5-
type: file08
* * using func2. yay!!! it's Simon!!!
RESULT2:
[mymachine] ~/simon/1-perl > tryFork.pl
1: reading data/file01: writing to log -log1-
type: file01
* using func1. you stink, Simon
2: reading data/file02: writing to log -log2-
type: file02
* * using func2. yay!!! it's Simon!!!
3: reading data/file03: writing to log -log3-
type: file03
* * using func2. yay!!! it's Simon!!!
4: reading data/file04: writing to log -log4-
type: file04
* * using func2. yay!!! it's Simon!!!
5: reading data/file05: writing to log -log5-
type: file05
* * using func2. yay!!! it's Simon!!!
6: reading data/file06: writing to log -log1-
type: file06
* * using func2. yay!!! it's Simon!!!
7: reading data/file07: writing to log -log2-
type: file07
* * using func2. yay!!! it's Simon!!!
8: reading data/file08: writing to log -log3-
type: file08
* * using func2. yay!!! it's Simon!!!
9: reading data/file09: writing to log -log4-
type: file09
* * using func2. yay!!! it's Simon!!!
10: reading data/file10: writing to log -log5-
type: file10
* * using func2. yay!!! it's Simon!!!
11: reading data/file07: writing to log -log1-
type: file07
* * using func2. yay!!! it's Simon!!!
12: reading data/file08: writing to log -log2-
type: file08
* * using func2. yay!!! it's Simon!!!
13: reading data/file09: writing to log -log3-
type: file09
* * using func2. yay!!! it's Simon!!!
14: reading data/file10: writing to log -log4-
type: file10
* * using func2. yay!!! it's Simon!!!
15: reading data/file07: writing to log -log5-
type: file07
* * using func2. yay!!! it's Simon!!!
16: reading data/file08: writing to log -log1-
type: file08
* * using func2. yay!!! it's Simon!!!
17: reading data/file09: writing to log -log2-
type: file09
* * using func2. yay!!! it's Simon!!!
....sometimes it stops at 10, sometimes at 15...MOST of the time it gets
all the way to 20. does anyone understand this behavior? do i need to
stick a continue block around the $pm->finish? i understand that if i
do this, the parent will also call finish, but this will be a silent
no-op when called by the parent.