Unixで公開要求を行う関数に対して複数の並列呼び出しを実行するには?

Unixで公開要求を行う関数に対して複数の並列呼び出しを実行するには?

パラメータ fileName を取り、そのファイルを使用して採用サーバーへの curl post 呼び出しを作成する関数 A があります。関数擬似コードは次のとおりです。

function A(filename)
{
// read file from local 
// send it to deployment server and via curl/any rest client
// read response code
// store data of filename and response code 
}

for all files f in folder :
 A(f)

// See all response code and print data, which all files passed and which all failed

今すぐ並列に実行するには、次のようにします。

A(f) & inside for loop

Unixでは簡単に使用できるハッシュマップ/プリタイプのサポートがないため、強調表示された部分、ファイル名の収集方法、レスポンスコードの詳細が心配です。また、Aのデータを印刷する場合、ファイル名を印刷する行と応答コードを印刷する行を保持すると、並列性のため、2行が連続しないことがある。 1行に両方を印刷できますが、後でいくつかの処理のために結果をキャプチャしたいと思います。簡単な方法がありますか?

ベストアンサー1

私は以下を使用しますparset

a() {
  filename="$1"
  echo "***  Here    is  ***"
  sleep 0.$RANDOM
  echo "the response code to"
  sleep 0.$RANDOM
  echo "$filename"
}
export -f a

# Put the results into a bash array. The order will be kept.
# Here we use "File  a" and "File  b" as input. Change those to your needs
parset res a ::: "File  a" "File  b"
echo "${res[0]}"
echo "${res[1]}"

または:

# If you want the output into an assoc array, convert the results:
# First build an array of the input
input=("File  a" "File  b")
# Then run the jobs in parallel
parset res a ::: "${input[@]}"

# Finally zip the two arrays to a single associative array
declare -A myassoc
for ((i=0; $i<${#input[@]}; i++)); do
  myassoc[${input[i]}]=${res[i]}
done
echo "${myassoc["File  a"]}"

parsetGNU Parallelの一部です。

有効にするには、parset機能として有効にする必要があります。この機能はの一部として定義されますenv_parallel

次の手順を実行し、シェルを再起動します。

bash:  Put this in $HOME/.bashrc:  . `which env_parallel.bash`
       E.g. by doing:  echo '. `which env_parallel.bash`' >> $HOME/.bashrc
       Supports: aliases, functions, variables, arrays

zsh:   Put this in $HOME/.zshrc:  . `which env_parallel.zsh`
       E.g. by doing:  echo '. `which env_parallel.zsh`' >> $HOME/.zshenv
       Supports: functions, variables, arrays

fish:  Unsupported

ksh:   Put this in $HOME/.kshrc:  source `which env_parallel.ksh`
       E.g. by doing:  echo 'source `which env_parallel.ksh`' >> $HOME/.kshrc
       Supports: aliases, functions, variables, arrays

mksh:  Put this in $HOME/.mkshrc:  source `which env_parallel.mksh`
       E.g. by doing:  echo 'source `which env_parallel.mksh`' >> $HOME/.mkshrc
       Supports: aliases, functions, variables, arrays

pdksh: Put this in $HOME/.profile:  source `which env_parallel.pdksh`
       E.g. by doing:  echo '. `which env_parallel.pdksh`' >> $HOME/.profile
       Supports: aliases, functions, variables, arrays

ash:   Put this in $HOME/.profile:  . `which env_parallel.ash`
       E.g. by doing:  echo '. `which env_parallel.ash`' >> $HOME/.profile
       Supports: aliases, variables

dash:  Put this in $HOME/.profile:  . `which env_parallel.dash`
       E.g. by doing:  echo '. `which env_parallel.dash`' >> $HOME/.profile
       Supports: aliases, variables

csh:   Unsupported

tcsh:  Unsupported

To install in all shells run:

  parset --install

おすすめ記事