好吧
我的情况是这样的:自从我对 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/