Який найкращий спосіб виконати 5 curlзапитів parallelіз сценарію bash? Я не можу запускати їх послідовно з міркувань продуктивності.
Який найкращий спосіб виконати 5 curlзапитів parallelіз сценарію bash? Я не можу запускати їх послідовно з міркувань продуктивності.
Відповіді:
Використовуйте "&" після команди, щоб фоновий процес, і "зачекайте", щоб дочекатися їх завершення. Використовуйте "()" навколо команд, якщо вам потрібно створити підзагін.
#!/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.