[go-nuts] Re: Goroutines chan to get data at specific time
looks better solution to restrict goroutines to continue it's pending work Thanks for the reply On Monday, August 7, 2017 at 8:21:09 AM UTC+5:30, Timothy Raymond wrote: > > I'm not entirely sure, but my intuition says that using `time.After` like > that with pending sends from another goroutine will cause that goroutine to > leak. A better solution may be to use the `WithTimeout` functionality of > the `context` package and then periodically check the `Done()` channel to > see if the timeout has expired. It works something like this if you imagine > `HeavyWork` is a call to an external API: > https://play.golang.org/p/DRtgNBLnE5 . > > On Sunday, August 6, 2017 at 3:53:36 AM UTC-4, Abhijit Desai wrote: >> >> Can you please help with below code to get output at specific cutoff time >> and exit >> >> Thanks in advance >> >> Abhi >> >> >> >> package main >> >> import "time" >> import "fmt" >> >> func main() { >> >> c1 := make(chan string) >> >> go func() { //Sending data after certain time >> >> c1 <- "result 1" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 1 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 2 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 3 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 4 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 5 sec" >> }() >> >> select { >> case <-time.After(time.Second * 4): { //cut off 4s and return the >> value >> res := <-c1 >> fmt.Println(res) // expecting result "result 2 afer 3 sec" >> but returning "result 1" >> } >> } >> } >> > -- 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] Re: Goroutines chan to get data at specific time
I’m not sure exactly what you are trying to accomplish, but I suspect that this article would help you. https://blog.golang.org/go-concurrency-patterns-timing-out-and John John Souvestre - New Orleans LA From: golang-nuts@googlegroups.com [mailto:golang-nuts@googlegroups.com] On Behalf Of desaiabhi...@gmail.com Sent: 2017 August 06, Sun 09:58 To: golang-nuts Subject: [go-nuts] Re: Goroutines chan to get data at specific time yes this is what I want.. Need to access web service multiple times to get the entire data unless return status is not "completed" But same time I need timeout Thanks for the help Thanks Abhi On Sunday, August 6, 2017 at 7:48:05 PM UTC+5:30, snmed wrote: I still no get the idea behind your requirement, in your example you calling the web service 6 times in sequential manner and then write it to the channel. But again you can only write once to a channel with a capacity of one, as long as you not read from the same channel and you still read it in the select statement. That means all other calls to 'GetPartWebServiceData()' in the goroutine are blocked until that read in the select statement happens. In your case it is always the first value written to the channel. I try to give you a solution with although i'm not completely understand your requirements: Here a working sequential solution: https://play.golang.org/p/2qohwIjP96 I hope that helps Cheers snmed Am Sonntag, 6. August 2017 13:05:11 UTC+2 schrieb Abhijit Desai: Requirement is to collect the data with in cuttoff time say 3 sec by invoking web service multiple times and return only latest invoked data c1 <- "result 1" // In real scenario this taking almost 1 seconds as in real scenario it's taking Part chunk of data from Web Service Something like... Expecting data from c1 <- GetPartWebServiceData("request 3") //at least I should get this data!? package main import "time" import "fmt" func main() { c1 := make(chan string, 1) go func() { c1 <- GetPartWebServiceData("request 1") //say try 6 times c1 <- GetPartWebServiceData("request 2") c1 <- GetPartWebServiceData("request 3") //at least I should get this data!? c1 <- GetPartWebServiceData("request 4") c1 <- GetPartWebServiceData("request 5") c1 <- GetPartWebServiceData("request 6") }() select { case res := <-c1: //here program get out without waiting 3 seconds because it got the data fmt.Println(res) case <-time.After(time.Second * 4): fmt.Println("timeout") } } func GetPartWebServiceData(request string) string{ time.Sleep(time.Second * 1) // Time Consuming work returning part value partResponse := "Response for " + request //some response return partResponse } On Sunday, August 6, 2017 at 4:03:08 PM UTC+5:30, snmed wrote: Hi What are you trying to solve? Your channel has a capacity of 1, the first write to c1 is successful but the second write to c1 is blocked until it has been read in the select statement. And therefore you print the first value written to c1. I recommend you to read this https://golang.org/doc/effective_go.html#channels documentation about channels. Cheers snmed -- 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] Re: Goroutines chan to get data at specific time
I'm not entirely sure, but my intuition says that using `time.After` like that with pending sends from another goroutine will cause that goroutine to leak. A better solution may be to use the `WithTimeout` functionality of the `context` package and then periodically check the `Done()` channel to see if the timeout has expired. It works something like this if you imagine `HeavyWork` is a call to an external API: https://play.golang.org/p/DRtgNBLnE5 . On Sunday, August 6, 2017 at 3:53:36 AM UTC-4, Abhijit Desai wrote: > > Can you please help with below code to get output at specific cutoff time > and exit > > Thanks in advance > > Abhi > > > > package main > > import "time" > import "fmt" > > func main() { > > c1 := make(chan string) > > go func() { //Sending data after certain time > > c1 <- "result 1" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 1 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 2 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 3 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 4 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 5 sec" > }() > > select { > case <-time.After(time.Second * 4): { //cut off 4s and return the > value > res := <-c1 > fmt.Println(res) // expecting result "result 2 afer 3 sec" > but returning "result 1" > } > } > } > -- 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] Re: Goroutines chan to get data at specific time
yes this is what I want.. Need to access web service multiple times to get the entire data unless return status is not "completed" But same time I need timeout Thanks for the help Thanks Abhi On Sunday, August 6, 2017 at 7:48:05 PM UTC+5:30, snmed wrote: > > I still no get the idea behind your requirement, in your example you > calling the web service 6 times in sequential manner and then write it to > the channel. > But again you can only write once to a channel with a capacity of one, as > long as you not read from the same channel and you still read it in the > select statement. > That means all other calls to 'GetPartWebServiceData()' in the goroutine > are blocked until that read in the select statement happens. In your case > it is always the first > value written to the channel. > > I try to give you a solution with although i'm not completely understand > your requirements: > > Here a working sequential solution: https://play.golang.org/p/2qohwIjP96 > > I hope that helps > > Cheers snmed > > Am Sonntag, 6. August 2017 13:05:11 UTC+2 schrieb Abhijit Desai: >> >> Requirement is to collect the data with in cuttoff time say 3 sec by >> invoking web service multiple times and return only latest invoked data >> >> c1 <- "result 1" // In real scenario this taking almost 1 seconds as in >> real scenario it's taking Part chunk of data from Web Service >> >> Something like... Expecting data from c1 <- >> GetPartWebServiceData("request 3") //at least I should get this data!? >> >> >> package main >> >> import "time" >> import "fmt" >> >> func main() { >> >> c1 := make(chan string, 1) >> >> go func() { >> >> c1 <- GetPartWebServiceData("request 1") //say try 6 times >> c1 <- GetPartWebServiceData("request 2") >> c1 <- GetPartWebServiceData("request 3") //at least I should get >> this data!? >> c1 <- GetPartWebServiceData("request 4") >> c1 <- GetPartWebServiceData("request 5") >> c1 <- GetPartWebServiceData("request 6") >> }() >> >> select { >> case res := <-c1: //here program get out without waiting 3 >> seconds because it got the data >> fmt.Println(res) >> case <-time.After(time.Second * 4): >> fmt.Println("timeout") >> } >> } >> >> func GetPartWebServiceData(request string) string{ >> time.Sleep(time.Second * 1) // Time Consuming work returning part >> value >> partResponse := "Response for " + request //some response >> return partResponse >> } >> >> >> >> On Sunday, August 6, 2017 at 4:03:08 PM UTC+5:30, snmed wrote: >>> >>> Hi >>> >>> What are you trying to solve? Your channel has a capacity of 1, the >>> first write to c1 is successful but the second write to c1 is blocked until >>> it has been read in the select statement. And therefore you print the first >>> value written to c1. >>> >>> I recommend you to read this >>> https://golang.org/doc/effective_go.html#channels documentation about >>> channels. >>> >>> Cheers snmed >>> >>> -- 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] Re: Goroutines chan to get data at specific time
I still no get the idea behind your requirement, in your example you calling the web service 6 times in sequential manner and then write it to the channel. But again you can only write once to a channel with a capacity of one, as long as you not read from the same channel and you still read it in the select statement. That means all other calls to 'GetPartWebServiceData()' in the goroutine are blocked until that read in the select statement happens. In your case it is always the first value written to the channel. I try to give you a solution with although i'm not completely understand your requirements: Here a working sequential solution: https://play.golang.org/p/2qohwIjP96 I hope that helps Cheers snmed Am Sonntag, 6. August 2017 13:05:11 UTC+2 schrieb Abhijit Desai: > > Requirement is to collect the data with in cuttoff time say 3 sec by > invoking web service multiple times and return only latest invoked data > > c1 <- "result 1" // In real scenario this taking almost 1 seconds as in > real scenario it's taking Part chunk of data from Web Service > > Something like... Expecting data from c1 <- GetPartWebServiceData("request > 3") //at least I should get this data!? > > > package main > > import "time" > import "fmt" > > func main() { > > c1 := make(chan string, 1) > > go func() { > > c1 <- GetPartWebServiceData("request 1") //say try 6 times > c1 <- GetPartWebServiceData("request 2") > c1 <- GetPartWebServiceData("request 3") //at least I should get > this data!? > c1 <- GetPartWebServiceData("request 4") > c1 <- GetPartWebServiceData("request 5") > c1 <- GetPartWebServiceData("request 6") > }() > > select { > case res := <-c1: //here program get out without waiting 3 > seconds because it got the data > fmt.Println(res) > case <-time.After(time.Second * 4): > fmt.Println("timeout") > } > } > > func GetPartWebServiceData(request string) string{ > time.Sleep(time.Second * 1) // Time Consuming work returning part value > partResponse := "Response for " + request //some response > return partResponse > } > > > > On Sunday, August 6, 2017 at 4:03:08 PM UTC+5:30, snmed wrote: >> >> Hi >> >> What are you trying to solve? Your channel has a capacity of 1, the first >> write to c1 is successful but the second write to c1 is blocked until it >> has been read in the select statement. And therefore you print the first >> value written to c1. >> >> I recommend you to read this >> https://golang.org/doc/effective_go.html#channels documentation about >> channels. >> >> Cheers snmed >> >> -- 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] Re: Goroutines chan to get data at specific time
hi Bjoern >>> Why would you expect this? "result 1" is the fist value that was sent to the channel. Channels are sort of FIFO. Yeah I know.. but what is the workaround to get latest c1 value which is retrieve just before timeout Do I need to use "for ( loop )" around Select till timeout to invoke res: = > > > Am Sonntag, 6. August 2017 09:53:36 UTC+2 schrieb Abhijit Desai: >> >> Can you please help with below code to get output at specific cutoff time >> and exit >> >> Thanks in advance >> >> Abhi >> >> >> >> package main >> >> import "time" >> import "fmt" >> >> func main() { >> >> c1 := make(chan string) >> >> go func() { //Sending data after certain time >> >> c1 <- "result 1" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 1 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 2 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 3 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 4 sec" >> >> time.Sleep(time.Second * 1) >> c1 <- "result 2 afer 5 sec" >> }() >> >> select { >> case <-time.After(time.Second * 4): { //cut off 4s and return the >> value >> res := <-c1 >> fmt.Println(res) // expecting result "result 2 afer 3 sec" >> but returning "result 1" >> > > Why would you expect this? "result 1" is the fist value that was sent to > the channel. Channels are sort of FIFO. > > >> } >> } >> } >> > -- 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] Re: Goroutines chan to get data at specific time
Am Sonntag, 6. August 2017 09:53:36 UTC+2 schrieb Abhijit Desai: > > Can you please help with below code to get output at specific cutoff time > and exit > > Thanks in advance > > Abhi > > > > package main > > import "time" > import "fmt" > > func main() { > > c1 := make(chan string) > > go func() { //Sending data after certain time > > c1 <- "result 1" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 1 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 2 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 3 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 4 sec" > > time.Sleep(time.Second * 1) > c1 <- "result 2 afer 5 sec" > }() > > select { > case <-time.After(time.Second * 4): { //cut off 4s and return the > value > res := <-c1 > fmt.Println(res) // expecting result "result 2 afer 3 sec" > but returning "result 1" > Why would you expect this? "result 1" is the fist value that was sent to the channel. Channels are sort of FIFO. > } > } > } > -- 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] Re: Goroutines chan to get data at specific time
Requirement is to collect the data with in cuttoff time say 3 sec by invoking web service multiple times and return only latest invoked data c1 <- "result 1" // In real scenario this taking almost 1 seconds as in real scenario it's taking Part chunk of data from Web Service Something like... Expecting data from c1 <- GetPartWebServiceData("request 3") //at least I should get this data!? package main import "time" import "fmt" func main() { c1 := make(chan string, 1) go func() { c1 <- GetPartWebServiceData("request 1") //say try 6 times c1 <- GetPartWebServiceData("request 2") c1 <- GetPartWebServiceData("request 3") //at least I should get this data!? c1 <- GetPartWebServiceData("request 4") c1 <- GetPartWebServiceData("request 5") c1 <- GetPartWebServiceData("request 6") }() select { case res := <-c1: //here program get out without waiting 3 seconds because it got the data fmt.Println(res) case <-time.After(time.Second * 4): fmt.Println("timeout") } } func GetPartWebServiceData(request string) string{ time.Sleep(time.Second * 1) // Time Consuming work returning part value partResponse := "Response for " + request //some response return partResponse } On Sunday, August 6, 2017 at 4:03:08 PM UTC+5:30, snmed wrote: > > Hi > > What are you trying to solve? Your channel has a capacity of 1, the first > write to c1 is successful but the second write to c1 is blocked until it > has been read in the select statement. And therefore you print the first > value written to c1. > > I recommend you to read this > https://golang.org/doc/effective_go.html#channels documentation about > channels. > > Cheers snmed > > -- 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.