示例#1
0
func main() {

	var (
		w1 = NewWorker("Worker 1")
		w2 = NewWorker("Worker 2")
		w3 = NewWorker("Worker 3")

		g = killable.New(w1, w2, w3)
	)

	killable.Defer(g, func() {
		fmt.Println("All workers are dead")
	})

	w1.Start()
	w2.Start()
	w3.Start()

	go func() {
		time.Sleep(2 * time.Second)
		fmt.Println("Killing the worker group")
		g.Kill(fmt.Errorf("time to die!"))
	}()

	if err := g.Err(); err != nil {
		log.Println(err)
	}

	// wait for the deferreds to run
	<-g.Dead()
	time.Sleep(time.Second)

}
示例#2
0
func (w *Worker) Start() {

	killable.Defer(w, func() {
		fmt.Printf("worker: %s: all processes complete, cleaning up\n", w.name)
	})

	killable.Go(w, func() error {
		w.startProducer()
		return w.consumer()
	})
}