Sql Go等效于GCD串行调度队列
是否有与苹果的GCD串行调度队列相当的Go 到目前为止,我只找到了一个解决方案,即函数通道Sql Go等效于GCD串行调度队列,sql,go,queue,grand-central-dispatch,Sql,Go,Queue,Grand Central Dispatch,是否有与苹果的GCD串行调度队列相当的Go 到目前为止,我只找到了一个解决方案,即函数通道 work := make(chan func()) 我将有一个函数从这个通道接收并调用接收到的函数。这些功能必须按FIFO顺序执行 在围棋中有没有更好的方法或结构 这应该不会有什么不同,但我希望将SQL查询排队以在FIFO中运行 像这样的东西应该行得通,但是我不熟悉GCD是如何工作的,所以我可能有点不对劲 func main() { q := NewQueue(10) // the size
work := make(chan func())
我将有一个函数从这个通道接收并调用接收到的函数。这些功能必须按FIFO顺序执行
在围棋中有没有更好的方法或结构
这应该不会有什么不同,但我希望将SQL查询排队以在FIFO中运行 像这样的东西应该行得通,但是我不熟悉GCD是如何工作的,所以我可能有点不对劲
func main() {
q := NewQueue(10) // the size is mainly so it wouldn't block, you can play with that to your liking.
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
i := i
q <- func() { log.Println("i =", i); wg.Done() }
}
wg.Wait()
close(q)
}
func NewQueue(size int) (q chan func()) {
q = make(chan func(), size)
go func() {
for fn := range q {
fn()
}
}()
return
}
func main(){
q:=NewQueue(10)//大小主要是为了防止阻塞,您可以随意使用。
var wg sync.WaitGroup
对于i:=0;i<10;i++{
工作组.添加(1)
i:=i
像这样的东西应该可以用,但是我不熟悉GCD是如何工作的,所以我可能会有点不对劲
func main() {
q := NewQueue(10) // the size is mainly so it wouldn't block, you can play with that to your liking.
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
i := i
q <- func() { log.Println("i =", i); wg.Done() }
}
wg.Wait()
close(q)
}
func NewQueue(size int) (q chan func()) {
q = make(chan func(), size)
go func() {
for fn := range q {
fn()
}
}()
return
}
func main(){
q:=NewQueue(10)//大小主要是为了防止阻塞,您可以随意使用。
var wg sync.WaitGroup
对于i:=0;i<10;i++{
工作组.添加(1)
i:=i
其中一个,很接近,但不太接近
我最终在Go中实现了串行调度队列
它基本上是一个go例程,在类型为func()
的通道上阻塞,并按顺序运行传递的函数
实施:
//Package serialqueue provides a serial queue for functions.
//Queue items are processed in First In First Out (FIFO) order.
package serialqueue
//New returns a new serial queue.
//Enqueue items like queueObj <- func() {doWork(data)}
func New() chan func() {
//create channel of type function
var queue = make(chan func())
//spawn go routine to read and run functions in the channel
go func() {
for true {
nextFunction := <-queue
nextFunction()
}
}()
return queue
}
//Package serialqueue为函数提供串行队列。
//队列项目按先进先出(FIFO)顺序处理。
包串行队列
//New返回一个新的串行队列。
//将queueObj之类的项目排在队列中,其中一个很接近,但不完全相同
我最终在Go中实现了串行调度队列
它基本上是一个go例程,在类型为func()
的通道上阻塞,并按顺序运行传递的函数
实施:
//Package serialqueue provides a serial queue for functions.
//Queue items are processed in First In First Out (FIFO) order.
package serialqueue
//New returns a new serial queue.
//Enqueue items like queueObj <- func() {doWork(data)}
func New() chan func() {
//create channel of type function
var queue = make(chan func())
//spawn go routine to read and run functions in the channel
go func() {
for true {
nextFunction := <-queue
nextFunction()
}
}()
return queue
}
//Package serialqueue为函数提供串行队列。
//队列项目按先进先出(FIFO)顺序处理。
包串行队列
//New返回一个新的串行队列。
//将项目(如queueObj)排队