added the example for worker
This commit is contained in:
parent
50411ee1a9
commit
a6fab88b22
18
example/Makefile
Normal file
18
example/Makefile
Normal file
@ -0,0 +1,18 @@
|
||||
# Copyright 2009 The Go Authors. All rights reserved.
|
||||
# Use of this source code is governed by a BSD-style
|
||||
# license that can be found in the LICENSE file.
|
||||
|
||||
include $(GOROOT)/src/Make.inc
|
||||
|
||||
TARG=gearman
|
||||
|
||||
# GOFILES=\
|
||||
# worker.go\
|
||||
|
||||
CLEANFILES+= worker
|
||||
|
||||
include $(GOROOT)/src/Make.pkg
|
||||
|
||||
%: %.go
|
||||
$(GC) $*.go
|
||||
$(LD) -o $@ $*.$O
|
45
example/worker.go
Normal file
45
example/worker.go
Normal file
@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gearman"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ToUpper(job *gearman.Job) ([]byte, os.Error) {
|
||||
data := []byte(strings.ToUpper(string(job.Data)))
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
worker := gearman.NewWorker()
|
||||
worker.AddServer("127.0.0.1:4730")
|
||||
worker.AddFunction("ToUpper", ToUpper, 0)
|
||||
worker.AddFunction("ToUpperTimeOut5", ToUpper, 5)
|
||||
|
||||
go func() {
|
||||
log.Println("start worker")
|
||||
for {
|
||||
print("cmd: ")
|
||||
var str string
|
||||
fmt.Scan(&str)
|
||||
switch str {
|
||||
case "echo":
|
||||
worker.Echo([]byte("Hello world!"))
|
||||
job := <-worker.Queue
|
||||
log.Println(string(job.Data))
|
||||
case "quit":
|
||||
worker.Close()
|
||||
return
|
||||
case "result":
|
||||
job := <-worker.Queue
|
||||
log.Println(string(job.Data))
|
||||
default:
|
||||
log.Println("Unknown command")
|
||||
}
|
||||
}
|
||||
}()
|
||||
worker.Work()
|
||||
}
|
Loading…
Reference in New Issue
Block a user