Jaki jest najlepszy sposób na wykonanie 5 curlżądań parallelze skryptu bash? Nie mogę uruchomić ich szeregowo ze względu na wydajność.
Jaki jest najlepszy sposób na wykonanie 5 curlżądań parallelze skryptu bash? Nie mogę uruchomić ich szeregowo ze względu na wydajność.
Odpowiedzi:
Użyj polecenia „&” po poleceniu, aby uruchomić proces w tle, i „poczekaj”, aby poczekać na zakończenie procesu. Użyj „()” wokół poleceń, jeśli chcesz utworzyć podpowłokę.
#!/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 ma parametr „-P” do równoległego uruchamiania procesów. Na przykład:
wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Odniesienie: http://www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget
Używam GNU równolegle do takich zadań.
curlz gnu parallel?
Oto curlprzykład z xargs:
$ cat URLS.txt | xargs -P 10 -n 1 curl
Powyższy przykład powinien zawierać curlkażdy z adresów URL równolegle, po 10 naraz. -n 1Jest tak, że xargsużywa tylko 1 linię z URLS.txtpliku za curlwykonanie.
Co każdy z parametrów xargs robi:
$ 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.