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

I'm writing a script that iteratively runs a command on many small filess and reaps the output. The commands each take 1-2 seconds to run, and for the most part, this works. However, there's one particular instance where if I use a particular input file, then waitpid() stalls.

The command itself runs fine with that particular file in bash, and I'm aware that I can use WNOHANG to prevent waitpid from causing the script itself to stop, but I'm struggling to understand why waitpid() stalls at the same file each time.

The (incomplete) section of code I'm using is:

my $cmd = "blastp -query ".$TmpDir."/".$count.".fasta -db ".$Dir."/".$ +count.".db -outfmt 6"; $pid=open3($wtr, $rdr, $err, $cmd); waitpid($pid, 0);

Replies are listed 'Best First'.
Re: waitpid stalling on one particular file
by salva (Canon) on Sep 25, 2014 at 07:04 UTC
    Use strace or some similar utility to see what is happening at the OS level.

    Probably the child process doesn't finish because it is waiting for some I/O.

      I did an strace -f on the command, and here's the end of the output (path edited out):

      [pid 24623] stat("/path/65.db.psq", {st_mode=S_IFREG|0666, st_size=811 +5, ...}) = 0 [pid 24623] open("/path/65.db.psq", O_RDONLY) = 6 [pid 24623] mmap(NULL, 8115, PROT_READ, MAP_PRIVATE, 6, 0) = 0x7f78165 +73000 [pid 24623] write(1, "AT5G51490.1.CDS\tAT4G00190.1\t46.0"..., 1340) = +1340 [pid 24623] write(1, "AT2G26450.1.CDS\tAT1G53830.1\t40.5"..., 1615) = +1615 [pid 24623] write(1, "AT1G11580.1.CDS\tAT3G14310.1\t50.1"..., 1286) = +1286 [pid 24623] write(1, "AT2G45220.1.CDS\tAT4G00190.1\t52.6"..., 1279) = +1279 [pid 24623] write(1, "AT3G14310.1.CDS\tAT3G14310.1\t100."..., 1270) = +1270 [pid 24623] write(1, "AT5G55590.1.CDS\tAT5G55590.1\t100."..., 1452 <un +finished ...>

      I guess this isn't a perl question, but it doesn't make sense to me that writing to a file would freeze.

        It freezes because file descriptor 1 (stdout) is attached to a pipe and nobody is reading from the other side.

        Why are you using open3 to launch the process?