golang 에서 흔히 볼 수 있 는 몇 가지 병발 모델 프레임 워 크

3649 단어 go.
골 랑 에서 협 정 을 자주 사용 하여 높 은 병발 을 하 는데 본 고 는 몇 가지 흔히 볼 수 있 는 병발 모델 을 열거 했다.
package main

import (
	"fmt"
	"math/rand"
	"os"
	"runtime"
	"sync"
	"sync/atomic"
	"time"
)

type Scenario struct {
	Name        string
	Description []string
	Examples    []string
	RunExample  func()
}

var s1 = &Scenario{
	Name: "s1",
	Description: []string{
		"        ",
	},
	Examples: []string{
		"             ",
	},
	RunExample: RunScenario1,
}

var s2 = &Scenario{
	Name: "s2",
	Description: []string{
		"            ",
	},
	Examples: []string{
		"      ,            ,        ",
	},
	RunExample: RunScenario2,
}

var s3 = &Scenario{
	Name: "s3",
	Description: []string{
		"             , goroutine worker pool",
	},
	Examples: []string{
		"                TB/GB   ",
	},
	RunExample: RunScenario3,
}

var s4 = &Scenario{
	Name: "s4",
	Description: []string{
		"          (goroutine+select+channel)",
	},
	Examples: []string{
		"",
	},
	RunExample: RunScenario4,
}

var s5 = &Scenario{
	Name: "s5",
	Description: []string{
		"       (Ticker)",
	},
	Examples: []string{
		"           ,       :    ,IOPS,    ",
	},
	RunExample: RunScenario5,
}

var Scenarios []*Scenario

func init() {
	Scenarios = append(Scenarios, s1)
	Scenarios = append(Scenarios, s2)
	Scenarios = append(Scenarios, s3)
	Scenarios = append(Scenarios, s4)
	Scenarios = append(Scenarios, s5)
}

//           
func main() {
	if len(os.Args) == 1 {
		fmt.Println("        ==> ")
		for _, sc := range Scenarios {
			fmt.Printf("  : %s ,", sc.Name)
			printDescription(sc.Description)
		}
		return
	}
	for _, arg := range os.Args[1:] {
		sc := matchScenario(arg)
		if sc != nil {
			printDescription(sc.Description)
			printExamples(sc.Examples)
			sc.RunExample()
		}
	}
}

func printDescription(str []string) {
	fmt.Printf("    : %s 
", str) } func printExamples(str []string) { fmt.Printf(" : %s
", str) } func matchScenario(name string) *Scenario { for _, sc := range Scenarios { if sc.Name == name { return sc } } return nil } var doSomething = func(i int) string { time.Sleep(time.Millisecond * time.Duration(10)) fmt.Printf("Goroutine %d do things ....
", i) return fmt.Sprintf("Goroutine %d", i) } var takeSomthing = func(res string) string { time.Sleep(time.Millisecond * time.Duration(10)) tmp := fmt.Sprintf("Take result from %s....
", res) fmt.Println(tmp) return tmp } // 1: func RunScenario1() { count := 10 var wg sync.WaitGroup for i := 0; i < count; i++ { wg.Add(1) go func(index int) { defer wg.Done() doSomething(index) }(i) } wg.Wait() } // 2: func RunScenario2() { timeout := time.Now().Add(time.Second * time.Duration(10)) n := runtime.NumCPU() waitForAll := make(chan struct{}) done := make(chan struct{}) concurrentCount := make(chan struct{}, n) for i := 0; i < n; i++ { concurrentCount 50 { return true } else { return false } } // 5: (Ticker) // , : , func RunScenario5() { totalSize := int64(0) totalCount := int64(0) totalErr := int64(0) concurrencyCount := runtime.NumCPU() stop := make(chan struct{}) fileSizeExample := int64(10) timeout := 10 // seconds to stop go func() { for i := 0; i < concurrencyCount; i++ { go func(index int) { for { select { case timeout { t.Stop() close(stop) return } } } }

좋은 웹페이지 즐겨찾기