From 04781ab6e99fd953c6da1789fcc4db97e29a92e0 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Fri, 21 Feb 2014 10:20:16 +0100 Subject: [PATCH] parallel: Code cleanup. --- src/parallel | 73 ++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/src/parallel b/src/parallel index bd8872a4..3d96b77d 100755 --- a/src/parallel +++ b/src/parallel @@ -626,7 +626,7 @@ sub get_options_from_array { sub parse_options { # Returns: N/A # Defaults: - $Global::version = 20140122; + $Global::version = 20140220; $Global::progname = 'parallel'; $Global::infinity = 2**31; $Global::debug = 0; @@ -4183,6 +4183,17 @@ sub openoutputfiles { unlink $self->fh($fdno,"unlink"); } } + if($opt::linebuffer) { + # Set non-blocking when using --linebuffer + $Global::use{"Fcntl"} ||= eval "use Fcntl qw(:DEFAULT :flock); 1;"; + for my $fdno (1,2) { + my $fdr = $self->fh($fdno,'r'); + my $flags; + fcntl($fdr, &F_GETFL, $flags) || die $!; # Get the current flags on the filehandle + $flags |= &O_NONBLOCK; # Add non-blocking to the flags + fcntl($fdr, &F_SETFL, $flags) || die $!; # Set the flags on the filehandle + } + } } sub set_fh { @@ -4908,48 +4919,37 @@ sub print { my $self = shift; ::debug(">>joboutput ".$self->replaced()."\n"); -# if($opt::pipe and $self->virgin()) { -# # Nothing was printed to this job: -# # cleanup tmp files if --files was set -# close $self->fh(0,"w"); -# for my $fdno (1,2) { -# close $self->fh($fdno,"w"); -# close $self->fh($fdno,"r"); -# # Unlink files that are empty -# unlink $self->fh($fdno,"name"); -# } -# return; -# } if($opt::dryrun) { # Nothing was printed to this job: # cleanup tmp files if --files was set unlink $self->fh(1,"name"); } if($opt::pipe and $self->virgin()) { + # Skip --joblog, --dryrun, --verbose } else { if($Global::joblog) { $self->print_joblog() } - # Printing is only relevant for grouped output. - $Global::grouped or return; - # Check for disk full - exit_if_disk_full(); - my $command = $self->sshlogin_wrap(); - - if(($opt::dryrun or $Global::verbose) and $Global::grouped - and - not $self->{'verbose_printed'}) { - $self->{'verbose_printed'}++; - if($Global::verbose <= 1) { - print STDOUT $self->replaced(),"\n"; - } else { - # Verbose level > 1: Print the rsync and stuff - print STDOUT $command,"\n"; + # Printing is only relevant for grouped output. + $Global::grouped or return; + # Check for disk full + exit_if_disk_full(); + my $command = $self->sshlogin_wrap(); + + if(($opt::dryrun or $Global::verbose) and $Global::grouped + and + not $self->{'verbose_printed'}) { + $self->{'verbose_printed'}++; + if($Global::verbose <= 1) { + print STDOUT $self->replaced(),"\n"; + } else { + # Verbose level > 1: Print the rsync and stuff + print STDOUT $command,"\n"; + } + # If STDOUT and STDERR are merged, + # we want the command to be printed first + # so flush to avoid STDOUT being buffered + flush STDOUT; } - # If STDOUT and STDERR are merged, - # we want the command to be printed first - # so flush to avoid STDOUT being buffered - flush STDOUT; - } } for my $fdno (sort { $a <=> $b } keys %Global::fd) { # Sort by file descriptor numerically: 1,2,3,..,9,10,11 @@ -4968,7 +4968,7 @@ sub print { close $self->fh($fdno,"w"); close $in_fh; if($opt::pipe and $self->virgin()) { - # Nothing was printed to this job: # cleanup tmp files if --files was set + # Nothing was printed to this job: # cleanup unused tmp files if --files was set for my $fdno (1,2) { unlink $self->fh($fdno,"name"); unlink $self->fh($fdno,"unlink"); @@ -5015,7 +5015,7 @@ sub print { if($self->fh($fdno,"rpid") and kill 0, $self->fh($fdno,"rpid")) { # decompress still running - } elsif(defined $self->{'exitstatus'} and length $$partial > 0) { + } elsif(defined $self->{'exitstatus'}) { # If the job is dead: print the remaining partial line # read remaining if($opt::tag or defined $opt::tagstring) { @@ -5023,8 +5023,9 @@ sub print { $$partial =~ s/^/$tag/gm; } print $out_fd $$partial; + # Release the memory $$partial = undef; - # then close fd + # then close fh close $in_fh; } } else {