goroutine

How to run the maximum number of goroutines

戏子无情 提交于 2020-07-23 06:18:06
问题 The code is like this: func find(start int, end int){ for i := start; i < end ; i++ { // Do something... } } func main() { // Do something... // For example, I know: len = 1500 // and run max goroutine are 3 // will be go find(0, 500) go find(500, 1000) go find(1000, 1500) // Do something... } That is when I know in advance the maximum threads of goroutines and the length of "length". But if I do not know how many threads can run at goroutine, and the length of "length". Is there any way to

How to run the maximum number of goroutines

橙三吉。 提交于 2020-07-23 06:17:08
问题 The code is like this: func find(start int, end int){ for i := start; i < end ; i++ { // Do something... } } func main() { // Do something... // For example, I know: len = 1500 // and run max goroutine are 3 // will be go find(0, 500) go find(500, 1000) go find(1000, 1500) // Do something... } That is when I know in advance the maximum threads of goroutines and the length of "length". But if I do not know how many threads can run at goroutine, and the length of "length". Is there any way to

How to run the maximum number of goroutines

假装没事ソ 提交于 2020-07-23 06:16:31
问题 The code is like this: func find(start int, end int){ for i := start; i < end ; i++ { // Do something... } } func main() { // Do something... // For example, I know: len = 1500 // and run max goroutine are 3 // will be go find(0, 500) go find(500, 1000) go find(1000, 1500) // Do something... } That is when I know in advance the maximum threads of goroutines and the length of "length". But if I do not know how many threads can run at goroutine, and the length of "length". Is there any way to

Wait result of multiple goroutines

独自空忆成欢 提交于 2020-05-08 07:03:35
问题 I am searching a way to execute asynchronously two functions in go which returns different results and errors, wait for them to finish and print both results. Also if one of function returned error I do not want to wait for another function, and just print the error. For example, I have this functions: func methodInt(error bool) (int, error) { <-time.NewTimer(time.Millisecond * 100).C if error { return 0, errors.New("Some error") } else { return 1, nil } } func methodString(error bool)

Wait result of multiple goroutines

烈酒焚心 提交于 2020-05-08 07:02:09
问题 I am searching a way to execute asynchronously two functions in go which returns different results and errors, wait for them to finish and print both results. Also if one of function returned error I do not want to wait for another function, and just print the error. For example, I have this functions: func methodInt(error bool) (int, error) { <-time.NewTimer(time.Millisecond * 100).C if error { return 0, errors.New("Some error") } else { return 1, nil } } func methodString(error bool)

golang too many open files in go function, goroutine [closed]

▼魔方 西西 提交于 2020-04-08 18:13:39
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . package main import ( "os" "sync" ) func main() { var wg sync.WaitGroup wg.Add(1024 * 1024) for i := 0; i < (1024 * 1024); i++ { go func(index int) { if f, e := os.Open(i); e == nil { //blah blah f.Close() } }(i) } wg.Done() } If you run the program brings up the following error.

golang too many open files in go function, goroutine [closed]

独自空忆成欢 提交于 2020-04-08 18:13:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . package main import ( "os" "sync" ) func main() { var wg sync.WaitGroup wg.Add(1024 * 1024) for i := 0; i < (1024 * 1024); i++ { go func(index int) { if f, e := os.Open(i); e == nil { //blah blah f.Close() } }(i) } wg.Done() } If you run the program brings up the following error.

golang too many open files in go function, goroutine [closed]

只谈情不闲聊 提交于 2020-04-08 18:11:34
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . package main import ( "os" "sync" ) func main() { var wg sync.WaitGroup wg.Add(1024 * 1024) for i := 0; i < (1024 * 1024); i++ { go func(index int) { if f, e := os.Open(i); e == nil { //blah blah f.Close() } }(i) } wg.Done() } If you run the program brings up the following error.

How to share one HTTP request instance beween two goroutines?

◇◆丶佛笑我妖孽 提交于 2020-03-28 06:39:31
问题 I have some code that makes 3 requests to fill 3 variables now. Two requests are same. I want to share one http request between two different functions (in real world, these functions are splitted into two different modules). Let me describe the problem what I have based on much simpler example than I have in real world. At the moment, I have the following main function and Post data structure: type Post struct { ID int `json:"id"` Title string `json:"title"` UserID int `json:"userId"`

Zombie goroutines in Go

三世轮回 提交于 2020-02-29 06:24:17
问题 I was always wondering how could a goroutine in Go get killed until i recently watched A Channel Compendium by John Graham-Cumming at GoPherCon and realized that its as simple as having a return statement inside the code executed by goroutines. So the same goes for closures right? Shouldn't we always use return statements inside closures so every goroutine executing a closure terminates at some point? What are the disadvantages of having zombie goroutines in your system and how do they get