gocelery 测试例子(windows会有报错,建议linux执行)
Posted 招财猫2021
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gocelery 测试例子(windows会有报错,建议linux执行)相关的知识,希望对你有一定的参考价值。
celery
server
package main
import (
"fmt"
"github.com/gocelery/gocelery"
"github.com/gomodule/redigo/redis"
"time"
)
func main() {
// create redis connection pool
redisPool := &redis.Pool{
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL("redis://127.0.0.1:6379")
if err != nil {
return nil, err
}
return c, err
},
}
// initialize celery client
cli, _ := gocelery.NewCeleryClient(
gocelery.NewRedisBroker(redisPool),
&gocelery.RedisCeleryBackend{Pool: redisPool},
500, // number of workers
)
// task
add := func(a, b int) int {
fmt.Println(a + b)
return a + b
}
// register task
cli.Register("worker.add", add)
// start workers (non-blocking call)
cli.StartWorker()
// wait for client request
time.Sleep(1000000 * time.Hour)
//// stop workers gracefully (blocking call)
cli.StopWorker()
}
client
package main
import (
"github.com/gocelery/gocelery"
"github.com/gomodule/redigo/redis"
"math/rand"
)
func main() {
redisPool1 := &redis.Pool{
Dial: func() (redis.Conn, error) {
c, err := redis.DialURL("redis://127.0.0.1:6379")
if err != nil {
return nil, err
}
return c, err
},
}
// initialize celery client
cli, _ := gocelery.NewCeleryClient(
gocelery.NewRedisBroker(redisPool1),
&gocelery.RedisCeleryBackend{Pool: redisPool1},
1,
)
// prepare arguments
taskName := "worker.add"
argA := rand.Intn(10)
argB := rand.Intn(10)
// run task
_, err := cli.Delay(taskName, argA, argB)
if err != nil {
panic(err)
}
//// get results from backend with timeout
//res, err := asyncResult.Get(10 * time.Second)
//if err != nil {
// panic(err)
//}
//
//fmt.Printf("result: %+v of type %+v", res, reflect.TypeOf(res))
}
以上是关于gocelery 测试例子(windows会有报错,建议linux执行)的主要内容,如果未能解决你的问题,请参考以下文章