使用 bash '&' 命令并行处理

Parallel processing with bash '&' command

我正在尝试 运行 使用不同的参数同时多次 运行 一个 R 文件。 GNU Parallel 对我来说不是一个选项,因为我 运行 在我无权安装 Parallel 的服务器上安装它。所以我选择了bash命令&.

command1 & command2 & command3 ..... command30

但是,当我 运行 这样做时,它的行为并不像预期的那样。在命令中,我将每个命令的输出保存到一个新文件中,我注意到一些文件是空的。其实大部分。所以我猜只写了上面的内容,一些进程就被杀死了。

然而,当我给

command1 &
command2 &
command3 &
command4 &

wait &

command5 &
command6 &
command7 &
command8 &

wait 

.
.
.

它工作正常,但问题是它只比 运行ning 花费了将近 5 倍的时间 command1 因为它要等到前面的命令完成。时间在这里是非常重要的因素,我希望所有命令同时(几乎)运行,因为它只需要一个命令。

(从技术上讲,我知道 & 不会并行生成进程 运行,但它会为每个命令创建不同的进程)。

提前致谢!

我的原码:

Rscript svmRScript_v2.r 0.05 1 > output/out0.1-10.txt &
Rscript svmRScript_v2.r 0.05 2 > output/out0.05-2.txt &
Rscript svmRScript_v2.r 0.05 5 > output/out0.05-5.txt &
Rscript svmRScript_v2.r 0.05 10 > output/out0.05-10.txt &

wait &

Rscript svmRScript_v2.r 0.05 50 > output/out0.05-50.txt &
Rscript svmRScript_v2.r 0.05 100 > output/out0.05-100.txt &
Rscript svmRScript_v2.r 0.05 500 > output/out0.05-500.txt &
Rscript svmRScript_v2.r 0.01 1 > output/out0.1-10.txt &

wait &

Rscript svmRScript_v2.r 0.01 2 > output/out0.01-2.txt &
Rscript svmRScript_v2.r 0.01 5 > output/out0.01-5.txt &
Rscript svmRScript_v2.r 0.01 10 > output/out0.01-10.txt &
Rscript svmRScript_v2.r 0.01 50 > output/out0.01-50.txt &

wait &

Rscript svmRScript_v2.r 0.01 100 > output/out0.01-100.txt &
Rscript svmRScript_v2.r 0.01 500 > output/out0.01-500.txt &
Rscript svmRScript_v2.r 0.005 1 > output/out0.1-10.txt &
Rscript svmRScript_v2.r 0.005 2 > output/out0.005-2.txt

您检查文件时,文件尚未写出。使用输出重定向,shell 在您开始作业时创建输出文件,但输出缓冲通常会使文件为空或至少不完整,直到该过程完成。等待迫使你推迟到工作真正完成。

除非单个作业大部分都在等待某些外部资源,否则期望并行处理在您可以执行一个作业的同时执行两个作业是非常不合理的。更多的工作需要更多的时间。

如果您启动太多后台进程,您实际上会更慢地完成作业,因为任务切换会占用更多的可用处理能力。只试验几个,尤其是当它们受到严重 CPU 约束时。五个作业的批次可能是一个合理的起点,但瓶颈显然完全取决于这些脚本实际在做什么。