使用nickalie/go-queue 实现简单的任务队列,队列任务目前基于内存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package main
import (
"fmt"
"github.com/nickalie/go-queue"
"time"
)
func main() {
for i := 0; i < 5; i++ {
go consumer(i + 1)
}
producer()
}
func producer() {
i := 0
for {
i++
queue.Put("messages", fmt.Sprintf("message %d", i))
time.Sleep(time.Second)
}
}
func consumer(index int) {
for {
var message string
queue.Get("messages", &message)
fmt.Printf("Consumer %d got a message: %s\n", index, message)
time.Sleep(2 * time.Second)
}
}
项目地址: https://github.com/nickalie/go-queue
本文网址: https://golangnote.com/topic/185.html 转摘请注明来源