Re: [go-nuts] Multiple Goroutine timeout

2017-10-25 Thread desaiabhijit
Thanks for the reply

How to gather all the values into CH and return to print

Thanks 

On Wednesday, October 25, 2017 at 7:41:34 PM UTC+5:30, M. Shulhan wrote:
>
> On Wed, 25 Oct 2017 06:30:09 -0700 (PDT) 
> desaia...@gmail.com  wrote: 
>
> > I am expecting to show url1 (2 sec ), url2 (4 sec ) but not url3( 6 
> > sec) where timeout is 5 sec 
> > 
> > but program showing only url1 value 
> > 
> > Please help 
> > 
> > https://play.golang.org/p/aMOoSEHjmZ 
> > 
> > Thanks in advance 
> > 
> > Rgds, 
> > 
> > Abhi 
> > 
> > 
> > package main 
> > 
> > import "fmt" 
> > import "time" 
> > 
> > func InvokeUrl1(u string, val chan<- string) { 
> > time.Sleep(2 * time.Second) 
> > val <- "Url1 Value" 
> > } 
> > func InvokeUrl2(u string, val chan<- string) { 
> > time.Sleep(4 * time.Second) 
> > val <- "Url2 Value" 
> > } 
> > func InvokeUrl3(u string, val chan<- string) { 
> > time.Sleep(6 * time.Second) 
> > val <- "Url3 Value" 
> > } 
> > 
> > func GetUrlValues(urls []string) { 
> > 
> > ch := make(chan string, 1) 
> > for _, url := range urls { 
> > go func(u string) { 
> > val := make(chan string) 
> > if u == "url1" { 
> >   go InvokeUrl1(u, val) 
> > } else if u == "url2" { 
> >   go InvokeUrl2(u, val) 
> > } else if u == "url3" { 
> >   go InvokeUrl3(u, val) 
> > } 
> > 
> > select { 
> > case ret := <-val: 
> > ch <- ret 
> > case <-time.After(5 * time.Second): 
> > ch <- "nil" 
> > } 
> > }(url) 
> > } 
> > fmt.Println(<-ch) 
>
> You only print once, it suppose to be inside the loop or wrapped with 
> select while <-ch != "nil". 
>
> > } 
> > func main() { 
> >   GetUrlValues([]string{"url1", "url2", "url3"}) 
> > } 
> > 
>
>
>
> -- 
> { "name":"Mhd Sulhan", "phone":"+628567826625", "site":"kilabit.info" } 
>

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Multiple Goroutine timeout

2017-10-25 Thread Shulhan
On Wed, 25 Oct 2017 06:30:09 -0700 (PDT)
desaiabhi...@gmail.com wrote:

> I am expecting to show url1 (2 sec ), url2 (4 sec ) but not url3( 6
> sec) where timeout is 5 sec
> 
> but program showing only url1 value
> 
> Please help
> 
> https://play.golang.org/p/aMOoSEHjmZ
> 
> Thanks in advance
> 
> Rgds,
> 
> Abhi
> 
> 
> package main
> 
> import "fmt"
> import "time"
> 
> func InvokeUrl1(u string, val chan<- string) {
> time.Sleep(2 * time.Second)
> val <- "Url1 Value"
> }
> func InvokeUrl2(u string, val chan<- string) {
> time.Sleep(4 * time.Second)
> val <- "Url2 Value"
> }
> func InvokeUrl3(u string, val chan<- string) {
> time.Sleep(6 * time.Second)
> val <- "Url3 Value"
> }
> 
> func GetUrlValues(urls []string) {
> 
> ch := make(chan string, 1)
> for _, url := range urls {
> go func(u string) {
> val := make(chan string)
> if u == "url1" {
>   go InvokeUrl1(u, val)
> } else if u == "url2" {
>   go InvokeUrl2(u, val)
> } else if u == "url3" {
>   go InvokeUrl3(u, val)
> }
> 
> select {
> case ret := <-val:
> ch <- ret
> case <-time.After(5 * time.Second):
> ch <- "nil"
> }
> }(url)
> }
> fmt.Println(<-ch)

You only print once, it suppose to be inside the loop or wrapped with
select while <-ch != "nil".

> }
> func main() {
>   GetUrlValues([]string{"url1", "url2", "url3"})
> }
> 



-- 
{ "name":"Mhd Sulhan", "phone":"+628567826625", "site":"kilabit.info" }

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Multiple Goroutine timeout

2017-10-25 Thread Ian Lance Taylor
As far as I can see your GetUrlValues function only ever prints one string.

Ian

On Wed, Oct 25, 2017 at 6:30 AM,   wrote:
> I am expecting to show url1 (2 sec ), url2 (4 sec ) but not url3( 6 sec)
> where timeout is 5 sec
>
> but program showing only url1 value
>
> Please help
>
> https://play.golang.org/p/aMOoSEHjmZ
>
> Thanks in advance
>
> Rgds,
>
> Abhi
>
>
> package main
>
> import "fmt"
> import "time"
>
> func InvokeUrl1(u string, val chan<- string) {
> time.Sleep(2 * time.Second)
> val <- "Url1 Value"
> }
> func InvokeUrl2(u string, val chan<- string) {
> time.Sleep(4 * time.Second)
> val <- "Url2 Value"
> }
> func InvokeUrl3(u string, val chan<- string) {
> time.Sleep(6 * time.Second)
> val <- "Url3 Value"
> }
>
> func GetUrlValues(urls []string) {
>
> ch := make(chan string, 1)
> for _, url := range urls {
> go func(u string) {
> val := make(chan string)
> if u == "url1" {
>   go InvokeUrl1(u, val)
> } else if u == "url2" {
>   go InvokeUrl2(u, val)
> } else if u == "url3" {
>   go InvokeUrl3(u, val)
> }
>
> select {
> case ret := <-val:
> ch <- ret
> case <-time.After(5 * time.Second):
> ch <- "nil"
> }
> }(url)
> }
> fmt.Println(<-ch)
> }
> func main() {
>   GetUrlValues([]string{"url1", "url2", "url3"})
> }
>
> --
> 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 golang-nuts+unsubscr...@googlegroups.com.
> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Multiple Goroutine timeout

2017-10-25 Thread desaiabhijit
I am expecting to show url1 (2 sec ), url2 (4 sec ) but not url3( 6 sec) 
where timeout is 5 sec

but program showing only url1 value

Please help

https://play.golang.org/p/aMOoSEHjmZ

Thanks in advance

Rgds,

Abhi


package main

import "fmt"
import "time"

func InvokeUrl1(u string, val chan<- string) {
time.Sleep(2 * time.Second)
val <- "Url1 Value"
}
func InvokeUrl2(u string, val chan<- string) {
time.Sleep(4 * time.Second)
val <- "Url2 Value"
}
func InvokeUrl3(u string, val chan<- string) {
time.Sleep(6 * time.Second)
val <- "Url3 Value"
}

func GetUrlValues(urls []string) {

ch := make(chan string, 1)
for _, url := range urls {
go func(u string) {
val := make(chan string)
if u == "url1" {
  go InvokeUrl1(u, val)
} else if u == "url2" {
  go InvokeUrl2(u, val)
} else if u == "url3" {
  go InvokeUrl3(u, val)
}

select {
case ret := <-val:
ch <- ret
case <-time.After(5 * time.Second):
ch <- "nil"
}
}(url)
}
fmt.Println(<-ch)
}
func main() {
  GetUrlValues([]string{"url1", "url2", "url3"})
}

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread Sairam Kunala
The ran the code on play, seems to come out within 2 seconds.

https://play.golang.org/p/yZIAvXI8IX

On Sun, Jan 8, 2017 at 12:53 PM,  wrote:

>
> 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 := {}
> 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 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 golang-nuts+unsubscr...@googlegroups.com.
> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
Hi Abhi.

 

An afterthought:  For the “done” case, add a check to clear out any “cs” values 
which might come in just before the “done” signal.  You won’t need to worry 
about this in the “timeout” case.

 

John

John Souvestre - New Orleans LA

 

From: John Souvestre [mailto:j...@souvestre.com] 
Sent: 2017 January 08, Sun 07:27
To: 'golang-nuts'
Subject: RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

Hi Abhi.

 

I’m thinking something like this might do the job.  Call monitorWorker(wg, 
done) as a goroutine and change it to do just the wg.Wait then send a signal on 
the “done” channel.

 

Next, start a goroutine which signals on a “timeout” channel if the timeout is 
exceeded.

 

Finally, change GetWorkerValues to be an endless loop containing a select with 
three cases: “<-cs” (add new value to array), “<-done” (break), and “<-timeout” 
(break).  After the loop, send the array.

 

John

John Souvestre - New Orleans LA

 

From: desaiabhi...@gmail.com [mailto:desaiabhi...@gmail.com] 
Sent: 2017 January 08, Sun 07:10
To: John Souvestre
Cc: golang-nuts
Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

Hi John

 

Can you please help me to apply correct logic to achieve it

 

Haven't found any solution on the net. All articles are talking about only 1 
routine timeout and not multiple

 

Thank you very much

 

Rgds

 

Abhi


On Jan 8, 2017, at 6:36 PM, John Souvestre <j...@souvestre.com> wrote:

Hi Abhi.

 

I believe that your logic is faulty.  The timeout does take place – but it 
doesn’t really do what you wanted, I think.

 

GetWorkerValues isn’t going to send it’s info because the “range cs” can’t 
finish until the channel is closed.  The channel is not closed until both 
workers are finished – timeout or not, thus it always contains the info for 
both workers.

 

John

John Souvestre - New Orleans LA

 

From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
Sent: 2017 January 08, Sun 06:24
To: golang-nuts
Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

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: golan...@googlegroups.com   
[mailto:golan...@googlegroups.com  ] On Behalf Of 
desaia...@gmail.com  
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

RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
Hi Abhi.

 

I’m thinking something like this might do the job.  Call monitorWorker(wg, 
done) as a goroutine and change it to do just the wg.Wait then send a signal on 
the “done” channel.

 

Next, start a goroutine which signals on a “timeout” channel if the timeout is 
exceeded.

 

Finally, change GetWorkerValues to be an endless loop containing a select with 
three cases: “<-cs” (add new value to array), “<-done” (break), and “<-timeout” 
(break).  After the loop, send the array.

 

John

John Souvestre - New Orleans LA

 

From: desaiabhi...@gmail.com [mailto:desaiabhi...@gmail.com] 
Sent: 2017 January 08, Sun 07:10
To: John Souvestre
Cc: golang-nuts
Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

Hi John

 

Can you please help me to apply correct logic to achieve it

 

Haven't found any solution on the net. All articles are talking about only 1 
routine timeout and not multiple

 

Thank you very much

 

Rgds

 

Abhi


On Jan 8, 2017, at 6:36 PM, John Souvestre <j...@souvestre.com> wrote:

Hi Abhi.

 

I believe that your logic is faulty.  The timeout does take place – but it 
doesn’t really do what you wanted, I think.

 

GetWorkerValues isn’t going to send it’s info because the “range cs” can’t 
finish until the channel is closed.  The channel is not closed until both 
workers are finished – timeout or not, thus it always contains the info for 
both workers.

 

John

John Souvestre - New Orleans LA

 

From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
Sent: 2017 January 08, Sun 06:24
To: golang-nuts
Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

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: golan...@googlegroups.com   
[mailto:golan...@googlegroups.com  ] On Behalf Of 
desaia...@gmail.com  
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: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of 
desaia...@gmail.com
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

 

p

Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread desaiabhijit
Hi John

Can you please help me to apply correct logic to achieve it

Haven't found any solution on the net. All articles are talking about only 1 
routine timeout and not multiple

Thank you very much

Rgds

Abhi

> On Jan 8, 2017, at 6:36 PM, John Souvestre <j...@souvestre.com> wrote:
> 
> Hi Abhi.
>  
> I believe that your logic is faulty.  The timeout does take place – but it 
> doesn’t really do what you wanted, I think.
>  
> GetWorkerValues isn’t going to send it’s info because the “range cs” can’t 
> finish until the channel is closed.  The channel is not closed until both 
> workers are finished – timeout or not, thus it always contains the info for 
> both workers.
>  
> John
> 
> John Souvestre - New Orleans LA
>  
> From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
> Behalf Of desaiabhi...@gmail.com
> Sent: 2017 January 08, Sun 06:24
> To: golang-nuts
> Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working
>  
> 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: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf 
> Of desaia...@gmail.com
> 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: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf 
> Of desaia...@gmail.com
> 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 m

RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
Hi Abhi.

 

I believe that your logic is faulty.  The timeout does take place – but it 
doesn’t really do what you wanted, I think.

 

GetWorkerValues isn’t going to send it’s info because the “range cs” can’t 
finish until the channel is closed.  The channel is not closed until both 
workers are finished – timeout or not, thus it always contains the info for 
both workers.

 

John

John Souvestre - New Orleans LA

 

From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
Sent: 2017 January 08, Sun 06:24
To: golang-nuts
Subject: Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

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: golan...@googlegroups.com   
[mailto:golan...@googlegroups.com  ] On Behalf Of 
desaia...@gmail.com  
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: golan...@googlegroups.com [mailto:golan...@googlegroups.com] On Behalf Of 
desaia...@gmail.com
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 := {}

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 *

Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread desaiabhijit
Hi John

I have taken code from one of the site  to make it work for me

In real program, spawning 100 goroutine to do the work with timeout 2 sec but 
found that entire program takes time which is equal to time taken by routine 
which finishes last irrespective of workgroup timeout

So need help to fix program so that timeout will force to close and merge the 
work done by all routines within 2 sec where few routines will return work 
where as few will not because of timeout

Thanks

Abhi

> On Jan 8, 2017, at 5:42 PM, John Souvestre <j...@souvestre.com> wrote:
> 
> Hi again.
>  
> Did you perhaps intend to make the call to monitorWorker() as a goroutine?  
> That would let it run in parallel with the rest of main.
>  
> John
> 
> John Souvestre - New Orleans LA
>  
> From: John Souvestre [mailto:j...@souvestre.com] 
> Sent: 2017 January 08, Sun 06:10
> To: 'golang-nuts'
> Subject: RE: [go-nuts] Multiple goroutine timeout for waitgroup not working
>  
> 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: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
> Behalf Of desaiabhi...@gmail.com
> 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
> -- 
> You received this message because you are subscribed to a topic in the Google 
> Groups "golang-nuts" group.
> To unsubscribe from this topic, visit 
> https://groups.google.com/d/topic/golang-nuts/o0DYWZIlmvs/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to 
> golang-nuts+unsubscr...@googlegroups.com.
> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread desaiabhijit
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:* golan...@googlegroups.com  [mailto:
> golan...@googlegroups.com ] *On Behalf Of *
> desaia...@gmail.com 
> *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:* golan...@googlegroups.com [mailto:golan...@googlegroups.com] *On 
> Behalf Of *desaia...@gmail.com
> *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 := {}
>
> 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)
>
>  
>
>  

RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
Hi again.

 

Did you perhaps intend to make the call to monitorWorker() as a goroutine?  
That would let it run in parallel with the rest of main.

 

John

John Souvestre - New Orleans LA

 

From: John Souvestre [mailto:j...@souvestre.com] 
Sent: 2017 January 08, Sun 06:10
To: 'golang-nuts'
Subject: RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

 

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: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
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

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
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: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
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: golan...@googlegroups.com   
[mailto:golan...@googlegroups.com  ] On Behalf Of 
desaia...@gmail.com  
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 := {}

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 golang-nuts...@googlegroups.com  .
For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"golang

Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread desaiabhijit
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:* golan...@googlegroups.com  [mailto:
> golan...@googlegroups.com ] *On Behalf Of *
> desaia...@gmail.com 
> *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 := {}
>
> 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 golang-nuts...@googlegroups.com .
> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread Abhijit Desai
Thanks for the reply Sairam

Problem is if I change Work2 time => 3 sec then that work should get
timeout but program still returning it

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)
}

Rgds,

Abhi

On Sun, Jan 8, 2017 at 1:39 PM, Sairam Kunala 
wrote:

> The ran the code on play, seems to come out within 2 seconds.
>
> https://play.golang.org/p/yZIAvXI8IX
>
> On Sun, Jan 8, 2017 at 12:53 PM,  wrote:
>
>>
>> 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 := {}
>> 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> 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 golang-nuts+unsubscr...@googlegroups.com.
>> 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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


RE: [go-nuts] Multiple goroutine timeout for waitgroup not working

2017-01-08 Thread John Souvestre
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: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On 
Behalf Of desaiabhi...@gmail.com
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 := {}

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 golang-nuts+unsubscr...@googlegroups.com.
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 golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.