Qual é a melhor maneira de executar 5 curlsolicitações em parallelum script bash? Não posso executá-los em série por motivos de desempenho.
Qual é a melhor maneira de executar 5 curlsolicitações em parallelum script bash? Não posso executá-los em série por motivos de desempenho.
Respostas:
Use '&' após um comando para fazer o segundo plano de um processo e 'wait' para esperar que eles terminem. Use '()' ao redor dos comandos se precisar criar um sub-shell.
#!/bin/bash
curl -s -o foo http://example.com/file1 && echo "done1" &
curl -s -o bar http://example.com/file2 && echo "done2" &
curl -s -o baz http://example.com/file3 && echo "done3" &
wait
O xargs possui um parâmetro "-P" para executar processos em paralelo. Por exemplo:
wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Referência: http://www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget
Eu uso o gnu paralelo para tarefas como esta.
curlcom gnu parallel?
Aqui está um curlexemplo com xargs:
$ cat URLS.txt | xargs -P 10 -n 1 curl
O exemplo acima deve curlcada um dos URLs em paralelo, 10 por vez. O -n 1existe para que xargsapenas use 1 linha do URLS.txtarquivo por curlexecução.
O que cada um dos parâmetros xargs faz:
$ man xargs
-P maxprocs
Parallel mode: run at most maxprocs invocations of utility at once.
-n number
Set the maximum number of arguments taken from standard input for
each invocation of utility. An invocation of utility will use less
than number standard input arguments if the number of bytes
accumulated (see the -s option) exceeds the specified size or there
are fewer than number arguments remaining for the last invocation of
utility. The current default value for number is 5000.