简体   繁体   中英

Go-routines with channels not running parallely

package main
import "time"
func main() {
    stringCh := make(chan func() (string))
    go func() {
        stringCh <- func() (string) {
            return stringReturner()
        }
        close(stringCh)
    }()

    intCh := make(chan func() (int))
    go func() {
        intCh <- func() (int) {
            return intReturner()
        }
        close(intCh)
    }()

    str := (<-stringCh)()
    print("Printing str: ", str,"\n\n")
    num := (<-intCh)()
    print("Printing int: ", num,"\n\n")     
}

func intReturner()int{
    time.Sleep(5 * time.Second)
    print("Inside int returner\n\n");
    return 1;
}

func stringReturner()string{
    time.Sleep(5 * time.Second)
    print("Inside string returner\n\n");
    return "abcd";
}

Output:

Inside string returner

Printing str: abcd

WAIT OF 5 SECONDS

Inside int returner

Printing int: 1

https://play.golang.org/p/oE2ybs7Jo-W

Why is this coding taking 10 seconds to execute instead of 5? We are parallelizing the calls by spawning two go-routines right (1 for string returner and 1 for int returner), but why is the int returner executing after the string returner executes?

The channel is returning function with sleep, and such function got called in sequential manner. That is why it will take 10s in general.

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

Thats because you are only writing to the channels parallelly. But the read from the channels are happening one-after-the-other. To fix this, you can read from both channels from separate go routines

var wg sync.WaitGroup
wg.Add(2)
go func() {
  defer wg.Done()
  str := (<-stringCh)()
  print("Printing str: ", str, "\n\n")
}()
go func() {
  defer wg.Done()
  num := (<-intCh)()
  print("Printing int: ", num, "\n\n")
}()
wg.Wait()

Based on the suggestions moving the sleep method into go routine to run both functions concurrently in 5s:

package main

import "time"

func main() {
    stringCh := make(chan func() string)
    go func() {
        time.Sleep(5 * time.Second)
        stringCh <- func() string {
            return stringReturner()
        }
        close(stringCh)
    }()

    intCh := make(chan func() int)
    go func() {
        time.Sleep(5 * time.Second)
        intCh <- func() int {
            return intReturner()
        }
        close(intCh)
    }()

    str := (<-stringCh)()
    print("Printing str: ", str, "\n\n")
    num := (<-intCh)()
    print("Printing int: ", num, "\n\n")
}

func intReturner() int {
    print("Inside int returner\n\n")
    return 1
}

func stringReturner() string {
    print("Inside string returner\n\n")
    return "abcd"
}

Output:

Inside string returner

Printing str: abcd

Inside int returner

Printing int: 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM