I have a script which is selecting some data from a database and sending this to a channel to be processed by multiple goroutines, with the results then sent back to the main thread to be updated on the database.
However, it is hanging (probably blocking) on sending the data to first channel.
The channels are created globally with:
var chin = make(chan in)
var chout = make(chan out)
in
and out
are both structs
First the goroutines are started:
for i:=0; i<5; i++ {
go worker()
}
The code to load the channels is then:
if verbose {
fmt.Println(`Getting nextbatch2 and sending to workers`)
}
rows, err = nextbatch2.Query()
if err != nil {
panic(err)
}
var numtodo int
for rows.Next() {
err = rows.Scan(&id, &data)
if err != nil {
rows.Close()
panic(err)
}
// Start
var vin in
vin.id = id
vin.data = data
chin <- vin
numtodo++
}
rows.Close()
Then immediately after this:
if verbose {
fmt.Println(`Processing out channel from workers`)
}
for res := range chout {
update5.Exec(res.data, res.id)
if numtodo--; numtodo == 0 {
break
}
}
And in the background are multiple worker()
goroutines running:
func worker() {
for res := range chin {
var v out
v.id = res.id
v.data = process(res.data)
chout <- v
}
}
This code hangs after printing Getting nextbatch2 and sending to workers
. It never gets to Processing out channel from workers
. So it's hanging somewhere inside the rows.Next()
loop, for which I can't figure out the reason as the chin
channel should be non-blocking - even if the worker()
goroutines were not processing it should still at least finish that loop.
Any ideas?
EDIT:
By adding on fmt.Println(" on", numtodo)
at the end of the rows.Next()
loop I can see that it blocks after 5, which I don't understand as it should be non-blocking, right?
EDIT 2:
By changing the channels to make(chan in/out, 100)
it now will block after 105.