Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

process synchronize.... Please help me out

by sandy1028 (Sexton)
on Apr 21, 2009 at 07:46 UTC ( #758896=perlquestion: print w/replies, xml ) Need Help??

sandy1028 has asked for the wisdom of the Perl Monks concerning the following question:

my $log = IO::File->new(">$log_file_name"); $log->print("date()."--\n\n\n"); sub process { my ($sub_dir, $file, $config, $log) = @_; $log->print("-- Reading $file file\n"); }
my $pm = new Parallel::ForkManager($tc+1); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; $tmp +Files[$ident] = undef; } ); foreach my $i (0..$#tmp) { # Forks and returns the pid for the child: my $pid = $pm->start($i) and next; $SIG{INT} = 'DEFAULT'; my $filename = $tmp[$i]->filename(); my $file = IO::File->new("<$filename") or die "Can't open $filen +ame\n"; while((my $line) = $file->getline()) { last unless defined($line); chomp $line; my ($dir, $file) = split(/\t/, $line); # my $process = shift; is created above # Calling this from another file $process->($dir, $file, $config, $log); } $pm->finish; # Terminates the child process }
Here 5 process is created and subroutine is called from another file which opens a log file write.
$process->($dir, $file, $config, $log);
When I print $pid always returns as 0. How can I synchronize all the forked process to write to same log file or create a different log file for each process.
or
How can I create a threading process as the replacement for forking process

Replies are listed 'Best First'.
Re: process synchronize.... Please help me out
by Corion (Patriarch) on Apr 21, 2009 at 12:04 UTC

    Where do you print $pid? You declare two variables $pid in two places:

    $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; $tmpFile +s[$ident] = undef; } );

    Here, $pid is likely the PID of the child that just terminated.

    my $pid = $pm->start($i) and next;

    Here, $pid will always be 0 after the next statement. Maybe you want something like this:

    my $pid = $pm->start($i); if ($pid) { print "Launched child $pid\n"; next; };
      How to lock the processes for writing in to the log file.
      Now all the process does not synchronize and all the processes write to single log file and some of the logging information is missed.
      How can I create a separate log file for each processes or synchronize all processes to write to single log file

        By writing code to use separate log files?

        I'm not sure where you have problems. You didn't post any code that shows the problem and it seems to me that you are simply continuing a series of fruitless posts of sandy1028 who seems to expect to get his/her code written by us. This is not how this site works. If you want to write a program that runs multiple instances of itself in parallel, you will have to learn how to program first. Generating multiple log file names is not hard.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://758896]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2023-10-01 05:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?