【问题标题】:How to parallel process a function, with loops如何使用循环并行处理函数
【发布时间】:2018-11-09 20:24:44
【问题描述】:

所以我有这个函数,我希望这个函数同时运行它本身包含的所有内容。到目前为止,它不起作用,并且根据其他消息来源,这就是您的操作方式。如果函数不是并行的,函数本身就可以工作。

#!/bin/bash
foo () {
    cd ${HOME}/sh/path/to/script/execute
    for f in *.sh; do  #goes to "execute" directory and executes all 
    #scripts the current directory "execute" basically run-parts without cron

        cd ~/sh/path/to/script
        while IFS= read -r l1 #Line 1 in master.txt
              IFS= read -r l2 #Line 2 in master.txt
              IFS= read -r l3 #Line 3 in master.txt
        do
        cd /dev/shm/arb
        echo ${l1} > arg.txt & echo ${l2} > arg2.txt & echo ${l3} > arg3.txt 
        cd ${HOME}/sh/path/to/script/execute 
        bash -H ${f} #executes all scripts inside "execute" folder
        cd ~/sh/path/to/script/here
        ./here.sh &
        cd ~/sh/path/to/script &
        done <master.txt
    done

}

export -f foo
parallel ::: foo

结果

#No result at all....., just buffers. htop doesn't acknowledge any 
#processes, and when this runs its pretty taxing on the cores.

master.txt 内容

如果这是相关的:

apple_fruit
apple_veggie
veggie_fruit
#apple changes
pear_fruit
pear_veggie
veggie_fruit
#pear changes
cucumber_fruit
...

我对使用并行非常陌生,不知道它在高级(和基本)情况下是如何工作的,所以循环会干扰吗?如果它确实干扰,是否有解决方法?

【问题讨论】:

  • GNU Parallel 的理念是你有很多事情要做,你可以并行执行很多foo() 的实例。所以,你需要瞄准parallel foo ::: &lt;lots of things&gt;
  • 你认为cd somewhere &amp; 是做什么的?它在后台启动一个新进程,然后更改目录并退出......而不影响前台进程。
  • 这是一个错字,我删除了它
  • 你不能同时将数十或数百个并行任务全部写入arg[1-3].txt - 你会弄得一团糟!
  • 唯一应该进去的就是master.txt的前三行,它会重写,因为它没有被附加。

标签: bash loops parallel-processing sh gnu-parallel


【解决方案1】:

结果可能是这样的:

inner() {
  script="$1"
  parallel -N3 "'$script' {}; here.sh  {}"  :::: master.txt
}
export -f inner
parallel inner ::: ${HOME}/sh/path/to/script/execute/*.sh

这将使用来自 master.txt 的 3 个参数调用 ${HOME}/sh/path/to/script/execute/(和 here.sh)中的每个脚本,如下所示:

${HOME}/sh/path/to/script/execute/script1.sh apple_fruit apple_veggie veggie_fruit

您需要更改脚本以便:

  • 他们从命令行获取参数(不是从arg.txtarg2.txtarg3.txt)。
  • 他们将输出发送到标准输出

【讨论】:

  • 如果你放parallel inner ::: ${HOME}/sh/path/to/script/execute/*.sh {insert command here to be referenced by $2},可以用$2来引用并行
  • 这里我提出了这个问题。 stackoverflow.com/questions/50636075/…
猜你喜欢
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-26
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
相关资源
最近更新 更多