goroutine

Can I concurrently write different slice elements

喜欢而已 提交于 2019-12-17 09:47:48
问题 I have a slice that contains work to be done, and a slice that will contain the results when everything is done. The following is a sketch of my general process: var results = make([]Result, len(jobs)) wg := sync.WaitGroup{} for i, job := range jobs { wg.Add(1) go func(i int, j job) { defer wg.Done() var r Result = doWork(j) results[i] = r }(i, job) } wg.Wait() // Use results It seems to work, but I have not tested it thoroughly and am not sure if it is safe to do. Generally I would not feel

How would you define a pool of goroutines to be executed at once in Golang?

霸气de小男生 提交于 2019-12-17 08:01:22
问题 TL;TR: Please just go to the last part and tell me how you would solve this problem. I've begun using Golang this morning coming from Python. I want to call a closed-source executable from Go several times, with a bit of concurrency, with different command line arguments. My resulting code is working just well but I'd like to get your input in order to improve it. Since I'm at an early learning stage, I'll also explain my workflow. For the sake of simplicity, assume here that this "external

How would you define a pool of goroutines to be executed at once in Golang?

孤街浪徒 提交于 2019-12-17 08:01:11
问题 TL;TR: Please just go to the last part and tell me how you would solve this problem. I've begun using Golang this morning coming from Python. I want to call a closed-source executable from Go several times, with a bit of concurrency, with different command line arguments. My resulting code is working just well but I'd like to get your input in order to improve it. Since I'm at an early learning stage, I'll also explain my workflow. For the sake of simplicity, assume here that this "external

Close multiple goroutine if an error occurs in one in go

本秂侑毒 提交于 2019-12-17 07:39:36
问题 consider this function : func doAllWork() error { var wg sync.WaitGroup wg.Add(3) for i := 0; i < 2; i++ { go func() { defer wg.Done() for j := 0; j < 10; j++ { result, err := work(j) if err != nil { // can't use `return err` here // what sould I put instead ? os.Exit(0) } } }() } wg.Wait() return nil } In each goroutine, the function work() is called 10 times. If one call to work() returns an error in any of the running goroutines, I want all the goroutines to stop immediately, and the

Max number of goroutines

和自甴很熟 提交于 2019-12-17 07:11:24
问题 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 回答1: If a goroutine is blocked, there is no cost involved other than: memory usage slower garbage-collection

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

烈酒焚心 提交于 2019-12-17 07:03:03
问题 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"

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

蹲街弑〆低调 提交于 2019-12-17 07:02:46
问题 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"

No output from goroutine in Go

有些话、适合烂在心里 提交于 2019-12-16 19:55:14
问题 While SayHello() executes as expected, the goroutine prints nothing. package main import "fmt" func SayHello() { for i := 0; i < 10 ; i++ { fmt.Print(i, " ") } } func main() { SayHello() go SayHello() } 回答1: When your main() function ends, your program ends as well. It does not wait for other goroutines to finish. Quoting from the Go Language Specification: Program Execution: Program execution begins by initializing the main package and then invoking the function main . When that function

why input.Text() is evaluated in the main goroutine

Deadly 提交于 2019-12-13 10:50:33
问题 In chapter 8 of The Go Programming Language, there is a description to the concurrency echo server as below: The arguments to the function started by go are evaluated when the go statement itself is executed; thus input.Text() is evaluated in the main goroutine. I don't understand this. Why the input.Text() is evaluated at the main goroutine? Shouldn't it be in the go echo() goroutine? // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan. // License: https://creativecommons.org

Using a channel for dispatching tasks to go routine

江枫思渺然 提交于 2019-12-13 03:56:36
问题 I am writing a program to render diagrams. Todo so I'm searching for all files and want to dispatch them async to go routines to process them in parallel. But I think I misunderstood the concept of channels. files := umlFiles("uml") // list of strings queue := make(chan string) for i := 0; i < 4; i++ { go func(queue chan string) { file, ok := <-queue if !ok { return } exec.Command("some command", file).Run() }(queue) } for _, f := range files { queue <- f } close(queue) This will run in a