goroutine

Max number of goroutines

心不动则不痛 提交于 2019-11-27 03:03:33
How many goroutines can I use painless? For example wikipedia says, in Erlang 20 million processes can be created without degrading performance. Update: I've just investigated in goroutines performance a little and got such a results: It looks like goroutine lifetime is more then calculating sqrt() 1000 times ( ~45µs for me ), the only limitation is memory Goroutine costs 4 — 4.5 KB If a goroutine is blocked, there is no cost involved other than: memory usage slower garbage-collection The costs (in terms of memory and average time to actually start executing a goroutine) are: Go 1.6.2 (April

How to wait for all goroutines to finish without using time.Sleep?

。_饼干妹妹 提交于 2019-11-27 02:50:15
This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the file is printed out). How do I avoid using the sleep method to keep the main method from exiting? I have problems wrapping my head around channels (I assume that's what it takes, to synchronize the results) so any help is appreciated! package main import ( "fmt" "io/ioutil" "path" "path/filepath" "os" "runtime" "time" ) func eachFile(extension string, callback func(file string)) { exeDir := filepath

Go, tcp too many open files debug

放肆的年华 提交于 2019-11-27 02:46:42
问题 Here's a straightforward Go http (tcp) connection test script func main() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, client") })) defer ts.Close() var wg sync.WaitGroup for i := 0; i < 2000; i++ { wg.Add(1) go func(i int) { defer wg.Done() resp, err := http.Get(ts.URL) if err != nil { panic(err) } greeting, err := ioutil.ReadAll(resp.Body) resp.Body.Close() if err != nil { panic(err) } fmt.Printf("%s", i, greeting) }(i) }

Example for sync.WaitGroup correct?

三世轮回 提交于 2019-11-27 02:34:00
Is this example usage of sync.WaitGroup correct? It gives the expected result, but I am unsure about the wg.Add(4) and the position of wg.Done() . Does it make sense to add the four goroutines at once with wg.Add() ? http://play.golang.org/p/ecvYHiie0P package main import ( "fmt" "sync" "time" ) func dosomething(millisecs time.Duration, wg *sync.WaitGroup) { duration := millisecs * time.Millisecond time.Sleep(duration) fmt.Println("Function in background, duration:", duration) wg.Done() } func main() { var wg sync.WaitGroup wg.Add(4) go dosomething(200, &wg) go dosomething(400, &wg) go

Why does it not create many threads when many goroutines are blocked in writing file in golang?

女生的网名这么多〃 提交于 2019-11-27 02:27:32
问题 As we know in go, a thread may be created when the goroutine has to perform a blocking call, such as a system call, or a call to a C library via cgo. Some test code: package main import ( "io/ioutil" "os" "runtime" "strconv" ) func main() { runtime.GOMAXPROCS(2) data, err := ioutil.ReadFile("./55555.log") if err != nil { println(err) return } for i := 0; i < 200; i++ { go func(n int) { for { err := ioutil.WriteFile("testxxx"+strconv.Itoa(n), []byte(data), os.ModePerm) if err != nil { println

Go project's main goroutine sleep forever?

不羁的心 提交于 2019-11-27 01:56:38
问题 Is there any API to let the main goroutine sleep forever? In other words, I want my project always run except when I stop it. 回答1: "Sleeping" You can use numerous constructs that block forever without "eating" up your CPU. For example a select without any case (and no default ): select{} Or receiving from a channel where nobody sends anything: <-make(chan int) Or receiving from a nil channel also blocks forever: <-(chan int)(nil) Or sending on a nil channel also blocks forever: (chan int)(nil

How best do I keep a long running Go program, running?

天大地大妈咪最大 提交于 2019-11-27 00:15:05
问题 I've a long running server written in Go. Main fires off several goroutines where the logic of the program executes. After that main does nothing useful. Once main exits, the program will quit. The method I am using right now to keep the program running is just a simple call to fmt.Scanln(). I'd like to know how others keep main from exiting. Below is a basic example. What ideas or best practices could be used here? I considered creating a channel and delaying exit of main by receiving on

How to collect values from N goroutines executed in a specific order?

萝らか妹 提交于 2019-11-26 22:55:58
Below is a struct of type Stuff. It has three ints. A Number , its Double and its Power . Let's pretend that calculating the double and power of a given list of ints is an expensive computation. type Stuff struct { Number int Double int Power int } func main() { nums := []int{2, 3, 4} // given numbers stuff := []Stuff{} // struct of stuff with transformed ints double := make(chan int) power := make(chan int) for _, i := range nums { go doubleNumber(i, double) go powerNumber(i, power) } // How do I get the values back in the right order? fmt.Println(stuff) } func doubleNumber(i int, c chan int)

Why is this Go code blocking?

我们两清 提交于 2019-11-26 22:54:32
问题 I wrote the following program: package main import ( "fmt" ) func processevents(list chan func()) { for { //a := <-list //a() } } func test() { fmt.Println("Ho!") } func main() { eventlist := make(chan func(), 100) go processevents(eventlist) for { eventlist <- test fmt.Println("Hey!") } } Since the channel eventlist is a buffered channel, I think I should get at exactly 100 times the output "Hey!", but it is displayed only once. Where is my mistake? 回答1: Update (Go version 1.2+) As of Go 1.2

all go routines are asleep - deadlock

瘦欲@ 提交于 2019-11-26 22:10:31
问题 I don't understand why the deadlock is occurring in this code. I've tried several different things to get the deadlock to stop (several different versions using WorkGroup). This is my first day in Go, and I am pretty disappointed so far with the complexity of fairly simple and straightforward operations. I feel like I'm missing something big and obvious, but all of the docs I have found on this are seemingly very different from what, to me, is a very basic mode of operation. All of the docs