goroutine 死锁 : In an app that reads from a blockchain and writes to rethinkdb, 有

标签 go deadlock channel goroutine blockchain

好吧

我的情况是这样的:自从我对 golang 着迷以来已经过去了三个星期零几个小时。我正在为 steem 开发一个区 block 链转储工具,我可能会向 github.com/go-steem/rpc 提供一些 gjson,这是我目前依赖的库。现在,话虽如此,这个问题是关于我当前的区 block 链读者的 goroutines 的。在这里(对不起,有点过分,但你会看到我也想拉回图书馆的部分):

    // 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)
}

我刚刚对此进行了第三次编辑,即从 goroutine getBlock 调用函数 writeBlock,而不是我以前做事的方式。我'

最佳答案

好的,现在已经解决了,但是不幸的是,这会产生另一个问题。

我已经让应用程序与 goroutine 一起工作,但是它并没有提高性能。

我让它工作的方法是不从 goroutine 生成 goroutine,而是调用一个普通函数,从 goroutine“getblock”中调用 writeBlock:

    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)
}

关于goroutine 死锁 : In an app that reads from a blockchain and writes to rethinkdb, 有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39573137/

相关文章:

Go 语言/CGO : Problems calling Mach API host_statistics() from Go

java - 两个线程是否可能因单个对象实例而面临死锁?

Golang 缓冲 channel 在发送之前接收数据

go - 使用 channel 或 sync.Cond 等待条件

go-swagger 不验证 POST 请求中的 Body

go - JSON1 支持 go-sqlite3 使用 gorm

go - 如何在Golang中通过引用传递具有值类型接口(interface)的 map slice

java - 线程转储分析工具/方法

java - 竞争文件访问是否有可能导致 Java 中的死锁?

go - 无法获取工作 channel 图