dongzhi6087 2016-09-19 12:25
浏览 47

goroutine死锁:在从区块链读取并写入rethinkdb的应用程序中,

Okay, so

My situation is this: It's been three weeks and some-odd hours since I've become entranced by golang. I'm working on a blockchain dump tool for steem, and I'm probably going to give a touch of gjson to github.com/go-steem/rpc, the library I currently rely on. Now, with this said, this question is about the goroutines for my current blockchain reader. Here it is (sorry a tad on the beefy side, but you'll see the part that I want to pull back into the library, too):

    // Keep processing incoming blocks forever.
    fmt.Println("---> Entering the block processing loop")
    for {
        // Get current properties.
        props, err := Client.Database.GetDynamicGlobalProperties()
        if err != nil {
            fmt.Println(err)
        }

        // Process blocks.
        for I := uint32(1); I <= props.LastIrreversibleBlockNum; I++ {
            go getblock(I, Client, Rsession)
        }
        if err != nil {
            fmt.Println(err)
        }

    }

}

func getblock(I uint32, Client *rpc.Client, Rsession *r.Session) {
    block, err := Client.Database.GetBlock(I)
    fmt.Println(I)
    writeBlock(block, Rsession)
    if err != nil {
        fmt.Println(err)
    }
}

func writeBlock(block *d.Block, Rsession *r.Session) {
    //rethinkdb writes
    r.Table("transactions").
        Insert(block.Transactions).
        Exec(Rsession)
    r.Table("blocks").
        Insert(block).
        Exec(Rsession)
}

I just made a third edit to this, which was to call the function writeBlock from goroutine getBlock instead of the way I was doing things before. I'

  • 写回答

1条回答 默认 最新

  • dpafea04148 2016-09-19 13:25
    关注

    Okay, so that is now resolved, but this is going to spawn another question, unfortunatley.

    I've got the application working with the goroutine, however it hasn't increased performance any.

    The way that I got it to work was by not spawning a goroutine from a goroutine and instead calling a plain function, writeBlock from the goroutine "getblock":

        fmt.Println("---> Entering the block processing loop")
        for {
            // Get current properties.
            props, err := Client.Database.GetDynamicGlobalProperties()
            if err != nil {
                fmt.Println(err)
            }
    
            // Process blocks.
            for I := uint32(1); I <= props.LastIrreversibleBlockNum; I++ {
                go getblock(I, Client, Rsession)
            }
            if err != nil {
                fmt.Println(err)
            }
    
        }
    
    }
    
    func getblock(I uint32, Client *rpc.Client, Rsession *r.Session) {
        block, err := Client.Database.GetBlock(I)
        fmt.Println(I)
        writeBlock(block, Rsession)
        if err != nil {
            fmt.Println(err)
        }
    }
    
    func writeBlock(block *d.Block, Rsession *r.Session) {
        //rethinkdb writes
        r.Table("transactions").
            Insert(block.Transactions).
            Exec(Rsession)
        r.Table("blocks").
            Insert(block).
            Exec(Rsession)
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大