2016-12-20 71 views
3

我試圖並行運行這個腳本,因爲我在每個集合中都有< = 4。 runspr.py本身是平行的,這很好。我試圖做的是在任何情況下只運行4個i循環。bash循環並行

在我現在的代碼中,它會運行一切。

#!bin/bash 
for i in * 
do 
    if [[ -d $i ]]; then 
    echo "$i id dir" 
    cd $i 
     python3 ~/bin/runspr.py SCF & 
    cd .. 
    else 
    echo "$i nont dir" 
    fi 
done 

我按照https://www.biostars.org/p/63816/https://unix.stackexchange.com/questions/35416/four-tasks-in-parallel-how-do-i-do-that 但無法impliment代碼並行。

回答

5

您不需要使用for循環。您可以使用gnu parallel這樣與find

find . -mindepth 1 -maxdepth 1 -type d ! -print0 | 
parallel -0 --jobs 4 'cd {}; python3 ~/bin/runspr.py SCF' 
+0

搜索:'水貨「CD {}; python3〜/ bin/runspr.py SCF'::: * /' –

0

另一種可能的解決方案是:

find . -mindepth 1 -maxdepth 1 -type d ! -print0 | 
xargs -I {} -P 4 sh -c 'cd {}; python3 ~/bin/runspr.py SCF'