hi John
I am expecting work2 should not include in
headers = <-messgesResponse
so output of below should be Len = 1 and should print only work 1 only and
not 2 ( as work 2 is timed out )
fmt.Printf("len > %s\n", len(headers))
for i:=0;i<len(headers);i++ {
fmt.Printf("Name > %s\n", headers[i].Name)
}
basically workgroup timeout doesnt close the timeout works
select {
case <-time.After(2 * time.Second): //This works
but dont stop the work2
return
}
Do I need to explicitly close them in
select {
case <-time.After(2 * time.Second):
close(cs)
return
}
Thanks for the help
Rgds,
Abhi
On Sunday, January 8, 2017 at 5:40:01 PM UTC+5:30, John Souvestre wrote:
>
> Hi Abhi.
>
>
>
> I believe that the wait group timeout (in monitorWorker) was set to 2
> seconds in the code you posted.
>
>
>
> I put a debugging print in the timeout case, and it is taking place.
>
>
>
> What were you expecting to see?
>
>
>
> John
>
> John Souvestre - New Orleans LA
>
>
>
> *From:* [email protected] <javascript:> [mailto:
> [email protected] <javascript:>] *On Behalf Of *
> [email protected] <javascript:>
> *Sent:* 2017 January 08, Sun 05:48
> *To:* golang-nuts
> *Subject:* Re: [go-nuts] Multiple goroutine timeout for waitgroup not
> working
>
>
>
> hi John
>
>
>
> Thanks for the reply
>
>
>
> sorry
>
>
>
> I mean Work2 takes => 3 seconds
>
> work1 takes => 1 seconds
>
>
>
> Wait group timeout is => 1 seconds
>
>
>
> It is expected that only Work1 should get done and Work 2 should get
> timeout which is not happening
>
>
>
> Waitgroup waits for both work.. program doing something wrong
>
>
>
> func Work2(message chan *TestStruct, wg *sync.WaitGroup, tokenNumber
> string) {
>
>
>
> defer wg.Done()
>
>
>
> v1 := new(TestStruct)
>
> v1.Name = tokenNumber
>
> time.Sleep(3 * time.Second)
>
>
>
> message <- v1
>
>
>
> fmt.Printf("finished %s\n", tokenNumber)
>
> }
>
>
>
> Thanks,
>
>
>
> Abhi
>
>
> On Sunday, January 8, 2017 at 5:09:56 PM UTC+5:30, John Souvestre wrote:
>
> What do you see when you run it? I see:
>
>
>
> finished 1
>
> finished 2
>
> len > %!s(int=2) ç Using a string format for an int
>
> Name > 1
>
> Name > 2
>
>
>
> Ø Work 2 - have 3 second
>
>
>
> I’m not sure exactly what you are trying to do, but I suspect that
> changing “messges” to hold 2 items might make it work.
>
>
>
> messges := make(chan *TestStruct, 2)
>
>
>
> I’m guessing, but I suspect that you didn’t realize that if Work1 runs
> first, then Work2 will block since the channel is full (or until you close
> it).
>
>
>
> John
>
> John Souvestre - New Orleans LA
>
>
>
> *From:* [email protected] [mailto:[email protected]] *On
> Behalf Of *[email protected]
> *Sent:* 2017 January 08, Sun 01:23
> *To:* golang-nuts
> *Subject:* [go-nuts] Multiple goroutine timeout for waitgroup not working
>
>
>
>
>
> Can you please help to correct below program where timeout not seems
> working
>
>
>
> Work 1 - have 1 second
>
> Work 2 - have 3 second
>
> Total Timeout - 2 sec
>
>
>
> program wait for entire 3 seconds and return both values rather than value
> from Work 1
>
>
>
> Program >
>
>
>
> package main
>
>
>
> import (
>
> "fmt"
>
> "time"
>
> "sync"
>
> )
>
>
>
>
>
> type TestStruct struct {
>
> Name string
>
> }
>
>
>
> func main() {
>
>
>
> wg := &sync.WaitGroup{}
>
> messges := make(chan *TestStruct, 1)
>
>
>
> wg.Add(1)
>
> go Work1(messges, wg, "1")
>
> wg.Add(1)
>
> go Work2(messges, wg, "2")
>
>
>
> monitorWorker(wg, messges)
>
>
>
> messgesResponse := make(chan []*TestStruct)
>
> go GetWorkerValues(messges, messgesResponse)
>
>
>
> headers := make([]*TestStruct, 0)
>
> headers = <-messgesResponse
>
>
>
> fmt.Printf("len > %s\n", len(headers))
>
>
>
> for i:=0;i<len(headers);i++ {
>
> fmt.Printf("Name > %s\n", headers[i].Name)
>
> }
>
> }
>
>
>
>
>
>
>
> func monitorWorker(wg *sync.WaitGroup, cs chan *TestStruct) {
>
> go func() {
>
> defer close(cs)
>
> wg.Wait()
>
> }()
>
>
>
> select {
>
> case <-time.After(2 * time.Second):
>
> return
>
> }
>
>
>
> }
>
> func Work1(message chan *TestStruct, wg *sync.WaitGroup, tokenNumber
> string) {
>
>
>
> defer wg.Done()
>
>
>
> v1 := new(TestStruct)
>
> v1.Name = tokenNumber
>
> time.Sleep(1 * time.Second)
>
>
>
> message <- v1
>
> fmt.Printf("finished %s\n", tokenNumber)
>
> }
>
>
>
> func Work2(message chan *TestStruct, wg *sync.WaitGroup, tokenNumber
> string) {
>
>
>
> defer wg.Done()
>
>
>
> v1 := new(TestStruct)
>
> v1.Name = tokenNumber
>
> time.Sleep(1 * time.Second)
>
>
>
> message <- v1
>
>
>
> fmt.Printf("finished %s\n", tokenNumber)
>
> }
>
>
>
>
>
> func GetWorkerValues(cs <-chan *TestStruct, response chan<- []*TestStruct)
> {
>
> var val []*TestStruct
>
>
>
> for header := range cs {
>
> val = append(val, header)
>
> }
>
>
>
> response <- val
>
> }
>
>
>
>
>
> Thanks,
>
>
>
> Abhi
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.