goroutine

Goroutine does not execute if time.Sleep included

让人想犯罪 __ 提交于 2020-01-08 17:17:07
问题 The following code runs perfectly fine: package main import ( "fmt" ) func my_func(c chan int){ fmt.Println(<-c) } func main(){ c := make(chan int) go my_func(c) c<-3 } playgound_1 However if I change c<-3 to time.Sleep(time.Second) c<-3 playground_2 My code does not execute. My gut feeling is that somehow main returns before the my_func finishes executing, but it seems like adding a pause should not have any effect. I am totally lost on this simple example, what's going on here? 回答1: When

How to stop one of multilpe of the same goroutine

风格不统一 提交于 2020-01-06 10:09:52
问题 As it will be made obvious soon, I am a golang n00b. I have some go code that starts goroutines based on an event channel. Say it starts 2 goroutines because we receive 2 events of type START. The goroutine is started with an uri as parameter, which gives us something unique about it. Later we receive one event of type STOP. How can I stop the goroutine that was started with the same uri ? for { select { case event := <-eventCh: if event.Entry != nil { switch event.Action { case foo.START:

Why golang don't iterate correctly in my for loop with range?

偶尔善良 提交于 2020-01-05 04:06:08
问题 I am confused why the following code does not print out iterated value. test:= []int{0,1,2,3,4} for i,v := range test{ go func(){ fmt.Println(i,v) } } What I think is that it should print out 0 0 1 1 2 2 3 3 4 4 But instead, it printed out 4 4 4 4 4 4 4 4 4 4 回答1: Your goroutines don't capture the current value of the variables i and v , but rather they reference the variables themselves. In this case, the 5 spawned goroutines did not get scheduled until the for loop finished, so all printed

Python-style generators in Go

好久不见. 提交于 2019-12-31 10:42:45
问题 I'm currently working through the Tour of Go, and I thought that goroutines have been used similarly to Python generators, particularly with Question 66. I thought 66 looked complex, so I rewrote it to this: package main import "fmt" func fibonacci(c chan int) { x, y := 1, 1 for { c <- x x, y = y, x + y } } func main() { c := make(chan int) go fibonacci(c) for i := 0; i < 10; i++ { fmt.Println(<-c) } } This seems to work. A couple of questions: If I turn up the buffer size on the channel to

golang http timeout and goroutines accumulation

随声附和 提交于 2019-12-30 05:23:04
问题 I use goroutines achieve http.Get timeout, and then I found that the number has been rising steadily goroutines, and when it reaches 1000 or so, the program will exit Code: package main import ( "errors" "io/ioutil" "log" "net" "net/http" "runtime" "time" ) // timeout dialler func timeoutDialler(timeout time.Duration) func(network, addr string) (net.Conn, error) { return func(network, addr string) (net.Conn, error) { return net.DialTimeout(network, addr, timeout) } } func timeoutHttpGet(url

Working with Slices and Golang sync.Map Structure

孤街浪徒 提交于 2019-12-25 19:37:49
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

末鹿安然 提交于 2019-12-25 19:37:33
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

Working with Slices and Golang sync.Map Structure

☆樱花仙子☆ 提交于 2019-12-25 19:37:17
问题 In order to debug some concurrency issues, I am in the process of switching part of my code from working on a regular Golang map to working on a sync.Map. However, when I try to run my new code, I am encountering two errors that I'm not sure how to debug. The original code block: sync_mutex.Lock() if _, ok := the_map[cur_h]; ok { the_map[cur_h] = append(the_map[cur_h], cur_id) } else { value := []int{cur_id} the_map[cur_h] = value } sync_mutex.Unlock() The new code block: if _, ok := sync_map

golang runtime: failed to create new OS thread (have 2049 already; errno=12)

大兔子大兔子 提交于 2019-12-25 08:48:37
问题 I created lots of goroutines on MacOs, and there was an error emitted when program executing. goRoutineId = 3710, i = 3683, len(chan) = 2049 runtime: failed to create new OS thread (have 2049 already; errno=12) fatal error: runtime.newosproc So I wonder what the "failed to create new OS thread" means, is that an operating system limitation, of just golang has no ability to create more goroutine? Thank you for helping me. 回答1: It's OS's limitation. I would assume you are using linux. According

goroutine soon blocked the http server when it was requested

雨燕双飞 提交于 2019-12-24 23:15:30
问题 goroutine sooblocked the http server when it was reqn uested The following code will soon be blocked In a device management function, by visiting the http REST ful interface to determine whether the device is online, 30s access to 1000 devices, the current program is roughly as follows to see the number of goroutine is not very high, but soon the program will not Move, cpu, memory is not occupied too high package main import ( "fmt" "net/http" "runtime" "time" ) func a() { b() //..... } var