SHARE
TWEET

Untitled

a guest Oct 17th, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "context"
  5.     "fmt"
  6.     "time"
  7. )
  8.  
  9. func consume(ctx context.Context, partition int) {
  10.  
  11.     for {
  12.         select {
  13.         case <-ctx.Done():
  14.             fmt.Println("I am done")
  15.             break
  16.         default:
  17.             fmt.Printf("consuming from parition: %d\n", partition)
  18.             time.Sleep(1 * time.Second)
  19.         }
  20.     }
  21. }
  22.  
  23. func main() {
  24.     ctx, cancel := context.WithCancel(context.Background())
  25.  
  26.     for i := 0; i < 5; i++ {
  27.         go consume(ctx, i)
  28.     }
  29.  
  30.     time.Sleep(5 * time.Second)
  31.  
  32.     cancel()
  33. }
RAW Paste Data
Top