并行过程:在bash脚本中将输出追加到数组 [英] Parallel processes: appending outputs to an array in a bash script

查看:125
本文介绍了并行过程:在bash脚本中将输出追加到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个for循环,其中调用了函数task.每次对该函数的调用都会返回一个附加到数组的字符串.我想并行处理此for循环.我尝试使用&,但它似乎不起作用.

I have a for loop in which a function task is called. Each call to the function returns a string that is appended to an array. I would like to parallelise this for loop. I tried using & but it does not seem to work.

这是未并行化的代码.

task (){ sleep 1;echo "hello $1"; }
arr=()

for i in {1..3}; do
    arr+=("$(task $i)")
done

for i in "${arr[@]}"; do
    echo "$i x";
done

输出为:

hello 1 x
hello 2 x
hello 3 x

太好了!但是现在,当我尝试将其与

Great! But now, when I try to parallelise it with

[...]
for i in {1..3}; do
    arr+=("$(task $i)")&
done
wait
[...]

输出为空.

推荐答案

GNU并行擅长并行处理:-)

task (){ sleep 1;echo "hello $1"; }

# Make "task" known to sub shells
export -f task

# Do tasks in parallel
parallel -k task ::: {1..3}

示例输出

hello 1
hello 2
hello 3

我建议您这样做-但查尔斯(Charles)友善地指出,这是一个已知的bash陷阱:

I am suggesting you do - but Charles kindly points out that this is a known bash pitfall:

array=( $(parallel -k task ::: {1..3}) )

查尔斯建议的解决方案是:

Charles' suggested solution is:

IFS=$'\n' read -r -d '' -a array < <(parallel -k task ::: 1 2 3 && printf '\0')

这篇关于并行过程:在bash脚本中将输出追加到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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