有一些类似的问题,但我的问题不是"并行运行几个程序" - 这可以通过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
Run Code Online (Sandbox Code Playgroud)
某些处理需要调用外部程序.
我想运行一些(4-10)任务,每个任务运行不同$i
.$ list中的元素总数> 500.
我知道我可以将整个for j ... done
循环放在外部脚本中,并且只是并行调用该程序,但是可以不在两个单独的程序之间分割功能吗?
Ole*_*nge 42
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
Run Code Online (Sandbox Code Playgroud)
如果你更喜欢这个函数,GNU Parallel可以一次性完成双for循环:
dowork() {
echo "Starting i=$1, j=$2"
sleep 5
echo "Done i=$1, j=$2"
}
export -f dowork
parallel dowork ::: "${list[@]}" ::: "${other[@]}"
Run Code Online (Sandbox Code Playgroud)
tha*_*guy 16
编辑:请考虑Ole的回答.
您可以将代码放在单独的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 "$@"' --
Run Code Online (Sandbox Code Playgroud)
并行运行多行命令的解决方案:
for ...your_loop...; do
if test "$(jobs | wc -l)" -ge 8; then
wait -n
fi
{
any bash commands here
} &
done
wait
Run Code Online (Sandbox Code Playgroud)
在你的情况下:
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
Run Code Online (Sandbox Code Playgroud)
如果已经有 8 个 bash 作业正在运行,wait
则将等待至少一个作业完成。如果/当作业较少时,它会异步启动新作业。
这种方法的好处:
man
)进行比较:并行启动时很慢 - 第一次大约 250 毫秒,之后大约 150 毫秒。
bash
工作。缺点:
wait
的作业少于所需的数量。但是,它将在至少一个作业完成时恢复,或者如果有 0 个作业正在运行(wait -n
在这种情况下立即退出),它将立即恢复。&
在同一个 bash 脚本中异步运行了一些命令 ( ),那么循环中的工作进程就会更少。 归档时间: |
|
查看次数: |
22646 次 |
最近记录: |