I'm doing some tests with goroutines just to learn how they work, however it seems they are not running at all. I've done a very simple test:
package main
import (
"fmt"
)
func test() {
fmt.Println("test")
}
func main() {
go test()
}
I would expect this to print "test" however it simply doesn't do anything, no message but no error either. I've also tried adding a for {} at the end of the program to give the goroutine time to print something but that didn't help.
Any idea what could be the issue?
program execution does not wait for the invoked function to complete
Go statements
Wait a while. For example,
package main
import (
"fmt"
"time"
)
func test() {
fmt.Println("test")
}
func main() {
go test()
time.Sleep(10 * time.Second)
}
Output:
test
I know it's answered, but for the sake of completeness:
Channels
package main
import (
"fmt"
)
func test(c chan int) {
fmt.Println("test")
// We are done here.
c <- 1
}
func main() {
c := make(chan int)
go test(c)
// Wait for signal on channel c
<- c
}
Related
I am trying to run this below piece of code
package main
import (
"fmt"
"time"
)
func main() {
time.Sleep(time.Millisecond*6000)
fmt.Println("Done")
}
As one expects, it waits for 6 seconds, print "done" and then exits
But if I remove the print statement,
package main
import (
"time"
)
func main() {
time.Sleep(time.Millisecond*6000)
}
it doesn't wait and exits immediately. Why?
Consequently, look at the code below
package main
import (
"fmt"
"time"
)
func main() {
c := make(chan int)
go count(6, c)
time.Sleep(time.Millisecond*5000)
}
func count(num int, c chan int) {
for i := 1; i <= num; i++ {
fmt.Println(i)
c <- i
time.Sleep(time.Millisecond*2000)
}
close(c)
}
Here the count goroutine will get blocked trying to send i to a channel when no receiver is there to read it and the main function immediately exits even though there is a sleep statement after it. But when I remove the statement
c <- i
the count goroutine gets to count till 3 since the main function does wait for those 5 seconds as stated.
What is going on here?
Run it locally, and it will wait. The output on the Go Playground is cached. If there is no output, it doesn't make you wait 6 seconds for nothing. If there is output, the timing of the output is preserved.
Read blog post: The Go Blog: Inside the Go Playground:
We capture the timing of each write to standard output and standard error and provide it to the client. Then the client can "play back" the writes with the correct timing, so that the output appears just as if the program were running locally.
I already set runtime.GOMAXPROCS(2), but this program still hang when output some numbers. I can see high cpu used by this program(more than 100%), but I can't understand why the for loop goroutine can make my program not work.
go version is 1.4.2 on linux/amd64, and my pc has 4 CPUs.
here is the code:
package main
import "fmt"
import "runtime"
import "time"
func forever() {
for {
}
}
func show() {
for number := 1; number < 999999; number++ {
time.Sleep(1000)
fmt.Println(number)
}
}
func main() {
runtime.GOMAXPROCS(2)
go show()
go forever()
for {
time.Sleep(1000)
}
}
There's no need to ever have a busy loop that does nothing except burn CPU time. Not only does it consume an entire OS thread, but goroutines are cooperatively scheduled, and it will interfere with the runtime's goroutines. For example, on Go1.5 this will usually block the stop-the-world phase of the GC, (which you can test by setting GOGC=off).
To make this program run, you could insert a scheduling point in the for loop, but it would be better to remove it altogether.
func forever() {
for {
runtime.Gosched()
}
}
From code, it looks like you want to print the number in for loop in go routine. In this case why not use channel to indicate when go routine is completed with for loop and accordingly exit the main function. Something like this
package main
import "fmt"
import "runtime"
import "time"
func show(result chan bool) {
for number := 1; number < 999999; number++ {
time.Sleep(1000)
fmt.Println(number)
}
result <- true
}
func main() {
runtime.GOMAXPROCS(2)
result := make(chan bool)
go show(result)
<- result
}
It's good to use runtime.Gosched().
But in golang, time.Duration is in Nanoseconds, so time.Sleep(1000) is nearly no sleep. mostly you consider it as milliseconds like things in Java. you can try
time.Sleep( 1000 * time.MilliSecond )
This example taken from tour.golang.org/#63
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}
The output
hello
world
hello
world
hello
world
hello
world
hello
Why world is printed only 4 times instead of 5 ?
Edit: The answer can be quoted from golang specification:
Program execution begins by initializing the main package and then
invoking the function main. When the function main returns, the
program exits. It does not wait for other (non-main) goroutines to
complete.
When your main function ends your program ends, i.e. all goroutines are terminated.
Your main terminates before go say("world") is done. If you sleep some time at the end of main you should see the last world.
Here is how you solve that synchronization problem properly - with sync.WaitGroup
Playground link
package main
import (
"fmt"
"sync"
"time"
)
func say(s string, wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
wg := new(sync.WaitGroup)
wg.Add(2)
go say("world", wg)
go say("hello", wg)
wg.Wait()
fmt.Println("All done")
}
Because the calling gorouting terminates before the second one you spawned does. This causes the second to shut down. To illustrate, modify your code slightly:
package main
import (
"fmt"
"time"
)
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Print(i)
fmt.Println(":"+s)
}
}
func main() {
go say("world")
say("hello")
}
Try putting in a "wait" or a sleep to the end of the main function.
I started to learn go language days ago. When I tried to start writing some fun codes, I am stuck by a strange behavior.
package main
import "fmt"
func recv(value int) {
if value < 0 {
return
}
fmt.Println(value)
go recv(value-1)
}
func main() {
recv(10)
}
when I run the above code, only 10 is printed. When I remove the go before the call to recv, 10 to 0 are printed out. I believe I am misusing go routine here, but I can not understand why it failed start a go routine this way.
When the main function returns, Go will not wait for any still existing goroutines to finish but instead just exit.
recv will return to main after the first "iteration" and because main has nothing more to do, the program will terminate.
One solution to this problem is to have a channel that signals that all work is done, like the following:
package main
import "fmt"
func recv(value int, ch chan bool) {
if value < 0 {
ch <- true
return
}
fmt.Println(value)
go recv(value - 1, ch)
}
func main() {
ch := make(chan bool)
recv(10, ch)
<-ch
}
Here, recv will send a single boolean before returning, and main will wait for that message on the channel.
For the logic of the program, it does not matter what type or specific value you use. bool and true are just a straightforward example. If you want to be more efficient, using a chan struct{} instead of a chan bool will save you an additional byte, since empty structs do not use any memory.
A sync.Waitgroup is another solution and specifically intended for the purpose of waiting for an arbitrary amount of goroutines to run their course.
package main
import (
"fmt"
"sync"
)
func recv(value int, wg *sync.WaitGroup) {
if value < 0 {
return
}
fmt.Println(value)
wg.Add(1) // Add 1 goroutine to the waitgroup.
go func() {
recv(value-1, wg)
wg.Done() // This goroutine is finished.
}()
}
func main() {
var wg sync.WaitGroup
recv(10, &wg)
// Block until the waitgroup signals
// all goroutines to be finished.
wg.Wait()
}
I did so and also worked. How come?
package main
import "fmt"
func recv(value int) {
if value < 0 {
return
}
fmt.Println(value)
recv(value - 1)
}
func main() {
recv(10)
}
I've a long running server written in Go. Main fires off several goroutines where the logic of the program executes. After that main does nothing useful. Once main exits, the program will quit. The method I am using right now to keep the program running is just a simple call to fmt.Scanln(). I'd like to know how others keep main from exiting. Below is a basic example. What ideas or best practices could be used here?
I considered creating a channel and delaying exit of main by receiving on said channel, but I think that could be problematic if all my goroutines become inactive at some point.
Side note: In my server (not the example), the program isn't actually running connected to a shell, so it doesn't really make sense to interact with the console anyway. For now it works, but I'm looking for the "correct" way, assuming there is one.
package main
import (
"fmt"
"time"
)
func main() {
go forever()
//Keep this goroutine from exiting
//so that the program doesn't end.
//This is the focus of my question.
fmt.Scanln()
}
func forever() {
for ; ; {
//An example goroutine that might run
//indefinitely. In actual implementation
//it might block on a chanel receive instead
//of time.Sleep for example.
fmt.Printf("%v+\n", time.Now())
time.Sleep(time.Second)
}
}
Block forever. For example,
package main
import (
"fmt"
"time"
)
func main() {
go forever()
select {} // block forever
}
func forever() {
for {
fmt.Printf("%v+\n", time.Now())
time.Sleep(time.Second)
}
}
The current design of Go's runtime assumes that the programmer is responsible for detecting when to terminate a goroutine and when to terminate the program. The programmer needs to compute the termination condition for goroutines and also for the entire program. A program can be terminated in a normal way by calling os.Exit or by returning from the main() function.
Creating a channel and delaying exit of main() by immediately receiving on said channel is a valid approach of preventing main from exiting. But it does not solve the problem of detecting when to terminate the program.
If the number of goroutines cannot be computed before the main() function enters the wait-for-all-goroutines-to-terminate loop, you need to be sending deltas so that main function can keep track of how many goroutines are in flight:
// Receives the change in the number of goroutines
var goroutineDelta = make(chan int)
func main() {
go forever()
numGoroutines := 0
for diff := range goroutineDelta {
numGoroutines += diff
if numGoroutines == 0 { os.Exit(0) }
}
}
// Conceptual code
func forever() {
for {
if needToCreateANewGoroutine {
// Make sure to do this before "go f()", not within f()
goroutineDelta <- +1
go f()
}
}
}
func f() {
// When the termination condition for this goroutine is detected, do:
goroutineDelta <- -1
}
An alternative approach is to replace the channel with sync.WaitGroup. A drawback of this approach is that wg.Add(int) needs to be called before calling wg.Wait(), so it is necessary to create at least 1 goroutine in main() while subsequent goroutines can be created in any part of the program:
var wg sync.WaitGroup
func main() {
// Create at least 1 goroutine
wg.Add(1)
go f()
go forever()
wg.Wait()
}
// Conceptual code
func forever() {
for {
if needToCreateANewGoroutine {
wg.Add(1)
go f()
}
}
}
func f() {
// When the termination condition for this goroutine is detected, do:
wg.Done()
}
Go's runtime package has a function called runtime.Goexit that will do exactly what you want.
Calling Goexit from the main goroutine terminates that goroutine
without func main returning. Since func main has not returned,
the program continues execution of other goroutines.
If all other goroutines exit, the program crashes.
Example in the playground
package main
import (
"fmt"
"runtime"
"time"
)
func main() {
go func() {
time.Sleep(time.Second)
fmt.Println("Go 1")
}()
go func() {
time.Sleep(time.Second * 2)
fmt.Println("Go 2")
}()
runtime.Goexit()
fmt.Println("Exit")
}
Nobody mentioned signal.Notify(c chan<- os.Signal, sig ...os.Signal)
Example:
package main
import (
"fmt"
"time"
"os"
"os/signal"
"syscall"
)
func main() {
go forever()
quitChannel := make(chan os.Signal, 1)
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
//time for cleanup before exit
fmt.Println("Adios!")
}
func forever() {
for {
fmt.Printf("%v+\n", time.Now())
time.Sleep(time.Second)
}
}
Here is a simple block forever using channels
package main
import (
"fmt"
"time"
)
func main() {
done := make(chan bool)
go forever()
<-done // Block forever
}
func forever() {
for {
fmt.Printf("%v+\n", time.Now())
time.Sleep(time.Second)
}
}
You could daemonize the process using Supervisor (http://supervisord.org/). Your function forever would just be a process that it runs, and it would handle the part of your function main. You would use the supervisor control interface to start/shutdown/check on your process.