Skip to content

Instantly share code, notes, and snippets.

@samuell
Created November 27, 2015 13:12
Show Gist options
  • Save samuell/630467732703feefcc81 to your computer and use it in GitHub Desktop.
Save samuell/630467732703feefcc81 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"os/exec"
"sync"
)
func main() {
wg := new(sync.WaitGroup)
for i := 0; i < 2500; i++ {
wg.Add(1)
go func(i int) {
var b bytes.Buffer
fmt.Println("Starting sleep ", i, "...")
cmd := exec.Command("sleep", "3600")
cmd.Stdout = &b
go func() {
output := make([]byte, 100)
_, err := b.Read(output)
if err != nil {
fmt.Println(i, "says ", string(output))
} else {
fmt.Println(i, "error ", err)
}
cmd.Wait()
wg.Done()
}()
err := cmd.Start()
if err != nil {
panic(err)
}
fmt.Println("Finishing sleep ", i, "...")
}(i)
}
fmt.Println("Waiting for WaitGroup ...")
wg.Wait()
fmt.Println("WaitGroup finished!")
}
@samuell
Copy link
Author

samuell commented Nov 27, 2015

Running it:

$ ulimit -n 10000
$ go run jiangyds_code_example.go

... and checking number of threads in another terminal:

$ ps -eLf | grep jiangyds_code_example | wc -l
2835

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment