gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:50:26 25 4
gpt4 key购买 nike

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

command1 & command2 & command3 ..... command30

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

但是,当我给

command1 &
command2 &
command3 &
command4 &

wait &

command5 &
command6 &
command7 &
command8 &

wait

.
.
.

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

  • 为什么没有 wait 就“崩溃”了?
  • 有什么方法可以缩短时间,使所有命令都可以在一个命令所用的时间运行吗?
  • 有什么方法可以在不使用 wait 的情况下实现它?

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

提前致谢!

我的原始代码:

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 在您启 Action 业时创建输出文件,但输出缓冲通常会使文件为空或至少不完整,直到该过程完成。等待会迫使您延迟,直到工作真正完成。

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

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

关于linux - 使用 bash '&' 命令并行处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069851/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com