bash 并行循环

标签 bash gnu-parallel

我正在尝试并行运行此脚本,因为每个集合中的 i<=4。 runspr.py 本身是并行的,这很好。我想做的是在任何情况下都只运行 4 i 循环。

在我目前的代码中,它将运行所有内容。

#!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 但无法并行实现代码。

最佳答案

您不需要使用for 循环。您可以使用 gnu parallel像这样使用 find:

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

关于bash 并行循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41247105/

相关文章:

linux - 更新所有目录中的 shell 脚本更改

bash - bash 是一种编程语言吗?

ffmpeg - 使用 ffmpeg 批量转换文件并找到 -exec 和并行

parallel-processing - GNU parallel 有两个参数

arrays - 使用管道运行 bash 数组

regex - 如何仅打印与 sed 匹配的内容?

c++ - 用于 int3 的 Cuda AtomicAdd

python - 使用 GNU parallel 并行化多线程命令

bash - 同时运行三个shell脚本

linux - GNU 并行 : execute one command parallel for all files in a folder