bash loop in parallel

别说谁变了你拦得住时间么 提交于 2019-12-10 17:15:32

问题


I am trying to run this script in parallel, for i<=4 in each set. The runspr.py is itself parallel, and thats fine. What I am trying to do is running only 4 i loop in any instance.

In my present code, it will run everything.

#!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

I have followed https://www.biostars.org/p/63816/ and https://unix.stackexchange.com/questions/35416/four-tasks-in-parallel-how-do-i-do-that but unable to impliment the code in parallel.


回答1:


You don't need to use for loop. You can use gnu parallel like this with find:

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



回答2:


Another possible solution is:

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


来源:https://stackoverflow.com/questions/41247105/bash-loop-in-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!