I am leaning go, so this might be a stupid question.
I can't seem to figure out why one of my go routines is being blocked by another. My understanding (could be wrong) go routines run as independent light weight threads so they should not be blocking each other, unless I have messed up :)
I have pasted the code below and would appreciate any help / tips to figure this out.
package main
import "fmt"
import "time"
import "sync"
func worker( jobs <-chan int, job2 chan<- int) {
defer wg.Done()
for j := range jobs {
fmt.Println("finished job", j)
time.Sleep(time.Second/2)
if(j%3==0){
job2 <- j
}
}
close(job2)
fmt.Println("channel job2 closed")
}
func worker2(job2 <-chan int) {
defer wg.Done()
for i:= range job2 {
fmt.Println(i)
time.Sleep(time.Second*10)
}
}
var wg sync.WaitGroup
func main() {
wg.Add(2)
jobs := make(chan int)
job2 := make(chan int)
go func() {
for j := 1; j <= 10; j++ {
jobs <- j
}
close(jobs)
fmt.Println("channel jobs closed")
}()
go worker(jobs,job2)
go worker2(job2)
wg.Wait()
fmt.Println("exiting main")
}
I get the following output when I run this code
finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
6
finished job 7
finished job 8
finished job 9
9
finished job 10
channel jobs closed
channel job2 closed
exiting main
however I was expecting something like this?
finished job 1
finished job 2
finished job 3
finished job 4
3
finished job 5
finished job 6
finished job 7
finished job 8
finished job 9
finished job 10
channel jobs closed
6
9
channel job2 closed
exiting main
Your routines are sort-of blocking because the channels aren't buffered. A write/read on an unbuffered channel is a blocking operation. Therefore your routines by definition have to wait on eachother.
Essentially, your sleep of half a second is kind of irrelevant, because the second worker sleeps for 10 seconds. Those 10 seconds will block reads/writes to the second channel. Add a buffer to the channel to get around this.
Some other things I'd like to point out are:
time.Sleep(time.Second/2)
isn't going to work (well, it is, but dividing by 3 for example isn't). time.Sleep
expects a time.Duration
argument, which is an int64
. You need to pass something like time.Millisecond * 500
insteadimport "package"
, just use import ( "package1"\n"package2")
context.Context
and the select
construct. You can create a context.WithCancel
and in select listen for ctx.Done()
in all routines. Then you can just cancel all routines in one go without having to handle signals and pushing stuff onto a cancel channelI've changed a couple of things (mainly channel creation, and some minor code-cleanup), and created a playground example here