make:并行运行多个任务,然后等待完成 [英] make: Run several tasks in parallel and wait for completion

查看:298
本文介绍了make:并行运行多个任务,然后等待完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的makefile文件中的一个目标是非常耗时且耗时的任务.但是我可以拆分工作负载并并行运行几次任务,以加快整个过程.

One target in my makefile is a very CPU and time consuming task. But I can split the workload and run the task several times in parallel to speed up the entire process.

我的问题是make不会等待所有过程完成.

My problem is that make doesn't wait for all processes to complete.

考虑这个名为myTask.sh的简单脚本:

Consider this simple script, named myTask.sh:

#!/bin/bash

echo "Sleeping $1 seconds"
sleep $1
echo "$1 are over!"

现在,让我们从bash脚本中调用它,并使用wait等待所有任务完成:

Now, let's call this from a bash script, and use wait to wait for all tasks to complete:

#!/bin/bash

echo "START"
./myTask.sh 5 &
./myTask.sh 15 &
./myTask.sh 10 &

wait  # Wait for all tasks to complete

echo "DONE"

输出符合预期:

START
Sleeping 15 seconds
Sleeping 5 seconds
Sleeping 10 seconds
5 are over!
10 are over!
15 are over!
DONE

但是在Makefile中尝试相同的操作时:

But when trying the same in a Makefile:

test:
    echo "START"
    ./myTask.sh 5 &
    ./myTask.sh 15 &
    ./myTask.sh 10 &
    wait
    echo "DONE"

不起作用:

START
Sleeping 5 seconds
Sleeping 15 seconds
Sleeping 10 seconds
DONE
sweber@pc:~/testwait $5 are over!
10 are over!
15 are over!

当然,我可以创建多个目标,这些目标可以由make并行构建",或者让make运行运行任务的bash脚本. 但是有没有办法像我已经尝试过的那样做呢?

Of course, I could create multiple targets which can be "built" in parallel by make, or let make just run the bash script which runs the tasks. But is there a way to do it more like what I already tried?

推荐答案

配方中的每个逻辑行都在其自己的shell中调用.因此,您的makefile基本上正在运行:

Each individual logical line in a recipe is invoked in its own shell. So your makefile is basically running this:

test:
        /bin/sh -c 'echo "START"'
        /bin/sh -c './myTask.sh 5 &'
        /bin/sh -c './myTask.sh 15 &'
        /bin/sh -c './myTask.sh 10 &'
        /bin/sh -c 'wait'
        /bin/sh -c 'echo "DONE"'

您应该使用分号和反斜杠/换行符使它们全部在单个shell中运行,以将物理行组合为一条逻辑行:

You should make it all run in a single shell using semicolons and backslash/newlines to combine the physical lines into one logical line:

test:
        echo "START"; \
        ./myTask.sh 5 & \
        ./myTask.sh 15 & \
        ./myTask.sh 10 & \
        wait; \
        echo "DONE"

这篇关于make:并行运行多个任务,然后等待完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆