Qual è il modo migliore per eseguire 5 curlrichieste parallelda uno script bash? Non posso eseguirli in serie per motivi di prestazioni.
Qual è il modo migliore per eseguire 5 curlrichieste parallelda uno script bash? Non posso eseguirli in serie per motivi di prestazioni.
Risposte:
Utilizzare '&' dopo un comando per eseguire il background di un processo e 'wait' per attendere che finiscano. Usa '()' attorno ai comandi se devi creare una sotto-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
xargs ha un parametro "-P" per eseguire processi in parallelo. Per esempio:
wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Riferimento: http://www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget
Uso gnu parallel per compiti come questo.
curlcon gnu parallel?
Ecco un curlesempio con xargs:
$ cat URLS.txt | xargs -P 10 -n 1 curl
L'esempio sopra dovrebbe curlciascuno degli URL in parallelo, 10 alla volta. Il -n 1è là in modo che xargsutilizza solo 1 riga dal URLS.txtfile per curll'esecuzione.
Cosa fanno ciascuno dei parametri di xargs:
$ 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.