小ネタ。
parallel () {
if [ $# -lt 2 ]; then
echo -e "Usage:\n parallel <N> <command>"
return 1
fi
seq $1 | xargs -I{} -P $@
}
.bashrc
とか .zshrc
とかにこれを書いておくと、
$ parallel 3 echo foo
foo
foo
foo
こんな感じで parallel <並列数> <実行コマンド>
で同じコマンドを並列実行できて便利。
$ parallel 3 echo process_{}
process_1
process_3
process_2
さらに、実行コマンド内の {}
は seq
が生成したユニークな番号に置き換えられるので、これも何かに使えるかもしれない (並列実行なので順序はバラバラ)
[追記] GNU Parallel
このエントリを書いた当時は知らなかったけど、GNU Parallel という便利コマンドがあってこちらの方が高性能。