【问题标题】:Run multiple Wget commands simultaneously [duplicate]同时运行多个Wget命令[重复]
【发布时间】:2021-09-17 15:22:50
【问题描述】:

我有一个文本文件,每行都有一个 url 和所需的文件名(用空格分隔)。我正在循环浏览此文本文件并下载 url 并使用 wget 将其保存为所需的名称:

while IFS=' ' read a b
 do wget $a -O $b
done < list.txt

问题是我的列表包含近 9000 个文件,因此逐个下载它们需要很长时间。反正我可以同时下载它们吗?

【问题讨论】:

  • * wget $a -O $b & *?
  • bash limit number of background jobs 上进行谷歌搜索会找到几个匹配项,例如thisthisthisthisthisthis;各种答案中解决了几种方法......其中一些应该适用于这种情况
  • 顺便说一句,在使用 GNU 工具的系统上,xargs -P 是你的朋友。
  • xargs -a list.txt -n 2 -P 4 -I{} wget "{}" -O "{}"
  • @LéaGris 我不确定这段代码是否将 url 与文件名分开,因为我收到一条错误消息,指出文件未找到,因为它刚刚合并了每一行

标签: linux bash shell parallel-processing wget


【解决方案1】:

你可以这样做:

number=10
while IFS=' ' read a b; do 
    if  test $(jobs | grep Running | wc -l) -ge $number; then
        sleep 1
    else
        echo "Starting $a..."
        wget $a -O $b &
    fi
done < list.txt
wait

【讨论】:

    【解决方案2】:

    尝试:

    while IFS=' ' read a b; do 
      wget $a -O $b &
    done < list.txt
    
    # Wait for background jobs to terminate...
    wait
    

    PS:请记住,9000个并发连接可能不会由服务器处理,因此我建议您将连接数限制在块中。

    【讨论】:

    • 我不想做 9000 个并发连接,但可能是 10 个这样的小数字。如何将其限制为 10?
    • 您可以通过在循环中放置一个计数器来限制它们。您必须自己编写代码。
    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2015-08-18
    • 1970-01-01
    • 2013-08-13
    相关资源
    最近更新 更多