xargs: losing output when redirecting stdout to a file in parallel mode

后端 未结 2 1515
春和景丽
春和景丽 2021-01-03 03:01

I am using GNU xargs (version 4.2.2) in parallel mode and I seem to be reliably losing output when redirecting to a file. When redirecting to a pipe, it appears to work corr

2条回答
  •  孤独总比滥情好
    2021-01-03 03:10

    Your problem is due to the output from different processes being mixed. It is shown here:

    parallel perl -e '\$a=\"1{}\"x10000000\;print\ \$a,\"\\n\"' '>' {} ::: a b c d e f
    ls -l a b c d e f
    parallel -kP4 -n1 grep 1 > out.par ::: a b c d e f
    echo a b c d e f | xargs -P4 -n1 grep 1 > out.xargs-unbuf
    echo a b c d e f | xargs -P4 -n1 grep --line-buffered 1 > out.xargs-linebuf
    echo a b c d e f | xargs -n1 grep 1 > out.xargs-serial
    ls -l out*
    md5sum out*
    

    The solution is to buffer the output from each job - either in memory or in tmpfiles (like GNU Parallel does).

提交回复
热议问题