简体   繁体   English

Shell脚本(BASH)中的多处理

[英]Multiprocessing in Shell Script (BASH)

I've written a script and What I'm wanting to do is get it to run as fast as possible by using sub-processing or background-processing or what ever I need to use in order to speed up the computations. 我已经编写了一个脚本,而我想要做的就是通过使用子处理或后台处理或我需要使用的任何东西来使其尽快运行,以加快计算速度。

So I was looking at using the & symbol in my script to simply speed up processing wherever I could, but when I put it in here, I don't get the results echo'ed to me. 因此,我一直在考虑在脚本中使用&符号来尽可能地加快处理速度,但是当我将其放在此处时,结果并没有回显。

This is the function that I have written and am trying to improve apon. 这是我编写的功能,正在尝试改善Apon。

InTri(){
    while read line
    do
        V1X=$(echo "$line" | awk '{print $1}') &
        V1Z=$(echo "$line" | awk '{print $2}') &
        V2X=$(echo "$line" | awk '{print $3}') &
        V2Z=$(echo "$line" | awk '{print $4}') &
        V3X=$(echo "$line" | awk '{print $5}') &
        V3Z=$(echo "$line" | awk '{print $6}') &
        run=$(echo "$line" | awk '{print $7}') &
        wait
        echo "$V1X $V1Z $V2X $V2Z $V3X $V3Z $run"
    done < <(mysql -u root -ppassword LightCycle -N -e "SELECT V1X, V1Z, V2X, V2Z, V3X, V3Z, ID FROM Temp3 WHERE (V1X <= $x OR V2X <= $x OR V3X <= $x) AND (V1X >= $x OR V2X >= $x OR V3X >= $x) AND (V1Z <= $z OR V2Z <= $z OR V3Z <= $z) AND (V1Z >= $z OR V2Z >= $z OR V3Z >= $z);")
}

I've read the "man bash" (or what of it I could understand) but I don't understand why this is not working. 我已经读过“ man bash”(或我能理解的内容),但我不明白为什么它不起作用。

Help! 救命! :) :)

Thanks to anyone who can, your input is much appreciated. 感谢所有能够提供帮助的人,我们非常感谢您的投入。

Whatever happens in & line happens in a child process, that can't modify parent's environment. 无论发生了什么&行发生在一个子进程,这是不能改变父母的环境。 You'll probably need to open pipes and read from them in parent process. 您可能需要打开管道并在父进程中读取它们。 For tasks with comparable costs of execution with your example it isn't worth the effort. 对于与您的示例具有可比的执行成本的任务,这是不值得的。

In this example you can just do 在这个例子中,你可以做

cat <(echo "$line" | awk '{print $1}') <(echo "$line" | awk '{print $1}') ...

Instead of this whole loop body. 而不是整个循环体。

If you feel your program is getting slower due to those 7 awk commands, you can do something like this without any awk: 如果您由于这7条awk命令而感到程序变慢,则可以执行以下操作而无需任何awk:

$ line="1 2 3 4 5 6 7"
$ a=($line)
$ echo ${a[0]}
1
$ echo ${a[1]}
2
$ echo ${a[@]}
1 2 3 4 5 6 7

where "a" is an array which contains all the elements of the line variable in it. 其中“ a”是一个数组,其中包含line变量的所有元素。

You might think that creating more processes will make it faster, but it might actually make it slower because of the overhead of creating new processes and all that piping. 您可能会认为创建更多进程会使其更快,但实际上可能会因为创建新进程和所有管道的开销而使其变慢。

Why not simply do the following? 为什么不简单地执行以下操作?

mysql -u root -ppassword LightCycle -N -e "SELECT V1X, V1Z, V2X, V2Z, V3X, V3Z, ID FROM Temp3 WHERE (V1X <= $x OR V2X <= $x OR V3X <= $x) AND (V1X >= $x OR V2X >= $x OR V3X >= $x) AND (V1Z <= $z OR V2Z <= $z OR V3Z <= $z) AND (V1Z >= $z OR V2Z >= $z OR V3Z >= $z);" | awk '{print $1" "$2" "$3" "$4" "$5" "$6" "$7}'

Or if you want to read them into variables: 或者,如果您想将它们读入变量:

while read V1X V1Z V2X V2Z V3X V3Z run
do
  echo "$V1X $V1Z $V2X $V2Z $V3X $V3Z $run"
done < <(mysql -u root -ppassword LightCycle -N -e "SELECT V1X, V1Z, V2X, V2Z, V3X, V3Z, ID FROM Temp3 WHERE (V1X <= $x OR V2X <= $x OR V3X <= $x) AND (V1X >= $x OR V2X >= $x OR V3X >= $x) AND (V1Z <= $z OR V2Z <= $z OR V3Z <= $z) AND (V1Z >= $z OR V2Z >= $z OR V3Z >= $z);")

In order to get the script above working as it should, I just needed to remove the & before the "wait" 为了使上面的脚本正常工作,我只需要在“等待”之前删除&

InTri(){
    while read line
    do
        V1X=$(echo "$line" | awk '{print $1}') &
        V1Z=$(echo "$line" | awk '{print $2}') &
        V2X=$(echo "$line" | awk '{print $3}') &
        V2Z=$(echo "$line" | awk '{print $4}') &
        V3X=$(echo "$line" | awk '{print $5}') &
        V3Z=$(echo "$line" | awk '{print $6}') &
        run=$(echo "$line" | awk '{print $7}') 
        wait
        echo "$V1X $V1Z $V2X $V2Z $V3X $V3Z $run"
    done < <(mysql -u root -ppassword LightCycle -N -e "SELECT V1X, V1Z, V2X, V2Z, V3X, V3Z, ID FROM Temp3 WHERE (V1X <= $x OR V2X <= $x OR V3X <= $x) AND (V1X >= $x OR V2X >= $x OR V3X >= $x) AND (V1Z <= $z OR V2Z <= $z OR V3Z <= $z) AND (V1Z >= $z OR V2Z >= $z OR V3Z >= $z);")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM