有一些类似的问题,但我的问题不是“并行运行多个程序”-这可以用parallel
或xargs
简单地完成。
我需要并行化Bash函数。
让我们想象一下这样的代码:
for i in "${list[@]}"
do
for j in "${other[@]}"
do
# some processing in here - 20-30 lines of almost pure bash
done
done
有些处理需要调用外部程序。
我想运行一些(4-10)个任务,每个任务运行不同的$i
。$list中的元素总数大于500。
我知道我可以把整个for j ... done
循环放在外部脚本中,然后并行调用这个程序,但是有没有可能不在两个单独的程序之间拆分功能呢?
发布于 2013-06-26 09:14:44
编辑:请考虑使用Ole's answer。
您可以将代码放在单独的bash函数中,而不是单独的脚本。然后,您可以导出它,并通过xargs运行它:
#!/bin/bash
dowork() {
sleep $((RANDOM % 10 + 1))
echo "Processing i=$1, j=$2"
}
export -f dowork
for i in "${list[@]}"
do
for j in "${other[@]}"
do
printf "%s\0%s\0" "$i" "$j"
done
done | xargs -0 -n 2 -P 4 bash -c 'dowork "$@"' --
发布于 2013-06-26 17:27:35
sem
是GNU Parallel的一部分,是为这种情况而设计的。
for i in "${list[@]}"
do
for j in "${other[@]}"
do
# some processing in here - 20-30 lines of almost pure bash
sem -j 4 dolong task
done
done
如果你更喜欢这个功能,GNU Parallel可以一次完成dual for循环:
dowork() {
echo "Starting i=$1, j=$2"
sleep 5
echo "Done i=$1, j=$2"
}
export -f dowork
parallel dowork ::: "${list[@]}" ::: "${other[@]}"
发布于 2018-01-12 02:48:12
并行运行多行命令的解决方案:
for ...your_loop...; do
if test "$(jobs | wc -l)" -ge 8; then
wait -n
fi
{
any bash commands here
} &
done
wait
在您的案例中:
for i in "${list[@]}"
do
for j in "${other[@]}"
do
if test "$(jobs | wc -l)" -ge 8; then
wait -n
fi
{
your
multi-line
commands
here
} &
done
done
wait
如果已经有8个bash作业在运行,wait
将等待至少一个作业完成。如果/当作业较少时,它将异步启动新作业。
此方法的优点:
man
):parallel在启动时很慢--第一次大约是250ms,之后是150ms。
bash
即可工作。缺点:
wait
拥有比所需的更少的工作。但是,它将在至少一个作业完成时恢复,或者如果有0个作业正在运行,则立即恢复(在这种情况下,wait -n
会立即退出)。&
),则循环中的工作进程会更少。https://stackoverflow.com/questions/17307800
复制相似问题