O
O
Oleg Bugrov2016-11-03 14:16:08
go
Oleg Bugrov, 2016-11-03 14:16:08

Why is there no output of the function result to the console when using Go-routines?

As far as I understand, the main thread completes faster than the routines complete, but I don’t understand how to synchronize.

package main

import (
  "fmt"
  "net/http"
  "time"
)

func getResp()  {
  res, err := http.Get("http://ya.ru")
  if err != nil {
    fmt.Println(err)
  }
  fmt.Println(res.Status)

}

func main() {
  start := time.Now()
  for i := 0; i < 10; i++ {
    go getResp()

  }
  elapsed := time.Since(start)
  fmt.Println("Время выполнения  ", elapsed)
}

As far as I understand, the whole problem is in fmt.Println(res.Status) , I tried to remove the output from the function and call it in main via anonymous -
package main

import (
  "fmt"
  "net/http"
  "time"
)

func getResp()string  {
  res, err := http.Get("http://ya.ru")
  if err != nil {
    fmt.Println(err)
  }
  return res.Status

}

func main() {
  start := time.Now()
  for i := 0; i < 10; i++ {
    go func() {fmt.Println(getResp())}()

  }
  elapsed := time.Since(start)
  fmt.Println("Время выполнения  ", elapsed)
}

The result is the same - there is a runtime, but there are no statuses in the console. The campaign needs synchronization, but I can’t figure out how and where.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Andrey K, 2016-11-03
@mrakodav

package main

import (
  "fmt"
  "net/http"
  "sync"
  "time"
)

var wg sync.WaitGroup

func getResp() {
  defer wg.Done()
  res, err := http.Get("http://ya.ru")
  if err != nil {
    fmt.Println(err)
  } else {
    fmt.Println(res.Status)
  }
}

func main() {
  start := time.Now()
  for i := 0; i < 10; i++ {
    wg.Add(1)
    go getResp()

  }
  wg.Wait()
  elapsed := time.Since(start)
  fmt.Println("Время выполнения  ", elapsed)
}

N
Nikita, 2016-11-03
@jetu

Here is an example

O
Oleg Bugrov, 2016-11-03
@mrakodav

Thanks Andrey Kot , I got it more creepy

package main

import (
  "fmt"
  "net/http"
  "sync"
  "time"
)

func getResp() string {
  res, err := http.Get("http://ya.ru")
  if err != nil {
    fmt.Println(err)
    time.Sleep(1)
  }
  return res.Status
}

func main() {
  start := time.Now()
  countThread := &sync.WaitGroup{}
  for i := 0; i < 10; i++ {
    countThread.Add(1)
    go func(cT *sync.WaitGroup) {
      fmt.Println(getResp())
      cT.Done()
    }(countThread)
  }
  countThread.Wait()
  elapsed := time.Since(start)
  fmt.Println("Время выполнения  ", elapsed)
}

V
Vladimir, 2017-01-16
@n1cew0lf

Logs? Router model? On what devices is it repeated?

4
4Proof, 2017-01-19
@4Proof

The router's DHCP IP may be expiring.
Try to set a week, for a home network this is not critical.
Or on a Mac, set the static IP network settings

R
Roman Vladimirovich F., 2017-01-21
@FiLinX

yes, a static IP for each MAC address in LAN, set manually (not auto) the Wi-Fi channel least clogged with neighbors, and naturally change to 8-digit new passwords for the waffle and the route's webmord itself (otherwise, maybe a bunch of neighbors got into you there, and it’s such a waffle, it gives priority to newly connected ones),
but the main thing I think is a bodyaga with a power supply for the route - do it like me, power the route through UPS (at least it’s because of the shnyaga of 220 that the route was constantly stupid. now everything is exactly already with year)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question