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 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了