Hello hello,
I have a perl script that I need to run a large number of times on different input data. I use Grid Cluster to send all the processes to our compute cluster.
All the processes are supposed to write to the same output files, of which there are four. However, in my first attempts the data was jumbled. I would get things like:
line1 data data data line2 data data dataline3 data data data line4 data data data line5 data data data line6 dataline7 data data data data line8 data data data data
I took this to mean that sometimes one process didn't quite finish printing when another process tried writing to the file, so I figured I was going to have to lock the output file before writing to it.
I read several tutorials, among which File locking, but it still doesn't work. Below are simplified versions of my scripts. Can someone help me?
use strict; use warnings; use GRID::Cluster; my $script = "script.plx"; my @processes = (...); my @machines = (...); my %max_num_processes = (...); my $cluster = GRID::Cluster->new( host_names => \@machines, max_num_np => \%max_num_processes, ); $cluster->qx(@processes);
use strict; use warnings; use Fcntl qw(:flock); my @letters = qw(a b c d); foreach my $letter (@letters) { my $output_file = "output_" . $letter . ".txt"; # This didn't solve my problem # I also tried +< instead of >>, but nothing happened at all # when I did that # open my $filehandle, ">>$output_file"; # flock($filehandle, LOCK_EX); # So instead I tried the version in the comments on the # tutorial page although I don't really understand it :-( open my $semaphore, ">$output_file.lock"; flock($semaphore, LOCK_EX); open my $filehandle, ">>$output_file"; print $filehandle "line$number data data data\t"; close $filehandle; close $semaphore; }
In reply to Threads and output files (locking) by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |