在 bash 脚本中并行运行数千个 curl 后台进程

zav*_*avg 16 linux performance bash wget curl

我在以下 bash 脚本中并行运行数以千计的curl后台进程

START=$(date +%s)
for i in {1..100000}
do       
    curl -s "http://some_url_here/"$i  > $i.txt&
    END=$(date +%s)
    DIFF=$(( $END - $START ))
    echo "It took $DIFF seconds"
done
Run Code Online (Sandbox Code Playgroud)

我有 49Gb Corei7-920 专用服务器(非虚拟)。

我通过top命令跟踪内存消耗和 CPU ,它们离界限很远。

ps aux | grep curl | wc -l用来计算当前curl进程的数量。这个数字迅速增加到 2-4 千,然后开始不断减少。

如果我通过管道 curl 添加简单的解析到 awk ( curl | awk > output) 比 curl 进程数量增加到 1-2 千,然后减少到 20-30 ...

为什么进程数量减少如此显着?这种架构的边界在哪里?

Ole*_*nge 15

按照严格的问题:

mycurl() {
    START=$(date +%s)
    curl -s "http://some_url_here/"$1  > $1.txt
    END=$(date +%s)
    DIFF=$(( $END - $START ))
    echo "It took $DIFF seconds"
}
export -f mycurl

seq 100000 | parallel -j0 mycurl
Run Code Online (Sandbox Code Playgroud)

如果您不需要时间周围的样板文本,则更短:

seq 100000 | parallel -j0 --joblog log curl -s http://some_url_here/{} ">" {}.txt
cut -f 4 log
Run Code Online (Sandbox Code Playgroud)

如果您想并行运行 1000 秒,您将遇到一些限制(例如文件句柄)。提高 ulimit -n 或 /etc/security/limits.conf 可能会有所帮助。

  • 引用它:`seq 100 | 并行 'echo 这里是命令 1: {}; echo 这里是命令 2:{}'`。花一个小时浏览本教程。你的命令行会喜欢你的:`man parallel_tutorial` (2认同)

jth*_*ill 2

for i in {1..100000}
Run Code Online (Sandbox Code Playgroud)

只有65536个端口。节流这个。

for n in {1..100000..1000}; do   # start 100 fetch loops
        for i in `eval echo {$n..$((n+999))}`; do
                echo "club $i..."
                curl -s "http://some_url_here/"$i  > $i.txt
        done &
        wait
done
Run Code Online (Sandbox Code Playgroud)

(编辑:( 编辑:删除有关操作系统限制的严重过时的断言并添加缺失的)echocurl
wait