GNU Parallelは私の機能からすべてのコマンドを同時に実行します。

GNU Parallelは私の機能からすべてのコマンドを同時に実行します。

さて、複数のフォルダに適用されるbash機能があります。

function task(){
do_thing1
do_thing2
do_thing3
...
}

この機能を並列に実行したいと思います。これまで私は少しフォークトリックを使用しています。

N=4 #core number
for temp_subj in ${raw_dir}/MRST*
do
  ((i=i%N)); ((i++==0)) && wait
  task "$temp_subj" &
done

そしてそれは素晴らしい作品です。しかし、私は「よりきれいな」ものを選択し、GNU Parallelを使用することにしました。

ls -d ${raw_dir}/MRST* | parallel task {}

問題は、私の作業関数のdo_thingを含むすべてを並列に配置することです。順番に実行する必要があるため、必然的に競合が発生します。さまざまな方法で並列呼び出しを修正してみましたが、何も機能しないようです。どんなアイデアがありますか?

ベストアンサー1

あなたの問題は以下に関連していると思いますdo_thingX

do_thing() { echo Doing "$@"; sleep 1; echo Did "$@"; }
export -f do_thing
do_thing1() { do_thing 1 "$@"; }
do_thing2() { do_thing 2 "$@"; }
do_thing3() { do_thing 3 "$@"; }
# Yes you can name functions ... - it is a bit unconventional, but it works
...() { do_thing ... "$@"; }
export -f do_thing1
export -f do_thing2
export -f do_thing3
export -f ...

function task(){
  do_thing1
  do_thing2
  do_thing3
  ...
}
export -f task
# This should take 4 seconds for a single input
ls ${raw_dir}/MRST* | time parallel task {}

またはGNU Parallelを使用していませんparallel。 GNUと並列であることを確認してください。

$ parallel --version
GNU parallel 20201122
Copyright (C) 2007-2020 Ole Tange, http://ole.tange.dk and Free Software
Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
GNU parallel comes with no warranty.

Web site: https://www.gnu.org/software/parallel

When using programs that use GNU Parallel to process data for publication
please cite as described in 'parallel --citation'.

おすすめ記事