goroutine

Always have x number of goroutines running at any time

蹲街弑〆低调 提交于 2019-12-03 03:39:34
问题 I see lots of tutorials and examples on how to make Go wait for x number of goroutines to finish, but what I'm trying to do is have ensure there are always x number running, so a new goroutine is launched as soon as one ends. Specifically I have a few hundred thousand 'things to do' which is processing some stuff that is coming out of MySQL. So it works like this: db, err := sql.Open("mysql", connection_string) checkErr(err) defer db.Close() rows,err := db.Query(`SELECT id FROM table`)

Catching return values from goroutines

自古美人都是妖i 提交于 2019-12-03 01:26:59
问题 The below code gives compilation error saying 'unexpected go': x := go doSomething(arg) func doSomething(arg int) int{ ... return my_int_value } I know, I can fetch the return value if call the function normally, without using goroutine. Or I can use channels etc. My question is why is it not possible to fetch a return value like this from a goroutine. 回答1: The strict answer is that you can do that. It's just probably not a good idea. Here's code that would do that: var x int go func() { x =

Golang context.WithValue: how to add several key-value pairs

偶尔善良 提交于 2019-12-02 23:55:30
With Go's context package it is possible to pass request-specific data to the stack of request handling functions using func WithValue(parent Context, key, val interface{}) Context This creates a new Context which is a copy of parent and contains the value val which can be accessed with key. How do I proceed if I want to store several key-value pairs in a Context ? Shall I call WithValue() several times, each time passing the Context received from my last call to WithValue() ? This appears cumbersome. Or shall I use a struct and put all my data there, s.t. I need to pass only one value (which

Context timeout implementation on every request using golang

梦想的初衷 提交于 2019-12-02 21:07:33
I am trying to handle context timeout for every request. We have following server structures: Flow overview: Go Server : Basically, acts as a [Reverse-proxy]. 2 Auth Server : Check for requests Authentication. Application Server : Core request processing logic. Now if Authorization server can't able to process a request in stipulated time, then I want to close the goroutine from memory. Here is what I tried: ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() req, _ := http.NewRequest("GET", authorizationServer, nil) req.Header = r.Header req.WithContext(ctx)

Always have x number of goroutines running at any time

徘徊边缘 提交于 2019-12-02 17:08:19
I see lots of tutorials and examples on how to make Go wait for x number of goroutines to finish, but what I'm trying to do is have ensure there are always x number running, so a new goroutine is launched as soon as one ends. Specifically I have a few hundred thousand 'things to do' which is processing some stuff that is coming out of MySQL. So it works like this: db, err := sql.Open("mysql", connection_string) checkErr(err) defer db.Close() rows,err := db.Query(`SELECT id FROM table`) checkErr(err) defer rows.Close() var id uint for rows.Next() { err := rows.Scan(&id) checkErr(err) go

Catching return values from goroutines

跟風遠走 提交于 2019-12-02 15:04:03
The below code gives compilation error saying 'unexpected go': x := go doSomething(arg) func doSomething(arg int) int{ ... return my_int_value } I know, I can fetch the return value if call the function normally, without using goroutine. Or I can use channels etc. My question is why is it not possible to fetch a return value like this from a goroutine. The strict answer is that you can do that. It's just probably not a good idea. Here's code that would do that: var x int go func() { x = doSomething() }() This will spawn off a new goroutine which will calculate doSomething() and then assign the

go routine for range over channels

邮差的信 提交于 2019-12-02 09:07:47
问题 I have been working in Golang for a long time. But still I am facing this problem though I know the solution to my problem. But never figured out why is it happening. For example If I have a pipeline situation for inbound and outbound channels like below: package main import ( "fmt" ) func main() { for n := range sq(sq(gen(3, 4))) { fmt.Println(n) } fmt.Println("Process completed") } func gen(nums ...int) <-chan int { out := make(chan int) go func() { for _, n := range nums { out <- n } close

Goroutines 8kb and windows OS thread 1 mb

天大地大妈咪最大 提交于 2019-12-02 01:28:00
As windows user, I know that OS threads consume ~1 Mb of memory due to By default, Windows allocates 1 MB of memory for each thread’s user-mode stack. How does golang use ~8kb of memory for each goroutine , if OS thread is much more gluttonous. Are goroutine sort of virtual threads? Goroutines are not threads, they are (from the spec ): ...an independent concurrent thread of control, or goroutine , within the same address space. Effective Go defines them as: They're called goroutines because the existing terms—threads, coroutines, processes, and so on—convey inaccurate connotations. A

Why does this code cause data race?

試著忘記壹切 提交于 2019-12-01 22:55:22
1 package main 2 3 import "time" 4 5 func main() { 6 m1 := make(map[string]int) 7 m1["hello"] = 1 8 m1["world"] = 2 9 go func() { 10 for i := 0; i < 100000000; i++ { 11 _ = m1["hello"] 12 } 13 }() 14 time.Sleep(100 * time.Millisecond) 15 m2 := make(map[string]int) 16 m2["hello"] = 3 17 m1 = m2 18 } I run command go run --race with this code and get: ================== WARNING: DATA RACE Read at 0x00c420080000 by goroutine 5: runtime.mapaccess1_faststr() /usr/local/go/src/runtime/hashmap_fast.go:208 +0x0 main.main.func1() /Users/meitu/test/go/map.go:11 +0x80 Previous write at 0x00c420080000 by

Stop goroutine execution on timeout

 ̄綄美尐妖づ 提交于 2019-12-01 19:57:07
I want to stop goroutine execution on timeout. But it seems like it is not working for me. I am using iris framework. type Response struct { data interface{} status bool } func (s *CicService) Find() (interface{}, bool) { ch := make(chan Response, 1) go func() { time.Sleep(10 * time.Second) fmt.Println("test") fmt.Println("test1") ch <- Response{data: "data", status: true} }() select { case <-ch: fmt.Println("Read from ch") res := <-ch return res.data, res.status case <-time.After(50 * time.Millisecond): return "Timed out", false } } Output: Timed out test test1 Expected Output: Timed out Can