doushenyi9104 2019-05-04 23:58
浏览 34

并发尝试失败

I'm having difficulty getting go concurrency to work correctly. I'm working with data loaded from an XML Data Source. Once I load the data into memory, i loop through the XML elements and perform an operation. The code prior to the concurrency addition has been tested and functional, and I don't believe it has any influence on the concurrency addition. I have 2 failed attempts at concurrency implementations, both with different outputs. I used locking because i dont want to enter a race condition.

For this implementation, it never enters the goroutine.

    var mu sync.Mutex

    // length is 197K
    for i:=0;i<len(listings.Listings);i++{
      go func(){
         mu.Lock()

         // code execution (tested prior to adding concurrency and locking)

         mu.Unlock()     
      }()
    }

For this implementation using waitGroups, a runtime out of memory occurs

    var mu sync.Mutex
    var wg sync.WaitGroup

    // length is 197K
    for i:=0;i<len(listings.Listings);i++{
       wg.Add(1)
       go func(){
           mu.Lock()

           // code execution (tested prior to adding concurrency and locking and wait group)      

           wg.Done()
           mu.Unlock()   
       }()
    }
    wg.Wait()

I'm not really sure what's going on and could use some assistance.

  • 写回答

1条回答 默认 最新

  • dongshi4078 2019-05-05 09:49
    关注
    1. You don't need Mutex here if you want to make it concurrent
    2. 197K goroitines are a lot, try lower amount of goroutines. You can accomplish it by creating N goroutines, when each of them is listening to the same channel.

    https://play.golang.org/p/s4e0YyHdyPq

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    type Listing struct{}
    
    func main() {
        var (
            wg          sync.WaitGroup
            concurrency = 100
        )
    
        c := make(chan Listing)
    
        wg.Add(concurrency)
        for i := 0; i < concurrency; i++ {
            go func(ci <-chan Listing) {
                for l := range ci {
                    // code, l is a single Listing
                    fmt.Printf("%v", l)
                }
                wg.Done()
            }(c)
        }
    
        // replace with your var
        listings := []Listing{Listing{}}
        for _, l := range listings {
            c <- l
        }
    
        close(c)
        wg.Wait()
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100