goroutine

all goroutines are asleep deadlock

£可爱£侵袭症+ 提交于 2021-02-20 18:17:53
问题 I am trying to play around with goroutines and channel package main import ( "fmt" "math/rand" "time" ) func boring(msg string) <-chan string { c := make(chan string) go func() { for i := 0; ; i++ { c <- fmt.Sprintf("%s %d", msg, i) time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) } }() return c } func main() { c := fanInNew(boring("joe"), boring("anh")) for i := 0; i < 10; i++ { fmt.Println(<-c) } fmt.Println("You both are boring, I am leaving") } func fanInNew(input1, input2 <

all goroutines are asleep deadlock

不羁的心 提交于 2021-02-20 18:14:06
问题 I am trying to play around with goroutines and channel package main import ( "fmt" "math/rand" "time" ) func boring(msg string) <-chan string { c := make(chan string) go func() { for i := 0; ; i++ { c <- fmt.Sprintf("%s %d", msg, i) time.Sleep(time.Duration(rand.Intn(1e3)) * time.Millisecond) } }() return c } func main() { c := fanInNew(boring("joe"), boring("anh")) for i := 0; i < 10; i++ { fmt.Println(<-c) } fmt.Println("You both are boring, I am leaving") } func fanInNew(input1, input2 <

How to get the number of elements in an unbuffered channel

回眸只為那壹抹淺笑 提交于 2021-02-10 07:29:07
问题 I am in a situation where my program is deadlocking and I want to debug this and tell how many elements are in an unbuffered channel, is there any way to do this in Go? The following code does not output a 2 as I would expect (further it deadlocks, which is also something I cannot find a reason for) package main import "fmt" func main() { channel := make(chan string) done_channel := make(chan bool) go func() { channel <- "value" channel <- "value" fmt.Println(len(channel)) done_channel <-

Best way to pass context.Context to a closure executed in a separate goroutine

三世轮回 提交于 2021-02-10 06:00:16
问题 What is best way to pass context.Context to closure to be executed in a separate goroutine? Since I'm not mutating context.Context inside the closure, I assume both options are valid. 2nd option can save me tiny bit of memory by not copying the interface. 1) pass as an argument func run(ctx context.Context) { for i := 0; i <= 5; i++ { go func(id int, ictx context.Context) { for { select { case <- ictx.Done(): return default: // do something } } }(i, ctx) } } 2) expose outer context variable

Break out of select loop?

时光毁灭记忆、已成空白 提交于 2021-02-05 20:19:57
问题 I'm trying to use a select in a loop to receive either a message or a timeout signal. If the timeout signal is received, the loop should abort: package main import ("fmt"; "time") func main() { done := time.After(1*time.Millisecond) numbers := make(chan int) go func() {for n:=0;; {numbers <- n; n++}}() for { select { case <-done: break case num := <- numbers: fmt.Println(num) } } } However, it doesn't seem to be stopping: $ go run a.go 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Goroutine Timeout

若如初见. 提交于 2021-02-05 11:30:35
问题 type Response struct { data interface{} status bool } func Find() (interface{}, bool) { ch := make(chan Response, 1) go func() { data, status := findCicCode() ch <- Response{data: data, status: status} }() select { case response := <-ch: return response.data, response.status case <-time.After(50 * time.Millisecond): return "Request timed out", false } } So, I have above function. Basically findCicCode() function call makes 3 http calls internally to external services. I have added combined

golang实现异步并发sokect

点点圈 提交于 2021-02-02 05:44:39
搜索golang + epoll的例子,得到下面这段代码,感觉golang的编程思维真正做到了并行编程: package main import ( "fmt" "net" "os" "time" ) const ( MAX_CONN_NUM = 5 ) //echo server Goroutine func EchoFunc(conn net.Conn) { defer conn.Close() buf := make([]byte, 1024) for { _, err := conn.Read(buf) if err != nil { //println("Error reading:", err.Error()) return } //send reply _, err = conn.Write(buf) if err != nil { //println("Error send reply:", err.Error()) return } } } //initial listener and run func main() { listener, err := net.Listen("tcp", "0.0.0.0:8088") if err != nil { fmt.Println("error listening:", err.Error()) os.Exit(1)

Multiple goroutines access/modify a list/map

前提是你 提交于 2021-01-28 20:10:52
问题 I am trying to implement a multithreaded crawler using a go lang as a sample task to learn the language. It supposed to scan pages, follow links and save them do DB. To avoid duplicates I'm trying to use map where I save all the URLs I've already saved. The synchronous version works fine, but I have troubles when I'm trying to use goroutines. I'm trying to use mutex as a sync object for map, and channel as a way to coordinate goroutines. But obviously I don't have clear understanding of them.

Check if function is being called as goroutine or not

我与影子孤独终老i 提交于 2021-01-28 10:56:25
问题 Is there any way to find out if a running function was called as goroutine or not? I've read 'go tour' and I am interested in building a websocket server with golang, so I found this tutorial https://tutorialedge.net/golang/go-websocket-tutorial/ Now I'm wondering if wsEndpoint function from the tutorial is invoked as goroutine (e.g. go wsEndpoint(...)) or not. I've tried to read http package documentation, but did not get clear picture, just a guess that the handler will be called with go

Golang Goroutine Error “all goroutines are asleep - deadlock!”

眉间皱痕 提交于 2021-01-28 05:50:32
问题 I am trying to make a code to scan from a folder link all my files and make a "top 10" by his size with also a regexp based on his content and his name. file. By it content, I make channels with goroutines but I dont understand why each time my goroutines are locked. Here is my code: package main import ( "flag" "fmt" "io/ioutil" "regexp" "runtime" "sort" "sync" "time" ) var rName = ".php" var rContent = "php" var maxSize, minSize int64 var files_ten []File func main() { start := time.Now()