proper use of sqlite3.BusyFunc using go?

huangapple go评论89阅读模式
英文:

proper use of sqlite3.BusyFunc using go?

问题

我正在尝试执行一些插入查询,每个goroutine执行一个查询(参见https://stackoverflow.com/questions/23268356/emulating-multiple-requests-to-sqlite-database-as-goroutine-causes-random-panic中的代码),但大部分查询都失败并显示错误:数据库文件被锁定。

我在http://godoc.org/code.google.com/p/go-sqlite/go1/sqlite3#BusyFunc中找到了以下内容:

> type BusyFunc func(count int) (retry bool)
BusyFunc是一个回调函数,在SQLite无法在表上获取锁时被调用。Count是此次锁定事件中回调已被调用的次数。如果函数返回false,则操作将被中止。否则,函数应该在返回true之前阻塞一段时间,然后让SQLite进行另一次锁定尝试。

我插入了以下代码:

sqlite3.BusyFunc(func(counted int) (bool) { 
    if counted > 10 { 
        return false 
    } else { 
        return true
    }
})

但它返回了sqlite3.BusyFunc(func literal) evaluated but not used。我漏掉了什么吗?

英文:

I am trying to execute some insert queries one query per goroutine (see code from https://stackoverflow.com/questions/23268356/emulating-multiple-requests-to-sqlite-database-as-goroutine-causes-random-panic) but most of failed with error: The database file is locked.

I found following from http://godoc.org/code.google.com/p/go-sqlite/go1/sqlite3#BusyFunc :

> type BusyFunc func(count int) (retry bool)
BusyFunc is a callback function invoked by SQLite when it is unable to acquire a lock on a table. Count is the number of times that the callback has been invoked for this locking event so far. If the function returns false, then the operation is aborted. Otherwise, the function should block for a while before returning true and letting SQLite make another locking attempt.

I inserted following code:

sqlite3.BusyFunc(func(counted int) (bool) { 
    if counted > 10 { 
        return false 
    } else { 
        return true
    }
})

but it returned sqlite3.BusyFunc(func literal) evaluated but not used. Am I missing something?

答案1

得分: 1

sqlite3.BusyFunc是一种类型。你正在将一个函数转换为该类型,结果你得到了一个该类型的函数。相反,你需要使用以下方式注册你的函数:

func (c *Conn) BusyFunc(f BusyFunc) (prev BusyFunc)

基本上,只需将"sqlite3"更改为连接句柄的名称,然后获取返回值即可。

英文:

sqlite3.BusyFunc is a type. What you're doing is converting a function into that type, as a result you get a function of that type. Instead, you have to register your function with:

func (c *Conn) BusyFunc(f BusyFunc) (prev BusyFunc)

Basically changing "sqlite3" for the name of the conn handle should do the job, just grab the return value.

huangapple
  • 本文由 发表于 2014年4月25日 03:06:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/23277678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定