Gearman-Go 是 Gearman-API 的 Go 语言封装版本,示例代码:
Worker:
// Limit number of concurrent jobs execution. // Use worker.Unlimited (0) if you want no limitation. w := worker.New(worker.OneByOne) w.ErrHandler = func(e error) { log.Println(e) } w.AddServer("127.0.0.1:4730") // Use worker.Unlimited (0) if you want no timeout w.AddFunc("ToUpper", ToUpper, worker.Unlimited) // This will give a timeout of 5 seconds w.AddFunc("ToUpperTimeOut5", ToUpper, 5) if err := w.Ready(); err != nil { log.Fatal(err) return } go w.Work()
Client:
// ... c, err := client.New("tcp4", "127.0.0.1:4730") // ... error handling defer c.Close() c.ErrorHandler = func(e error) { log.Println(e) } echo := []byte("Hello\x00 world") echomsg, err := c.Echo(echo) // ... error handling log.Println(string(echomsg)) jobHandler := func(resp *client.Response) { log.Printf("%s", resp.Data) } handle, err := c.Do("ToUpper", echo, client.JobNormal, jobHandler) // ...