Created
October 17, 2017 04:19
-
-
Save tolitius/7c8c070ade37a7d3ce093da5fbe34af3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"time" | |
) | |
func consume(ctx context.Context, partition int) { | |
for { | |
select { | |
case <-ctx.Done(): | |
fmt.Println("I am done") | |
break | |
default: | |
fmt.Printf("consuming from parition: %d\n", partition) | |
time.Sleep(1 * time.Second) | |
} | |
} | |
} | |
func main() { | |
ctx, cancel := context.WithCancel(context.Background()) | |
for i := 0; i < 5; i++ { | |
go consume(ctx, i) | |
} | |
time.Sleep(5 * time.Second) | |
cancel() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment