golang函数:带返回的并行执行

时间:2015-01-06 05:04:10

标签: go goroutine

如何使两个函数调用f1(2)f1(1)并行执行,以便所有程序执行2秒而不是3秒。

package main

import (
    "fmt"
    "time"
)

// sleeps for `secs` seconds
func f1(secs time.Duration) (result string) {
    fmt.Printf("waiting %V\n", secs)
    time.Sleep(secs * time.Second)
    result = fmt.Sprintf("waited for %d seconds", secs)
    return
}

// prints arg1, arg2
func f2(arg1, arg2 string) {
    fmt.Println(arg1)
    fmt.Println(arg2)
}

// this function executes for 3 seconds, because waits a lot
func runNotParallel() {

    out1 := f1(2)
    out2 := f1(1)
    f2(out1, out2)

}

// golang parallel return functions
// todo: make it run so all the function will executes for 2 seconds not for 3
func runParallel() {
    out1 := f1(2)
    out2 := f1(1)
    f2(out1, out2)
}

func main() {
    runNotParallel()
    runParallel()
}

playground

我想我只能用频道来做。我应该重新定义函数f1还是我可以保留原样,只改变我调用的方式?

3 个答案:

答案 0 :(得分:11)

使用chan / goroutine

package main

import (
    "fmt"
    "time"
)

// sleeps for `secs` seconds
func f1(secs time.Duration) (result string) {
    fmt.Printf("waiting %v\n", secs)
    time.Sleep(secs * time.Second)
    result = fmt.Sprintf("waited for %v seconds", secs)
    return
}

// prints arg1, arg2
func f2(arg1, arg2 string) {
    fmt.Println(arg1)
    fmt.Println(arg2)
}

// this function executes for 3 seconds, because waits a lot
func runNotParallel() {
    out1 := f1(2)
    out2 := f1(1)
    f2(out1, out2)

}

// golang parallel return functions
// todo: make it run so all the function will executes for 2 seconds not for 3
func runParallel() {
    out1 := make(chan string)
    out2 := make(chan string)
    go func() {
        out1 <- f1(2)
    }()
    go func() {
        out2 <- f1(1)
    }()
    f2(<-out1, <-out2)
}

func main() {
    runNotParallel()
    runParallel()
}

https://play.golang.org/p/G4RHiq9LJw

答案 1 :(得分:2)

另一种方法是使用WaitGroup

我编写了这个实用程序函数来帮助并行化一组函数:

import "sync"

// Parallelize parallelizes the function calls
func Parallelize(functions ...func()) {
    var waitGroup sync.WaitGroup
    waitGroup.Add(len(functions))

    defer waitGroup.Wait()

    for _, function := range functions {
        go func(copy func()) {
            defer waitGroup.Done()
            copy()
        }(function)
    }
}

所以在你的情况下,我们可以做到这一点

value1 := ""
value2 := ""

func1 := func() {
    value1 = f1(2)
}

func2 = func() {
    value2 = f1(1)
}

Parallelize(func1, func2)

f2(out1, out2)

如果您想使用Parallelize功能,可以在https://github.com/shomali11/util

找到它

答案 2 :(得分:0)

这是一个没有通道但缺少f2同步的解决方案:

package main

import (
    "fmt"
    "sync"
    "time"
)

// sleeps for `secs` seconds
func f1(secs time.Duration, result *string, sg *sync.WaitGroup) () {
    fmt.Printf("waiting %v\n", secs)
    time.Sleep(secs * time.Second)
    *result = fmt.Sprintf("waited for %d seconds", secs)
    if sg!= nil {
        sg.Done()
    }
    return
}

// prints arg1, arg2
func f2(arg1, arg2 string) {
    fmt.Println(arg1)
    fmt.Println(arg2)
}

// this function executes for 3 seconds, because waits a lot
func runNotParallel() {

    var out1, out2 string
    f1(2, &out1, nil)
    f1(1, &out2,nil)
    f2(out1, out2)

}

// golang parallel return functions
// todo: make it run so all the function will executes for 2 seconds not for 3
func runParallel() {
    var sg sync.WaitGroup
    sg.Add(2)
    var out1, out2 string
    go f1(2, &out1, &sg)
    go f1(1, &out2, &sg)
    sg.Wait()
    f2(out1, out2)
}

func main() {
    runNotParallel()
    runParallel()
}

基本上,go 运算符阻止使用/访问返回值,但可以使用签名中返回占位符的指针来完成