【问题标题】:bash pipe limitbash 管道限制
【发布时间】:2011-06-24 10:39:31
【问题描述】:

我有一个我要下载的 url 的 txt 列表

n=1
end=`cat done1 |wc -l`
while [ $n -lt $end ]
do
nextUrls=`sed -n "${n}p" < done1`
wget -N nH --random-wait -t 3 -a download.log -A$1 $nextUrls 
let "n++"
done

我想用管道更快地做到这一点,但如果我这样做了

 wget -N nH --random-wait -t 3 -a download.log -A$1 $nextUrls &

我的内存填满并完全阻塞了我的电脑。 Any1 知道如何将创建的管道限制为同时创建 10 个?

【问题讨论】:

    标签: bash sed limit pipe wget


    【解决方案1】:

    您不是在创建管道 (|),而是在创建后台进程 (&amp;)。每次你的while 执行它的主体时,你都会创建一个新的wget 进程并且不要等待它退出,这(取决于end 的值)可能会创建 lot of @ 987654327@ 处理速度非常快。依次执行(删除&amp;)或者您可以尝试executing n processes in parallel and wait for them

    顺便说一句,cat 没用:你可以这样做:

    end=`wc -l done1`
    

    【讨论】:

    • for ARG in $@; do command $ARG &amp; NPROC=$(($NPROC+1)) if [ "$NPROC" -ge 4 ]; then wait NPROC=0 fi done如何与上面的代码结合起来?
    • 在您的wget ... &amp; 行之后直接输入:NPROC=$(($NPROC+1)) ; if [ "$NPROC" -ge 4 ]; then wait ; NPROC=0 ; fi 这将并行执行wget,一旦四个运行,它会等到所有完成的。然后继续产生下一个。
    【解决方案2】:

    我有一个我想要下载的 url 的 txt 列表...我想做得更快..

    所以这里有一个最短的方法来做到这一点。以下命令从文件 *txt_list_of_urls* 中包含的列表中下载 URL,并行运行 10 个线程:

    xargs -a txt_list_of_urls -P 10 -r -n 1 wget -nv
    

    【讨论】:

    • xargs 是一个非常强大的命令。您应该在 man xargs 上阅读更多相关信息。
    猜你喜欢
    • 2018-08-03
    • 2019-08-23
    • 2023-03-26
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2010-09-06
    • 2012-03-13
    相关资源
    最近更新 更多