Note that there are some explanatory texts on larger screens.

plurals
  1. POgoroutines causing major slowdowns and headaches
    text
    copied!<p>I'm having something of a problem with goroutines. Why is it that this code executes in ~125ms (note sequential execution):</p> <pre><code>package main import ( "os/exec" "time" "fmt" ) func main() { cmd := exec.Command("lessc", "--yui-compress", "test.less") n := 2000 start := time.Now() for i := 0; i &lt; n; i++ { cmd.Run() } finish := time.Now() fmt.Printf("Program took %v to run\n", finish.Sub(start)) } </code></pre> <p>When this code takes about 20 seconds (concurrent execution using goroutines):</p> <pre><code>package main import ( "os/exec" "time" "fmt" ) func main() { cmd := exec.Command("lessc", "--yui-compress", "test.less") ch := make(chan bool) n := 2000 start := time.Now() for i := 0; i &lt; n; i++ { go lessc(ch, cmd) } fmt.Println(n, " goroutines started.") for i := 0; i &lt; n; i++ { _ = &lt;-ch } finish := time.Now() fmt.Printf("Program took %v to run\n", finish.Sub(start)) } func lessc(ch chan bool, c *exec.Cmd) { c.Run() ch &lt;- true } </code></pre> <p>Using go 1.0.3 on i7 720QM (4C/8T) 8GB RAM linux/x86-64 Also built and tested using 1.0.2 and got the same problem on the same machine.</p> <p>Edit: Solved by @jnml below. If anyone cares about the new fixed concurrent code here it is:</p> <pre><code>package main import ( "os/exec" "time" "fmt" ) func main() { ch := make(chan bool) n := 2000 start := time.Now() for i := 0; i &lt; n; i++ { go lessc(ch) } fmt.Println(n, " goroutines started.") for i := 0; i &lt; n; i++ { _ = &lt;-ch } finish := time.Now() fmt.Printf("Program took %v to run\n", finish.Sub(start)) } func lessc(ch chan bool) { cmd := exec.Command("lessc", "--yui-compress", "test.less") cmd.Run() ch &lt;- true } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload