将go例程的多个结果聚合到单个数组中
我有下面的函数,它派生出给定数量的go例程将go例程的多个结果聚合到单个数组中,go,channels,Go,Channels,我有下面的函数,它派生出给定数量的go例程 func (r *Runner) Execute() { var wg sync.WaitGroup wg.Add(len(r.pipelines)) for _, p := range r.pipelines { go executePipeline(p, &wg) } wg.Wait() errs := ....//contains list of errors repor
func (r *Runner) Execute() {
var wg sync.WaitGroup
wg.Add(len(r.pipelines))
for _, p := range r.pipelines {
go executePipeline(p, &wg)
}
wg.Wait()
errs := ....//contains list of errors reported by any/all go routines
}
我在想可能有办法使用通道,但我似乎不太明白。一种方法是使用互斥体,如果您可能会出现ExecutePiline retuen错误:
// ...
for _, p := range r.pipelines {
go func(p pipelineType) {
if err := executePipeline(p, &wg); err != nil {
mu.Lock()
errs = append(errs, err)
mu.UnLock()
}
}(p)
}
要使用通道,您可以使用单独的goroutine列表查找错误:
errCh := make(chan error)
go func() {
for e := range errCh {
errs = append(errs, e)
}
}
在Execute
功能中,进行以下更改:
// ...
wg.Add(len(r.pipelines))
for _, p := range r.pipelines {
go func(p pipelineType) {
if err := executePipeline(p, &wg); err != nil {
errCh <- err
}
}(p)
}
wg.Wait()
close(errCh)
/。。。
工作组增补(长(右)
对于u,p:=范围r{
go func(p管道类型){
如果错误:=ExecutePiline(p,&wg);错误!=nil{
errCh一种方法是使用互斥体,如果您可以使ExecutePiline返回错误:
// ...
for _, p := range r.pipelines {
go func(p pipelineType) {
if err := executePipeline(p, &wg); err != nil {
mu.Lock()
errs = append(errs, err)
mu.UnLock()
}
}(p)
}
要使用通道,您可以使用单独的goroutine列表查找错误:
errCh := make(chan error)
go func() {
for e := range errCh {
errs = append(errs, e)
}
}
在Execute
功能中,进行以下更改:
// ...
wg.Add(len(r.pipelines))
for _, p := range r.pipelines {
go func(p pipelineType) {
if err := executePipeline(p, &wg); err != nil {
errCh <- err
}
}(p)
}
wg.Wait()
close(errCh)
/。。。
工作组增补(长(右)
对于u,p:=范围r{
go func(p管道类型){
如果错误:=ExecutePiline(p,&wg);错误!=nil{
errCh您可以使用@Kaveh Shahbazian建议的频道:
func (r *Runner) Execute() {
pipelineChan := makePipeline(r.pipelines)
for cnt := 0; cnt < len(r.pipelines); cnt++{
//recieve from channel
p := <- pipelineChan
//do something with the result
}
}
func makePipeline(pipelines []pipelineType) <-chan pipelineType{
pipelineChan := make(chan pipelineType)
go func(){
for _, p := range pipelines {
go func(p pipelineType){
pipelineChan <- executePipeline(p)
}(p)
}
}()
return pipelineChan
}
func(r*Runner)Execute(){
pipelineChan:=makePipeline(r.pipelines)
对于cnt:=0;cnt p:=您可以使用@Kaveh Shahbazian建议的频道:
func (r *Runner) Execute() {
pipelineChan := makePipeline(r.pipelines)
for cnt := 0; cnt < len(r.pipelines); cnt++{
//recieve from channel
p := <- pipelineChan
//do something with the result
}
}
func makePipeline(pipelines []pipelineType) <-chan pipelineType{
pipelineChan := make(chan pipelineType)
go func(){
for _, p := range pipelines {
go func(p pipelineType){
pipelineChan <- executePipeline(p)
}(p)
}
}()
return pipelineChan
}
func(r*Runner)Execute(){
pipelineChan:=makePipeline(r.pipelines)
对于cnt:=0;cnt p:=创建一个大小为len(r.pipelines)
的切片,并让每个工作者写入其相应的索引。有一个模式用于将某些通道的结果合并到一个命名的扇入中。您可以在该模式中使用该方法(不一定是模式本身)。创建一个大小为len(r.pipelines)的切片
并让每个工作者写入其相应的索引。有一种模式用于将某些通道的结果合并到一个名为扇入的通道中。您可以在该模式中使用该方法(不一定是模式本身)。