Re: optimize log parsing



Hey Xho, I tried this:
----
#!/apps/webstats/bin/perl

use File::Copy;
use Parallel::ForkManager;


my $pm = Parallel::ForkManager->new(5);

$pm->run_on_start(
sub { my ($pid,$ident)=@_;
print "** $ident started, pid: $pid\n";
}
);

my @data = 1 ... shift;
for (@data) {
my $pid = $pm->start and next;
print "$pid: $_\n";
$pm->finish;
}

$pm->wait_all_children;
------------
and got this:
#####
[smro180 123] ~/simon/1-perl > tryFork.pl 10
** started, pid: 16208
0: 1
** started, pid: 16209
0: 2
** started, pid: 16210
0: 3
** started, pid: 16211
0: 4
** started, pid: 16212
0: 5
** started, pid: 16213
0: 6
** started, pid: 16214
0: 7
** started, pid: 16215
0: 8
** started, pid: 16216
0: 9
** started, pid: 16217
0: 10
[smro180 124] ~/simon/1-perl >
####

....I read this:
start [ $process_identifier ]
This method does the fork. It returns the pid of the child process for
the parent, and 0 for the child process. If the $processes parameter
for the constructor is 0 then, assuming you're in the child process,
$pm->start simply returns 0.

An optional $process_identifier can be provided to this method... It is
used by the "run_on_finish" callback (see CALLBACKS) for identifying
the finished process.

and this:
run_on_start $code
You can define a subroutine which is called when a child is started. It
called after the successful startup of a child in the parent process.

The parameters of the $code are the following:

- pid of the process which has been started
- identification of the process (if provided in the "start" method)



....but I don't understand why in my: print "$pid: $_\n";
line, i'm getting 0 as the pid. I know the documentation said i should
get 0 for the child process and the child pid for the parent, but
aren't i calling start on the parent? and in the callback, why are
there more than 5 different process ids?

.



Relevant Pages

  • Re: About sleep() in a child process
    ... int main{ ... Parent: PID is 11058 ... I expected the child process to print five c's (because ...
    (comp.unix.programmer)
  • Re: About sleep() in a child process
    ... int main{ ... Parent: PID is 11058 ... I expected the child process to print five c's (because ...
    (comp.unix.programmer)
  • Re: fork()-ing questions
    ... (somehow, I knew that, but I kept seeing a loop structure for some ... unless ($pid = fork()) { ... print "parent process has spawned child process $ret\n"; ... And the child doesn't return a value to the parent, ...
    (comp.lang.perl.misc)
  • Re: fork and exec PPID question
    ... the child process PID will be unique, but it's PPID will be the PID of ... the parent process PID will be unique, and it's PPID will be the PID of ... an immediate execperformed by the child process. ...
    (comp.unix.questions)
  • Re: Perl killed after child spawn
    ... I'm finding that if my C++ child process bombs out with signal ABRT ... (this is on Solaris btw, with Perl v5.8.4), then Perl dies. ... if ($pid) { ... As long as Americans honor Lincoln so highly, there is probably no hope for ...
    (comp.lang.perl.misc)