parallel: Generalized openoutputfiles().

This commit is contained in:
Ole Tange 2013-08-21 21:24:47 +02:00
parent 2c587e4055
commit 26fd31c2de

View file

@ -262,8 +262,26 @@ if($opt::nonall or $opt::onall) {
wait_and_exit(min(undef_as_zero($Global::exitstatus),254)); wait_and_exit(min(undef_as_zero($Global::exitstatus),254));
} }
$Global::JobQueue = JobQueue->new( $Global::JobQueue = JobQueue->new(
$command,\@fhlist,$Global::ContextReplace,$number_of_args,\@Global::ret_files); $command,\@fhlist,$Global::ContextReplace,$number_of_args,\@Global::ret_files);
if($opt::pipe and @opt::a) {
# Disabled for now
# ... | parallel --pipe cmd ::: arg1 arg2
# The command to run is:
# tee >((cmd arg1) >/tmp/tmp1 2>/tmp/err1) >((cmd arg2) >/tmp/tmp2 2>/tmp/err2) >/dev/null
my @jobs;
# Get all jobs
while(not $Global::JobQueue->empty()) {
push @jobs, $Global::JobQueue->get();
}
$command = "tee ".join("",map {">((".$_->replaced().") >/tmp/tmp) "} @jobs)
." >/dev/null";
$Global::JobQueue = JobQueue->new(
$command,\@fhlist,$Global::ContextReplace,$number_of_args,\@Global::ret_files);
}
if($opt::eta) { if($opt::eta) {
# Count the number of jobs before starting any # Count the number of jobs before starting any
$Global::JobQueue->total_jobs(); $Global::JobQueue->total_jobs();
@ -3673,29 +3691,43 @@ sub seq {
return $self->{'commandline'}->seq(); return $self->{'commandline'}->seq();
} }
sub openresultsfile { sub openoutputfiles {
my $self = shift; my $self = shift;
my ($outfh,$errfh,$outname,$errname);
if($opt::results) {
my $args_as_dirname = $self->{'commandline'}->args_as_dirname(); my $args_as_dirname = $self->{'commandline'}->args_as_dirname();
my ($outfh,$errfh,$name,$dir);
# prefix/name1/val1/name2/val2/ # prefix/name1/val1/name2/val2/
$dir = $opt::results."/".$args_as_dirname; my $dir = $opt::results."/".$args_as_dirname;
File::Path::mkpath($dir); File::Path::mkpath($dir);
# prefix/name1/val1/name2/val2/stdout # prefix/name1/val1/name2/val2/stdout
$name = "$dir/stdout"; $outname = "$dir/stdout";
if(not open($outfh,"+>",$name)) { if(not open($outfh,"+>",$outname)) {
::error("Cannot write to `$name'.\n"); ::error("Cannot write to `$outname'.\n");
::wait_and_exit(255); ::wait_and_exit(255);
} }
$self->set_fd_file_name(1,$name);
# prefix/name1/val1/name2/val2/stderr # prefix/name1/val1/name2/val2/stderr
$name = "$dir/stderr"; $errname = "$dir/stderr";
if(not open($errfh,"+>",$name)) { if(not open($errfh,"+>",$errname)) {
::error("Cannot write to `$name'.\n"); ::error("Cannot write to `$errname'.\n");
::wait_and_exit(255); ::wait_and_exit(255);
} }
$self->set_fd_file_name(2,$name); } elsif($Global::grouped) {
open OUT, '>&', $outfh or ::die_bug("Can't redirect STDOUT: $!"); # To group we create temporary files for STDOUT and STDERR
open ERR, '>&', $errfh or ::die_bug("Can't dup STDOUT: $!"); # To avoid the cleanup unlink the files immediately (but keep them open)
($outfh, $outname) = ::tempfile(SUFFIX => ".par");
$opt::files or unlink $outname;
($errfh, $errname) = ::tempfile(SUFFIX => ".par");
unlink $errname;
$errname = "";
} else {
# --ungroup
$outfh = *STDOUT;
$errfh = *STDERR;
$outname = "";
$errname = "";
}
$self->set_fd_file_name(1,$outname);
$self->set_fd_file_name(2,$errname);
$self->set_fd(1,$outfh); $self->set_fd(1,$outfh);
$self->set_fd(2,$errfh); $self->set_fd(2,$errfh);
} }
@ -4305,34 +4337,19 @@ sub start {
} }
} }
local (*IN,*OUT,*ERR);
my $pid; my $pid;
if($opt::results) { $job->openoutputfiles();
$job->openresultsfile(); my($stdout_fh,$stderr_fh) = ($job->fd(1),$job->fd(2));
} elsif($Global::grouped) { local (*IN,*OUT,*ERR);
my ($outfh,$errfh,$name); open OUT, '>&', $stdout_fh or ::die_bug("Can't redirect STDOUT: $!");
# To group we create temporary files for STDOUT and STDERR open ERR, '>&', $stderr_fh or ::die_bug("Can't dup STDOUT: $!");
# To avoid the cleanup unlink the files immediately (but keep them open)
($outfh, $name) = ::tempfile(SUFFIX => ".par");
$job->set_fd_file_name(1,$name);
$opt::files or unlink $name;
($errfh, $name) = ::tempfile(SUFFIX => ".par");
unlink $name;
open OUT, '>&', $outfh or ::die_bug("Can't redirect STDOUT: $!");
open ERR, '>&', $errfh or ::die_bug("Can't dup STDOUT: $!");
$job->set_fd(1,$outfh);
$job->set_fd(2,$errfh);
} else {
(*OUT,*ERR)=(*STDOUT,*STDERR);
}
if(($opt::dryrun or $Global::verbose) and not $Global::grouped) { if(($opt::dryrun or $Global::verbose) and not $Global::grouped) {
if($Global::verbose <= 1) { if($Global::verbose <= 1) {
print OUT $job->replaced(),"\n"; print $stdout_fh $job->replaced(),"\n";
} else { } else {
# Verbose level > 1: Print the rsync and stuff # Verbose level > 1: Print the rsync and stuff
print OUT $command,"\n"; print $stdout_fh $command,"\n";
} }
} }
if($opt::dryrun) { if($opt::dryrun) {