Каков наилучший способ выполнить 5 curlзапросов parallelиз bash-скрипта? Я не могу запустить их в сериале по соображениям производительности.
Каков наилучший способ выполнить 5 curlзапросов parallelиз bash-скрипта? Я не могу запустить их в сериале по соображениям производительности.
Ответы:
Используйте '&' после команды, чтобы создать фоновый процесс, и 'wait', чтобы дождаться их завершения. Используйте '()' вокруг команд, если вам нужно создать под-оболочку.
#!/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 есть параметр "-P" для параллельного запуска процессов. Например:
wget -nv http://en.wikipedia.org/wiki/Linux -O- | egrep -o "http://[^[:space:]]*.jpg" | xargs -P 10 -r -n 1 wget -nv
Ссылка: http://www.commandlinefu.com/commands/view/3269/parallel-file-downloading-with-wget
Я использую GNU параллельно для таких задач, как это.
curlс gnu parallel?
Вот curlпример с xargs:
$ cat URLS.txt | xargs -P 10 -n 1 curl
Приведенный выше пример должен curlкаждый из URL-адресов параллельно, по 10 за раз. Это -n 1так, что для выполнения xargsиспользуется только 1 строка из URLS.txtфайла curl.
Что делает каждый из параметров 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.