dpicx06888 2017-06-10 16:15
浏览 106
已采纳

Go例程的Golang订单输出

I have 16 go routines which return output , which is typically a struct.

struct output{
index int,
description string,
}

Now all these 16 go routines run in parallel, and the total expected output structs from all the go routines is expected to be a million. I have used the basic sorting of go lang it is very expensive to do that, could some one help me with the approach to take to sort the output based on the index and I need to write the "description" field on to a file based on the order of index.

For instance , if a go routine gives output as {2, "Hello"},{9,"Hey"},{4,"Hola"}, my output file should contain Hello Hola Hey

All these go routines run in parallel and I have no control on the order of execution , hence I am passing the index to finally order the output.

  • 写回答

1条回答 默认 最新

  • dsb12300 2017-06-10 20:39
    关注

    One thing to consider before getting into the answer is your example code will not compile. To define a type of struct in Go, you would need to change your syntax to

    type output struct {
        index       int
        description string
    }
    

    In terms of a potential solution to your problem - if you already reliably have unique index's as well as the expected count of the result set - you should not have to do any sorting at all. Instead synchronize the go routines over a channel and insert the output in an allocated slice at the respective index. You can then iterate over that slice to write the contents to a file. For example:

    ch := make(chan output) //each go routine will write to this channel
    wg := new(sync.WaitGroup) //wait group to sync all go routines
    
    //execute 16 goroutines 
    for i := 0; i < 16; i++ {
        wg.Add(1)
        go worker(ch, wg) //this is expecting each worker func to call wg.Done() when completing its portion of work
    }
    
    //create a "quit" channel that will be used to signal to the select statement below that your go routines are all done
    quit := make(chan bool)
    go func() {
        wg.Wait()
        quit <- true
    }()
    
    //initialize a slice with length and capacity to 1mil, the expected result size mentioned in your question
    sorted := make([]string, 1000000, 1000000)
    
    //use the for loop, select pattern to sync the results from your 16 go routines and insert them into the sorted slice
    for {
        select {
            case output := <-ch:
                //this is not robust - check notes below example
                sorted[output.index] = output.description
            case <-quit:
                //implement a function you could pass the sorted slice to that will write the results
                // Ex:  writeToFile(sorted)
                return
        }
    }
    

    A couple notes on this solution: it is dependent upon you knowing the size of the expected result set. If you do not know what the size of the result set is - in the select statement you will need to check if the index is read from ch exceeds the length of the sorted slice and allocate additional space before inserting our you program will crash as a result of an out of bounds error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测