dongshilve4392
2015-01-06 05:04
浏览 281
已采纳

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

How to make two functions calls f1(2) and f1(1) execute in parallel so that all the program would execute for 2 seconds not for 3.

package main

import (
    "fmt"
    "time"
)

// sleeps for `secs` seconds
func f1(secs time.Duration) (result string) {
    fmt.Printf("waiting %V
", 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

I guess I can do it only with channels. Should I redefine function f1 or I can leave it as is and change only way I call it?

图片转代码服务由CSDN问答提供 功能建议

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

  package main 
 
import(
“ fmt” 
  “ time” 
)
 
 //睡眠“ secs”秒
func f1(秒time.Duration)(结果字符串){
 fmt.Printf(“ waiting%V 
”,secs)
  time.Sleep(secs * time.Second)
结果= fmt.Sprintf(“等待%d秒”,secs)
返回
} 
 
 //显示arg1,arg2 
func f2(arg1,  arg2字符串){
 fmt.Println(arg1)
 fmt.Println(arg2)
} 
 
 //此函数执行3秒钟,因为等待了很多
func runNotParallel(){
 
  out1:= f1(2)
 out2:= f1(1)
 f2(out1,out2)
 
} 
 
 // golang并行返回函数
 // todo:使其全部运行 该函数将执行2秒钟,而不是3 
func runParallel(){
 out1:= f1(2)
 out2:= f1(1)
 f2(out1,out2)
} 
 
func main  (){
 runNotParallel()
  runParallel()
} 
   
 
 

游乐场< / a>

我想我只能通过渠道来做到这一点。 我应该重新定义函数 f1 还是让它保持不变并仅更改其调用方式?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douqiang3768 2015-01-06 05:15
    已采纳

    Use chan/goroutine

    package main
    
    import (
        "fmt"
        "time"
    )
    
    // sleeps for `secs` seconds
    func f1(secs time.Duration) (result string) {
        fmt.Printf("waiting %v
    ", 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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dtnbjjq51949 2017-06-07 03:22

    Another way you could do it is using WaitGroup

    I wrote this utility function to help parallelize a group of functions:

    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)
        }
    }
    

    So in your case, we could do this

    value1 := ""
    value2 := ""
    
    func1 := func() {
        value1 = f1(2)
    }
    
    func2 = func() {
        value2 = f1(1)
    }
    
    Parallelize(func1, func2)
    
    f2(out1, out2)
    

    If you wanted to use the Parallelize function, you can find it here https://github.com/shomali11/util

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题