Выполнять запросы curl параллельно в bash
Как лучше всего выполнить 5 curl
запросы в parallel
из скрипта bash? Я не могу запустить их в сериале по соображениям производительности.
3 ответа
Используйте '&' после команды, чтобы создать фоновый процесс, и '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
Вот 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.